src/java.base/solaris/classes/sun/nio/ch/DevPollArrayWrapper.java
author alanb
Thu, 15 Mar 2018 10:47:58 +0000
changeset 49248 15a0e60c8b97
parent 47216 71c04702a3d5
child 49290 07779973cbe2
permissions -rw-r--r--
8199611: (se) Minor selector implementation clean-up Reviewed-by: clanger, redestad, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2444
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2444
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2444
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2444
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2444
diff changeset
    23
 * questions.
2
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;
20176
59b7a49e7f8b 8024883: (se) SelectableChannel.register throws NPE if fd >= 64k (lnx)
alanb
parents: 19607
diff changeset
    29
import java.security.AccessController;
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    30
import java.util.BitSet;
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    31
import java.util.Map;
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    32
import java.util.HashMap;
20176
59b7a49e7f8b 8024883: (se) SelectableChannel.register throws NPE if fd >= 64k (lnx)
alanb
parents: 19607
diff changeset
    33
import sun.security.action.GetIntegerAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Manipulates a native array of pollfd structs on Solaris:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * typedef struct pollfd {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *    short events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *    short revents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * } pollfd_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class DevPollArrayWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // Event masks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final short POLLIN       = 0x0001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final short POLLPRI      = 0x0002;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final short POLLOUT      = 0x0004;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final short POLLRDNORM   = 0x0040;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final short POLLWRNORM   = POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final short POLLRDBAND   = 0x0080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final short POLLWRBAND   = 0x0100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final short POLLNORM     = POLLRDNORM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final short POLLERR      = 0x0008;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final short POLLHUP      = 0x0010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final short POLLNVAL     = 0x0020;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final short POLLREMOVE   = 0x0800;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final short POLLCONN     = POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Miscellaneous constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final short SIZE_POLLFD   = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static final short FD_OFFSET     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final short EVENT_OFFSET  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final short REVENT_OFFSET = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
    72
    // Special value to indicate that an update should be ignored
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
    73
    static final byte  IGNORE        = (byte)-1;
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // Maximum number of open file descriptors
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 12868
diff changeset
    76
    static final int   OPEN_MAX      = IOUtil.fdLimit();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Number of pollfd structures to create.
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    79
    // dpwrite/ioctl(DP_POLL) allows up to OPEN_MAX-1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final int   NUM_POLLFDS   = Math.min(OPEN_MAX-1, 8192);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
    82
    // Initial size of arrays for fd registration changes
20176
59b7a49e7f8b 8024883: (se) SelectableChannel.register throws NPE if fd >= 64k (lnx)
alanb
parents: 19607
diff changeset
    83
    private static final int INITIAL_PENDING_UPDATE_SIZE = 64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    85
    // maximum size of updatesLow
