jdk/src/solaris/classes/sun/nio/ch/EPollArrayWrapper.java
author alanb
Sun, 15 Feb 2009 12:25:54 +0000
changeset 2057 3acf8e5e2ca0
parent 1247 b4c26443dee5
child 2433 9f0ab7f726b8
permissions -rw-r--r--
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99 4313887: New I/O: Improved filesystem interface 4607272: New I/O: Support asynchronous I/O Reviewed-by: sherman, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
     2
 * Copyright 2005-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Manipulates a native array of epoll_event structs on Linux:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * typedef union epoll_data {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *     void *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *     int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *     __uint32_t u32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *     __uint64_t u64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  } epoll_data_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * struct epoll_event {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *     __uint32_t events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     epoll_data_t data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The system call to wait for I/O events is epoll_wait(2). It populates an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * array of epoll_event structures that are passed to the call. The data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * member of the epoll_event structure contains the same data as was set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * when the file descriptor was registered to epoll via epoll_ctl(2). In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * this implementation we set data.fd to be the file descriptor that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * register. That way, we have the file descriptor available when we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * process the events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * All file descriptors registered with epoll have the POLLHUP and POLLERR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * events enabled even when registered with an event set of 0. To ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * that epoll_wait doesn't poll an idle file descriptor when the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * connection is closed or reset then its registration is deleted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * epoll (it will be re-added again if the event set is changed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
class EPollArrayWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // EPOLL_EVENTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final int EPOLLIN      = 0x001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // opcodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final int EPOLL_CTL_ADD      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static final int EPOLL_CTL_DEL      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final int EPOLL_CTL_MOD      = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Miscellaneous constants
1142
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    72
    static final int SIZE_EPOLLEVENT  = sizeofEPollEvent();
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    73
    static final int EVENT_OFFSET     = 0;
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    74
    static final int DATA_OFFSET      = offsetofData();
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    75
    static final int FD_OFFSET        = DATA_OFFSET;
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    76
    static final int NUM_EPOLLEVENTS  = Math.min(fdLimit(), 8192);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Base address of the native pollArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final long pollArrayAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Set of "idle" file descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final HashSet<Integer> idleSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    EPollArrayWrapper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // creates the epoll file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        epfd = epollCreate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // the epoll_event array passed to epoll_wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int allocationSize = NUM_EPOLLEVENTS * SIZE_EPOLLEVENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        pollArray = new AllocatedNativeObject(allocationSize, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        for (int i=0; i<NUM_EPOLLEVENTS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            putEventOps(i, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            putData(i, 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // create idle set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        idleSet = new HashSet<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // Used to update file description registrations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static class Updator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Updator(int opcode, int fd, int events) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.opcode = opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            this.events = events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private LinkedList<Updator> updateList = new LinkedList<Updator>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // The epoll_event array for results from epoll_wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private AllocatedNativeObject pollArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // The fd of the epoll driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    final int epfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // The fd of the interrupt line going out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    int outgoingInterruptFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // The fd of the interrupt line coming in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    int incomingInterruptFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // The index of the interrupt FD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    int interruptedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // Number of updated pollfd entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    int updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    void initInterrupt(int fd0, int fd1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        outgoingInterruptFD = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        incomingInterruptFD = fd0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        epollCtl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    void putEventOps(int i, int event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        pollArray.putInt(offset, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void putData(int i, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int offset = SIZE_EPOLLEVENT * i + DATA_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        pollArray.putLong(offset, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    void putDescriptor(int i, int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int offset = SIZE_EPOLLEVENT * i + FD_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        pollArray.putInt(offset, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    int getEventOps(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return pollArray.getInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    int getDescriptor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int offset = SIZE_EPOLLEVENT * i + FD_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return pollArray.getInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Update the events for a given file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    void setInterest(int fd, int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // if the interest events are 0 then add to idle set, and delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // from epoll if registered (or pending)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (mask == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (idleSet.add(fd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    updateList.add(new Updator(EPOLL_CTL_DEL, fd, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // if file descriptor is idle then add to epoll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (!idleSet.isEmpty() && idleSet.remove(fd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                updateList.add(new Updator(EPOLL_CTL_ADD, fd, mask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // if the previous pending operation is to add this file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // to epoll then update its event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (updateList.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                Updator last = updateList.getLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (last.fd == fd && last.opcode == EPOLL_CTL_ADD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    last.events = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // update existing registration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            updateList.add(new Updator(EPOLL_CTL_MOD, fd, mask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Add a new file descriptor to epoll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    void add(int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            updateList.add(new Updator(EPOLL_CTL_ADD, fd, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Remove a file descriptor from epoll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    void release(int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // if file descriptor is idle then remove from idle set, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // delete from epoll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (!idleSet.remove(fd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                updateList.add(new Updator(EPOLL_CTL_DEL, fd, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Close epoll file descriptor and free poll array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    void closeEPollFD() throws IOException {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   227
        FileDispatcherImpl.closeIntFD(epfd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    int poll(long timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        updateRegistrations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        updated = epollWait(pollArrayAddress, NUM_EPOLLEVENTS, timeout, epfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for (int i=0; i<updated; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (getDescriptor(i) == incomingInterruptFD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                interruptedIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Update the pending registrations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    void updateRegistrations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            Updator u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            while ((u = updateList.poll()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                epollCtl(epfd, u.opcode, u.fd, u.events);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // interrupt support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public void interrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        interrupt(outgoingInterruptFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public int interruptedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return interruptedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    boolean interrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return interrupted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    void clearInterrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private native int epollCreate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private native void epollCtl(int epfd, int opcode, int fd, int events);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private native int epollWait(long pollAddress, int numfds, long timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                 int epfd) throws IOException;
1142
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
   283
    private static native int sizeofEPollEvent();
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
   284
    private static native int offsetofData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private static native int fdLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static native void interrupt(int fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}