jdk/src/solaris/classes/sun/nio/ch/DevPollArrayWrapper.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 2057 3acf8e5e2ca0
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2001-2008 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 sun.misc.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Manipulates a native array of pollfd structs on Solaris:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * typedef struct pollfd {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *    short events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *    short revents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * } pollfd_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class DevPollArrayWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // Event masks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static final short POLLIN       = 0x0001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final short POLLPRI      = 0x0002;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final short POLLOUT      = 0x0004;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final short POLLRDNORM   = 0x0040;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final short POLLWRNORM   = POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final short POLLRDBAND   = 0x0080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final short POLLWRBAND   = 0x0100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final short POLLNORM     = POLLRDNORM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final short POLLERR      = 0x0008;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final short POLLHUP      = 0x0010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final short POLLNVAL     = 0x0020;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final short POLLREMOVE   = 0x0800;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final short POLLCONN     = POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Miscellaneous constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final short SIZE_POLLFD   = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final short FD_OFFSET     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final short EVENT_OFFSET  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final short REVENT_OFFSET = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Maximum number of open file descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final int   OPEN_MAX      = fdLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Number of pollfd structures to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // DP_POLL ioctl allows up to OPEN_MAX-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final int   NUM_POLLFDS   = Math.min(OPEN_MAX-1, 8192);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Base address of the native pollArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private long pollArrayAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Maximum number of POLL_FD structs to update at once
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private int MAX_UPDATE_SIZE = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    DevPollArrayWrapper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int allocationSize = NUM_POLLFDS * SIZE_POLLFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        pollArray = new AllocatedNativeObject(allocationSize, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        wfd = init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for (int i=0; i<NUM_POLLFDS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            putDescriptor(i, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            putEventOps(i, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            putReventOps(i, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // Machinery for remembering fd registration changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // A hashmap could be used but the number of changes pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // is expected to be small
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static class Updator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Updator(int fd, int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            this.mask = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private LinkedList<Updator> updateList = new LinkedList<Updator>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // The pollfd array for results from devpoll driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private AllocatedNativeObject pollArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // The fd of the devpoll driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    int wfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // The fd of the interrupt line going out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    int outgoingInterruptFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // The fd of the interrupt line coming in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    int incomingInterruptFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // The index of the interrupt FD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    int interruptedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // Number of updated pollfd entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    int updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    void initInterrupt(int fd0, int fd1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        outgoingInterruptFD = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        incomingInterruptFD = fd0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        register(wfd, fd0, POLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    void putEventOps(int i, int event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int offset = SIZE_POLLFD * i + EVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        pollArray.putShort(offset, (short)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    void putReventOps(int i, int revent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int offset = SIZE_POLLFD * i + REVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        pollArray.putShort(offset, (short)revent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    void putDescriptor(int i, int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int offset = SIZE_POLLFD * i + FD_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        pollArray.putInt(offset, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    int getEventOps(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int offset = SIZE_POLLFD * i + EVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return pollArray.getShort(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    int getReventOps(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int offset = SIZE_POLLFD * i + REVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return pollArray.getShort(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    int getDescriptor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int offset = SIZE_POLLFD * i + FD_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return pollArray.getInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    void setInterest(int fd, int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            updateList.add(new Updator(fd, mask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    void release(int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            updateList.add(new Updator(fd, POLLREMOVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    void closeDevPollFD() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        FileDispatcher.closeIntFD(wfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    int poll(long timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        updateRegistrations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        updated = poll0(pollArrayAddress, NUM_POLLFDS, timeout, wfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        for (int i=0; i<updated; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (getDescriptor(i) == incomingInterruptFD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                interruptedIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    void updateRegistrations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // take snapshot of the updateList size to see if there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // any registrations to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int updateSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            updateSize = updateList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (updateSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // Construct a pollfd array with updated masks; we may overallocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // by some amount because if the events are already POLLREMOVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // then the second pollfd of that pair will not be needed. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // number of entries is limited to a reasonable number to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // allocating a lot of memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            int maxUpdates = Math.min(updateSize * 2, MAX_UPDATE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            int allocationSize =  maxUpdates * SIZE_POLLFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            AllocatedNativeObject updatePollArray =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                new AllocatedNativeObject(allocationSize, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    while (updateList.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        // We have to insert a dummy node in between each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        // real update to use POLLREMOVE on the fd first because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        // otherwise the changes are simply OR'd together
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        Updator u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        while ((u = updateList.poll()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            // First add pollfd struct to clear out this fd
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   220
                            putPollFD(updatePollArray, index, u.fd, POLLREMOVE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            // Now add pollfd to update this fd, if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            if (u.mask != POLLREMOVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                putPollFD(updatePollArray, index, u.fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                          (short)u.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                            // Check against the max allocation size; these are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            // all we will process. Valid index ranges from 0 to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            // (maxUpdates - 1) and we can use up to 2 per loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            if (index > maxUpdates - 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        // Register the changes with /dev/poll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        registerMultiple(wfd, updatePollArray.address(), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                // Free the native array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                updatePollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                // BUG: If an exception was thrown then the selector now believes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                // that the last set of changes was updated but it probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                // was not. This should not be a likely occurrence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private void putPollFD(AllocatedNativeObject array, int index, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                           short event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int structIndex = SIZE_POLLFD * index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        array.putInt(structIndex + FD_OFFSET, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        array.putShort(structIndex + EVENT_OFFSET, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        array.putShort(structIndex + REVENT_OFFSET, (short)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void interrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        interrupt(outgoingInterruptFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public int interruptedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return interruptedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    boolean interrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return interrupted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    void clearInterrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private native int init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private native void register(int wfd, int fd, int mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private native void registerMultiple(int wfd, long address, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private native int poll0(long pollAddress, int numfds, long timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                             int wfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private static native void interrupt(int fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private static native int fdLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}