20176
59b7a49e7f8b 8024883: (se) SelectableChannel.register throws NPE if fd >= 64k (lnx)
alanb
parents: 19607
diff changeset
    86
    private static final int MAX_UPDATE_ARRAY_SIZE = AccessController.doPrivileged(
59b7a49e7f8b 8024883: (se) SelectableChannel.register throws NPE if fd >= 64k (lnx)
alanb
parents: 19607
diff changeset
    87
        new GetIntegerAction("sun.nio.ch.maxUpdateArraySize", Math.min(OPEN_MAX, 64*1024)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // The pollfd array for results from devpoll driver
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    90
    private final AllocatedNativeObject pollArray;
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    91
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    92
    // Base address of the native pollArray
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    93
    private final long pollArrayAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // The fd of the devpoll driver
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    96
    private int wfd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // The fd of the interrupt line going out
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
    99
    private int outgoingInterruptFD;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // The fd of the interrupt line coming in
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   102
    private int incomingInterruptFD;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // The index of the interrupt FD
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   105
    private int interruptedIndex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // Number of updated pollfd entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    int updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   110
    // object to synchronize fd registration changes
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   111
    private final Object updateLock = new Object();
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   112
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   113
    // number of file descriptors with registration changes pending
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   114
    private int updateCount;
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   115
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   116
    // file descriptors with registration changes pending
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   117
    private int[] updateDescriptors = new int[INITIAL_PENDING_UPDATE_SIZE];
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   118
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   119
    // events for file descriptors with registration changes pending, indexed
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   120
    // by file descriptor and stored as bytes for efficiency reasons. For
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   121
    // file descriptors higher than MAX_UPDATE_ARRAY_SIZE (unlimited case at
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   122
    // least then the update is stored in a map.
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   123
    private final byte[] eventsLow = new byte[MAX_UPDATE_ARRAY_SIZE];
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   124
    private Map<Integer,Byte> eventsHigh;
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   125
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   126
    // Used by release and updateRegistrations to track whether a file
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   127
    // descriptor is registered with /dev/poll.
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   128
    private final BitSet registered = new BitSet();
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   129
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   130
    DevPollArrayWrapper() {
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   131
        int allocationSize = NUM_POLLFDS * SIZE_POLLFD;
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   132
        pollArray = new AllocatedNativeObject(allocationSize, true);
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   133
        pollArrayAddress = pollArray.address();
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   134
        wfd = init();
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   135
        if (OPEN_MAX > MAX_UPDATE_ARRAY_SIZE)
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   136
            eventsHigh = new HashMap<>();
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   137
    }
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   138
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void initInterrupt(int fd0, int fd1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        outgoingInterruptFD = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        incomingInterruptFD = fd0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        register(wfd, fd0, POLLIN);
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 putReventOps(int i, int revent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int offset = SIZE_POLLFD * i + REVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        pollArray.putShort(offset, (short)revent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    int getEventOps(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int offset = SIZE_POLLFD * i + EVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return pollArray.getShort(offset);
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 getReventOps(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int offset = SIZE_POLLFD * i + REVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return pollArray.getShort(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_POLLFD * 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
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   165
    private void setUpdateEvents(int fd, byte events) {
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   166
        if (fd < MAX_UPDATE_ARRAY_SIZE) {
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   167
            eventsLow[fd] = events;
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   168
        } else {
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   169
            eventsHigh.put(Integer.valueOf(fd), Byte.valueOf(events));
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   170
        }
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   171
    }
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   172
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   173
    private byte getUpdateEvents(int fd) {
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   174
        if (fd < MAX_UPDATE_ARRAY_SIZE) {
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   175
            return eventsLow[fd];
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   176
        } else {
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   177
            Byte result = eventsHigh.get(Integer.valueOf(fd));
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   178
            // result should never be null
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   179
            return result.byteValue();
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   180
        }
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   181
    }
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   182
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    void setInterest(int fd, int mask) {
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   184
        synchronized (updateLock) {
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   185
            // record the file descriptor and events, expanding the
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   186
            // respective arrays first if necessary.
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   187
            int oldCapacity = updateDescriptors.length;
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   188
            if (updateCount == oldCapacity) {
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   189
                int newCapacity = oldCapacity + INITIAL_PENDING_UPDATE_SIZE;
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   190
                int[] newDescriptors = new int[newCapacity];
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   191
                System.arraycopy(updateDescriptors, 0, newDescriptors, 0, oldCapacity);
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   192
                updateDescriptors = newDescriptors;
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   193
            }
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   194
            updateDescriptors[updateCount++] = fd;
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   195
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   196
            // events are stored as bytes for efficiency reasons
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   197
            byte b = (byte)mask;
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   198
            assert (b == mask) && (b != IGNORE);
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   199
            setUpdateEvents(fd, b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    void release(int fd) {
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   204
        synchronized (updateLock) {
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   205
            // ignore any pending update for this file descriptor
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   206
            setUpdateEvents(fd, IGNORE);
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   207
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   208
            // remove from /dev/poll
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   209
            if (registered.get(fd)) {
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   210
                register(wfd, fd, POLLREMOVE);
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   211
                registered.clear(fd);
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   212
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   216
    void close() throws IOException {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   217
        FileDispatcherImpl.closeIntFD(wfd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
2444
282b8abf92eb 6824477: (se) Selector.select fails with IOException: "Invalid argument" if maximum file descriptors is low
alanb
parents: 2057
diff changeset
   221
    int poll(long timeout) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        updateRegistrations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        updated = poll0(pollArrayAddress, NUM_POLLFDS, timeout, wfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        for (int i=0; i<updated; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (getDescriptor(i) == incomingInterruptFD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                interruptedIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
2444
282b8abf92eb 6824477: (se) Selector.select fails with IOException: "Invalid argument" if maximum file descriptors is low
alanb
parents: 2057
diff changeset
   234
    void updateRegistrations() throws IOException {
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   235
        synchronized (updateLock) {
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   236
            // Populate pollfd array with updated masks
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   237
            int j = 0;
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   238
            int index = 0;
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   239
            while (j < updateCount) {
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   240
                int fd = updateDescriptors[j];
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   241
                short events = getUpdateEvents(fd);
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   242
                boolean wasRegistered = registered.get(fd);
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   243
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   244
                // events = 0 => POLLREMOVE or do-nothing
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   245
                if (events != IGNORE) {
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   246
                    if (events == 0) {
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   247
                        if (wasRegistered) {
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   248
                            events = POLLREMOVE;
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   249
                            registered.clear(fd);
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   250
                        } else {
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   251
                            events = IGNORE;
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   252
                        }
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   253
                    } else {
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   254
                        if (!wasRegistered) {
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   255
                            registered.set(fd);
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   256
                        }
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   257
                    }
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   258
                }
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   259
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   260
                // populate pollfd array with updated event
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   261
                if (events != IGNORE) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   262
                    // insert POLLREMOVE if changing events
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   263
                    if (wasRegistered && events != POLLREMOVE) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   264
                        putPollFD(pollArray, index, fd, POLLREMOVE);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   265
                        index++;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   266
                    }
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   267
                    putPollFD(pollArray, index, fd, events);
2444
282b8abf92eb 6824477: (se) Selector.select fails with IOException: "Invalid argument" if maximum file descriptors is low
alanb
parents: 2057
diff changeset
   268
                    index++;
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   269
                    if (index >= (NUM_POLLFDS-1)) {
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   270
                        registerMultiple(wfd, pollArray.address(), index);
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   271
                        index = 0;
2444
282b8abf92eb 6824477: (se) Selector.select fails with IOException: "Invalid argument" if maximum file descriptors is low
alanb
parents: 2057
diff changeset
   272
                    }
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   273
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   274
                    // events for this fd now up to date
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents: 12872
diff changeset
   275
                    setUpdateEvents(fd, IGNORE);
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   276
                }
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   277
                j++;
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   278
            }
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   279
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   280
            // write any remaining updates
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   281
            if (index > 0)
12868
8ab4f225fc94 7173515: (se) Selector.open fails with OOME on Solaris when unlimited file descriptors
alanb
parents: 12844
diff changeset
   282
                registerMultiple(wfd, pollArray.address(), index);
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   283
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   284
            updateCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private void putPollFD(AllocatedNativeObject array, int index, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                           short event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int structIndex = SIZE_POLLFD * index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        array.putInt(structIndex + FD_OFFSET, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        array.putShort(structIndex + EVENT_OFFSET, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        array.putShort(structIndex + REVENT_OFFSET, (short)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public void interrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        interrupt(outgoingInterruptFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public int interruptedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return interruptedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    boolean interrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return interrupted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    void clearInterrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private native int init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private native void register(int wfd, int fd, int mask);
2444
282b8abf92eb 6824477: (se) Selector.select fails with IOException: "Invalid argument" if maximum file descriptors is low
alanb
parents: 2057
diff changeset
   317
    private native void registerMultiple(int wfd, long address, int len)
282b8abf92eb 6824477: (se) Selector.select fails with IOException: "Invalid argument" if maximum file descriptors is low
alanb
parents: 2057
diff changeset
   318
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private native int poll0(long pollAddress, int numfds, long timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                             int wfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private static native void interrupt(int fd);
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   322
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   323
    static {
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   324
        IOUtil.load();
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   325
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
}