author | alanb |
Thu, 15 Mar 2018 10:47:58 +0000 | |
changeset 49248 | 15a0e60c8b97 |
parent 47216 | 71c04702a3d5 |
child 49290 | 07779973cbe2 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
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 | 34 |
|
35 |
||
36 |
/** |
|
37 |
* Manipulates a native array of pollfd structs on Solaris: |
|
38 |
* |
|
39 |
* typedef struct pollfd { |
|
40 |
* int fd; |
|
41 |
* short events; |
|
42 |
* short revents; |
|
43 |
* } pollfd_t; |
|
44 |
* |
|
45 |
* @author Mike McCloskey |
|
46 |
* @since 1.4 |
|
47 |
*/ |
|
48 |
||
49 |
class DevPollArrayWrapper { |
|
50 |
||
51 |
// Event masks |
|
52 |
static final short POLLIN = 0x0001; |
|
53 |
static final short POLLPRI = 0x0002; |
|
54 |
static final short POLLOUT = 0x0004; |
|
55 |
static final short POLLRDNORM = 0x0040; |
|
56 |
static final short POLLWRNORM = POLLOUT; |
|
57 |
static final short POLLRDBAND = 0x0080; |
|
58 |
static final short POLLWRBAND = 0x0100; |
|
59 |
static final short POLLNORM = POLLRDNORM; |
|
60 |
static final short POLLERR = 0x0008; |
|
61 |
static final short POLLHUP = 0x0010; |
|
62 |
static final short POLLNVAL = 0x0020; |
|
63 |
static final short POLLREMOVE = 0x0800; |
|
64 |
static final short POLLCONN = POLLOUT; |
|
65 |
||
66 |
// Miscellaneous constants |
|
67 |
static final short SIZE_POLLFD = 8; |
|
68 |
static final short FD_OFFSET = 0; |
|
69 |
static final short EVENT_OFFSET = 4; |
|
70 |
static final short REVENT_OFFSET = 6; |
|
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 | 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 | 77 |
|
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 | 80 |
static final int NUM_POLLFDS = Math.min(OPEN_MAX-1, 8192); |
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 | 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 | 88 |
|
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 | 94 |
|
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 | 97 |
|
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 | 100 |
|
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 | 103 |
|
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 | 106 |
|
107 |
// Number of updated pollfd entries |
|
108 |
int updated; |
|
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 | 139 |
void initInterrupt(int fd0, int fd1) { |
140 |
outgoingInterruptFD = fd1; |
|
141 |
incomingInterruptFD = fd0; |
|
142 |
register(wfd, fd0, POLLIN); |
|
143 |
} |
|
144 |
||
145 |
void putReventOps(int i, int revent) { |
|
146 |
int offset = SIZE_POLLFD * i + REVENT_OFFSET; |
|
147 |
pollArray.putShort(offset, (short)revent); |
|
148 |
} |
|
149 |
||
150 |
int getEventOps(int i) { |
|
151 |
int offset = SIZE_POLLFD * i + EVENT_OFFSET; |
|
152 |
return pollArray.getShort(offset); |
|
153 |
} |
|
154 |
||
155 |
int getReventOps(int i) { |
|
156 |
int offset = SIZE_POLLFD * i + REVENT_OFFSET; |
|
157 |
return pollArray.getShort(offset); |
|
158 |
} |
|
159 |
||
160 |
int getDescriptor(int i) { |
|
161 |
int offset = SIZE_POLLFD * i + FD_OFFSET; |
|
162 |
return pollArray.getInt(offset); |
|
163 |
} |
|
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 | 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 | 200 |
} |
201 |
} |
|
202 |
||
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 | 213 |
} |
214 |
} |
|
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 | 218 |
pollArray.free(); |
219 |
} |
|
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 | 222 |
updateRegistrations(); |
223 |
updated = poll0(pollArrayAddress, NUM_POLLFDS, timeout, wfd); |
|
224 |
for (int i=0; i<updated; i++) { |
|
225 |
if (getDescriptor(i) == incomingInterruptFD) { |
|
226 |
interruptedIndex = i; |
|
227 |
interrupted = true; |
|
228 |
break; |
|
229 |
} |
|
230 |
} |
|
231 |
return updated; |
|
232 |
} |
|
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 | 285 |
} |
286 |
} |
|
287 |
||
288 |
private void putPollFD(AllocatedNativeObject array, int index, int fd, |
|
289 |
short event) |
|
290 |
{ |
|
291 |
int structIndex = SIZE_POLLFD * index; |
|
292 |
array.putInt(structIndex + FD_OFFSET, fd); |
|
293 |
array.putShort(structIndex + EVENT_OFFSET, event); |
|
294 |
array.putShort(structIndex + REVENT_OFFSET, (short)0); |
|
295 |
} |
|
296 |
||
297 |
boolean interrupted = false; |
|
298 |
||
299 |
public void interrupt() { |
|
300 |
interrupt(outgoingInterruptFD); |
|
301 |
} |
|
302 |
||
303 |
public int interruptedIndex() { |
|
304 |
return interruptedIndex; |
|
305 |
} |
|
306 |
||
307 |
boolean interrupted() { |
|
308 |
return interrupted; |
|
309 |
} |
|
310 |
||
311 |
void clearInterrupted() { |
|
312 |
interrupted = false; |
|
313 |
} |
|
314 |
||
315 |
private native int init(); |
|
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 | 319 |
private native int poll0(long pollAddress, int numfds, long timeout, |
320 |
int wfd); |
|
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 | 326 |
} |