author | alanb |
Thu, 05 Apr 2018 15:04:09 +0100 | |
changeset 49527 | 5aa40f834b50 |
parent 49526 | cad4c844902a |
child 50602 | ed8de3d0cd28 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2002, 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 |
||
49493 | 28 |
import java.io.IOException; |
2 | 29 |
import java.nio.channels.ClosedSelectorException; |
30 |
import java.nio.channels.Pipe; |
|
49493 | 31 |
import java.nio.channels.Selector; |
32 |
import java.nio.channels.spi.SelectorProvider; |
|
33 |
import java.util.ArrayDeque; |
|
34 |
import java.util.ArrayList; |
|
35 |
import java.util.Deque; |
|
36 |
import java.util.HashMap; |
|
2 | 37 |
import java.util.List; |
49493 | 38 |
import java.util.Map; |
2 | 39 |
|
40 |
/** |
|
41 |
* A multi-threaded implementation of Selector for Windows. |
|
42 |
* |
|
43 |
* @author Konstantin Kladko |
|
44 |
* @author Mark Reinhold |
|
45 |
*/ |
|
46 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
47 |
class WindowsSelectorImpl extends SelectorImpl { |
2 | 48 |
// Initial capacity of the poll array |
49 |
private final int INIT_CAP = 8; |
|
50 |
// Maximum number of sockets for select(). |
|
51 |
// Should be INIT_CAP times a power of 2 |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
29920
diff
changeset
|
52 |
private static final int MAX_SELECTABLE_FDS = 1024; |
2 | 53 |
|
54 |
// The list of SelectableChannels serviced by this Selector. Every mod |
|
55 |
// MAX_SELECTABLE_FDS entry is bogus, to align this array with the poll |
|
56 |
// array, where the corresponding entry is occupied by the wakeupSocket |
|
57 |
private SelectionKeyImpl[] channelArray = new SelectionKeyImpl[INIT_CAP]; |
|
58 |
||
59 |
// The global native poll array holds file decriptors and event masks |
|
60 |
private PollArrayWrapper pollWrapper; |
|
61 |
||
62 |
// The number of valid entries in poll array, including entries occupied |
|
63 |
// by wakeup socket handle. |
|
64 |
private int totalChannels = 1; |
|
65 |
||
66 |
// Number of helper threads needed for select. We need one thread per |
|
67 |
// each additional set of MAX_SELECTABLE_FDS - 1 channels. |
|
68 |
private int threadsCount = 0; |
|
69 |
||
70 |
// A list of helper threads for select. |
|
2445
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
71 |
private final List<SelectThread> threads = new ArrayList<SelectThread>(); |
2 | 72 |
|
73 |
//Pipe used as a wakeup object. |
|
74 |
private final Pipe wakeupPipe; |
|
75 |
||
76 |
// File descriptors corresponding to source and sink |
|
77 |
private final int wakeupSourceFd, wakeupSinkFd; |
|
78 |
||
79 |
// Maps file descriptors to their indices in pollArray |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
29920
diff
changeset
|
80 |
private static final class FdMap extends HashMap<Integer, MapEntry> { |
895 | 81 |
static final long serialVersionUID = 0L; |
2 | 82 |
private MapEntry get(int desc) { |
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
34774
diff
changeset
|
83 |
return get(Integer.valueOf(desc)); |
2 | 84 |
} |
85 |
private MapEntry put(SelectionKeyImpl ski) { |
|
49526 | 86 |
return put(Integer.valueOf(ski.getFDVal()), new MapEntry(ski)); |
2 | 87 |
} |
88 |
private MapEntry remove(SelectionKeyImpl ski) { |
|
49526 | 89 |
Integer fd = Integer.valueOf(ski.getFDVal()); |
2 | 90 |
MapEntry x = get(fd); |
49526 | 91 |
if ((x != null) && (x.ski.channel() == ski.channel())) |
2 | 92 |
return remove(fd); |
93 |
return null; |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
// class for fdMap entries |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
29920
diff
changeset
|
98 |
private static final class MapEntry { |
49493 | 99 |
final SelectionKeyImpl ski; |
2 | 100 |
long updateCount = 0; |
101 |
MapEntry(SelectionKeyImpl ski) { |
|
102 |
this.ski = ski; |
|
103 |
} |
|
104 |
} |
|
105 |
private final FdMap fdMap = new FdMap(); |
|
106 |
||
107 |
// SubSelector for the main thread |
|
108 |
private final SubSelector subSelector = new SubSelector(); |
|
109 |
||
110 |
private long timeout; //timeout for poll |
|
111 |
||
112 |
// Lock for interrupt triggering and clearing |
|
113 |
private final Object interruptLock = new Object(); |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
34716
diff
changeset
|
114 |
private volatile boolean interruptTriggered; |
2 | 115 |
|
49526 | 116 |
// pending new registrations/updates, queued by implRegister and setEventOps |
49493 | 117 |
private final Object updateLock = new Object(); |
118 |
private final Deque<SelectionKeyImpl> newKeys = new ArrayDeque<>(); |
|
119 |
private final Deque<SelectionKeyImpl> updateKeys = new ArrayDeque<>(); |
|
120 |
||
121 |
||
2 | 122 |
WindowsSelectorImpl(SelectorProvider sp) throws IOException { |
123 |
super(sp); |
|
124 |
pollWrapper = new PollArrayWrapper(INIT_CAP); |
|
125 |
wakeupPipe = Pipe.open(); |
|
126 |
wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal(); |
|
127 |
||
128 |
// Disable the Nagle algorithm so that the wakeup is more immediate |
|
129 |
SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink(); |
|
130 |
(sink.sc).socket().setTcpNoDelay(true); |
|
131 |
wakeupSinkFd = ((SelChImpl)sink).getFDVal(); |
|
132 |
||
133 |
pollWrapper.addWakeupSocket(wakeupSourceFd, 0); |
|
134 |
} |
|
135 |
||
49493 | 136 |
private void ensureOpen() { |
137 |
if (!isOpen()) |
|
138 |
throw new ClosedSelectorException(); |
|
139 |
} |
|
140 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
141 |
@Override |
2 | 142 |
protected int doSelect(long timeout) throws IOException { |
49493 | 143 |
assert Thread.holdsLock(this); |
2 | 144 |
this.timeout = timeout; // set selector timeout |
49493 | 145 |
processUpdateQueue(); |
2 | 146 |
processDeregisterQueue(); |
147 |
if (interruptTriggered) { |
|
148 |
resetWakeupSocket(); |
|
149 |
return 0; |
|
150 |
} |
|
151 |
// Calculate number of helper threads needed for poll. If necessary |
|
152 |
// threads are created here and start waiting on startLock |
|
153 |
adjustThreadsCount(); |
|
154 |
finishLock.reset(); // reset finishLock |
|
155 |
// Wakeup helper threads, waiting on startLock, so they start polling. |
|
156 |
// Redundant threads will exit here after wakeup. |
|
157 |
startLock.startThreads(); |
|
158 |
// do polling in the main thread. Main thread is responsible for |
|
159 |
// first MAX_SELECTABLE_FDS entries in pollArray. |
|
160 |
try { |
|
161 |
begin(); |
|
162 |
try { |
|
163 |
subSelector.poll(); |
|
164 |
} catch (IOException e) { |
|
165 |
finishLock.setException(e); // Save this exception |
|
166 |
} |
|
167 |
// Main thread is out of poll(). Wakeup others and wait for them |
|
168 |
if (threads.size() > 0) |
|
169 |
finishLock.waitForHelperThreads(); |
|
170 |
} finally { |
|
171 |
end(); |
|
172 |
} |
|
173 |
// Done with poll(). Set wakeupSocket to nonsignaled for the next run. |
|
174 |
finishLock.checkForException(); |
|
175 |
processDeregisterQueue(); |
|
176 |
int updated = updateSelectedKeys(); |
|
177 |
// Done with poll(). Set wakeupSocket to nonsignaled for the next run. |
|
178 |
resetWakeupSocket(); |
|
179 |
return updated; |
|
180 |
} |
|
181 |
||
49493 | 182 |
/** |
183 |
* Process new registrations and changes to the interest ops. |
|
184 |
*/ |
|
185 |
private void processUpdateQueue() { |
|
186 |
assert Thread.holdsLock(this); |
|
187 |
||
188 |
synchronized (updateLock) { |
|
189 |
SelectionKeyImpl ski; |
|
190 |
||
191 |
// new registrations |
|
192 |
while ((ski = newKeys.pollFirst()) != null) { |
|
193 |
if (ski.isValid()) { |
|
194 |
growIfNeeded(); |
|
195 |
channelArray[totalChannels] = ski; |
|
196 |
ski.setIndex(totalChannels); |
|
197 |
pollWrapper.putEntry(totalChannels, ski); |
|
198 |
totalChannels++; |
|
199 |
MapEntry previous = fdMap.put(ski); |
|
200 |
assert previous == null; |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
// changes to interest ops |
|
205 |
while ((ski = updateKeys.pollFirst()) != null) { |
|
49526 | 206 |
int events = ski.translateInterestOps(); |
207 |
int fd = ski.getFDVal(); |
|
49493 | 208 |
if (ski.isValid() && fdMap.containsKey(fd)) { |
209 |
int index = ski.getIndex(); |
|
210 |
assert index >= 0 && index < totalChannels; |
|
211 |
pollWrapper.putEventOps(index, events); |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
||
2 | 217 |
// Helper threads wait on this lock for the next poll. |
218 |
private final StartLock startLock = new StartLock(); |
|
219 |
||
220 |
private final class StartLock { |
|
221 |
// A variable which distinguishes the current run of doSelect from the |
|
222 |
// previous one. Incrementing runsCounter and notifying threads will |
|
223 |
// trigger another round of poll. |
|
224 |
private long runsCounter; |
|
225 |
// Triggers threads, waiting on this lock to start polling. |
|
226 |
private synchronized void startThreads() { |
|
227 |
runsCounter++; // next run |
|
228 |
notifyAll(); // wake up threads. |
|
229 |
} |
|
230 |
// This function is called by a helper thread to wait for the |
|
231 |
// next round of poll(). It also checks, if this thread became |
|
232 |
// redundant. If yes, it returns true, notifying the thread |
|
233 |
// that it should exit. |
|
234 |
private synchronized boolean waitForStart(SelectThread thread) { |
|
235 |
while (true) { |
|
236 |
while (runsCounter == thread.lastRun) { |
|
237 |
try { |
|
238 |
startLock.wait(); |
|
239 |
} catch (InterruptedException e) { |
|
240 |
Thread.currentThread().interrupt(); |
|
241 |
} |
|
242 |
} |
|
2445
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
243 |
if (thread.isZombie()) { // redundant thread |
2 | 244 |
return true; // will cause run() to exit. |
245 |
} else { |
|
246 |
thread.lastRun = runsCounter; // update lastRun |
|
247 |
return false; // will cause run() to poll. |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
// Main thread waits on this lock, until all helper threads are done |
|
254 |
// with poll(). |
|
255 |
private final FinishLock finishLock = new FinishLock(); |
|
256 |
||
257 |
private final class FinishLock { |
|
258 |
// Number of helper threads, that did not finish yet. |
|
259 |
private int threadsToFinish; |
|
260 |
||
21278 | 261 |
// IOException which occurred during the last run. |
2 | 262 |
IOException exception = null; |
263 |
||
264 |
// Called before polling. |
|
265 |
private void reset() { |
|
266 |
threadsToFinish = threads.size(); // helper threads |
|
267 |
} |
|
268 |
||
269 |
// Each helper thread invokes this function on finishLock, when |
|
270 |
// the thread is done with poll(). |
|
271 |
private synchronized void threadFinished() { |
|
272 |
if (threadsToFinish == threads.size()) { // finished poll() first |
|
273 |
// if finished first, wakeup others |
|
274 |
wakeup(); |
|
275 |
} |
|
276 |
threadsToFinish--; |
|
277 |
if (threadsToFinish == 0) // all helper threads finished poll(). |
|
278 |
notify(); // notify the main thread |
|
279 |
} |
|
280 |
||
281 |
// The main thread invokes this function on finishLock to wait |
|
282 |
// for helper threads to finish poll(). |
|
283 |
private synchronized void waitForHelperThreads() { |
|
284 |
if (threadsToFinish == threads.size()) { |
|
285 |
// no helper threads finished yet. Wakeup them up. |
|
286 |
wakeup(); |
|
287 |
} |
|
288 |
while (threadsToFinish != 0) { |
|
289 |
try { |
|
290 |
finishLock.wait(); |
|
291 |
} catch (InterruptedException e) { |
|
292 |
// Interrupted - set interrupted state. |
|
293 |
Thread.currentThread().interrupt(); |
|
294 |
} |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
// sets IOException for this run |
|
299 |
private synchronized void setException(IOException e) { |
|
300 |
exception = e; |
|
301 |
} |
|
302 |
||
303 |
// Checks if there was any exception during the last run. |
|
304 |
// If yes, throws it |
|
305 |
private void checkForException() throws IOException { |
|
306 |
if (exception == null) |
|
307 |
return; |
|
21591 | 308 |
StringBuffer message = new StringBuffer("An exception occurred" + |
2 | 309 |
" during the execution of select(): \n"); |
310 |
message.append(exception); |
|
311 |
message.append('\n'); |
|
312 |
exception = null; |
|
313 |
throw new IOException(message.toString()); |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
private final class SubSelector { |
|
318 |
private final int pollArrayIndex; // starting index in pollArray to poll |
|
319 |
// These arrays will hold result of native select(). |
|
320 |
// The first element of each array is the number of selected sockets. |
|
321 |
// Other elements are file descriptors of selected sockets. |
|
322 |
private final int[] readFds = new int [MAX_SELECTABLE_FDS + 1]; |
|
323 |
private final int[] writeFds = new int [MAX_SELECTABLE_FDS + 1]; |
|
324 |
private final int[] exceptFds = new int [MAX_SELECTABLE_FDS + 1]; |
|
325 |
||
326 |
private SubSelector() { |
|
327 |
this.pollArrayIndex = 0; // main thread |
|
328 |
} |
|
329 |
||
330 |
private SubSelector(int threadIndex) { // helper threads |
|
331 |
this.pollArrayIndex = (threadIndex + 1) * MAX_SELECTABLE_FDS; |
|
332 |
} |
|
333 |
||
334 |
private int poll() throws IOException{ // poll for the main thread |
|
335 |
return poll0(pollWrapper.pollArrayAddress, |
|
336 |
Math.min(totalChannels, MAX_SELECTABLE_FDS), |
|
337 |
readFds, writeFds, exceptFds, timeout); |
|
338 |
} |
|
339 |
||
340 |
private int poll(int index) throws IOException { |
|
341 |
// poll for helper threads |
|
342 |
return poll0(pollWrapper.pollArrayAddress + |
|
343 |
(pollArrayIndex * PollArrayWrapper.SIZE_POLLFD), |
|
344 |
Math.min(MAX_SELECTABLE_FDS, |
|
345 |
totalChannels - (index + 1) * MAX_SELECTABLE_FDS), |
|
346 |
readFds, writeFds, exceptFds, timeout); |
|
347 |
} |
|
348 |
||
349 |
private native int poll0(long pollAddress, int numfds, |
|
350 |
int[] readFds, int[] writeFds, int[] exceptFds, long timeout); |
|
351 |
||
352 |
private int processSelectedKeys(long updateCount) { |
|
353 |
int numKeysUpdated = 0; |
|
354 |
numKeysUpdated += processFDSet(updateCount, readFds, |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
21591
diff
changeset
|
355 |
Net.POLLIN, |
5983
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
356 |
false); |
2 | 357 |
numKeysUpdated += processFDSet(updateCount, writeFds, |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
21591
diff
changeset
|
358 |
Net.POLLCONN | |
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
21591
diff
changeset
|
359 |
Net.POLLOUT, |
5983
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
360 |
false); |
2 | 361 |
numKeysUpdated += processFDSet(updateCount, exceptFds, |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
21591
diff
changeset
|
362 |
Net.POLLIN | |
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
21591
diff
changeset
|
363 |
Net.POLLCONN | |
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
21591
diff
changeset
|
364 |
Net.POLLOUT, |
5983
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
365 |
true); |
2 | 366 |
return numKeysUpdated; |
367 |
} |
|
368 |
||
369 |
/** |
|
49527
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
370 |
* updateCount is used to tell if a key has been counted as updated |
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
371 |
* in this select operation. |
2 | 372 |
* |
49527
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
373 |
* me.updateCount <= updateCount |
2 | 374 |
*/ |
5983
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
375 |
private int processFDSet(long updateCount, int[] fds, int rOps, |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
376 |
boolean isExceptFds) |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
377 |
{ |
2 | 378 |
int numKeysUpdated = 0; |
379 |
for (int i = 1; i <= fds[0]; i++) { |
|
380 |
int desc = fds[i]; |
|
381 |
if (desc == wakeupSourceFd) { |
|
382 |
synchronized (interruptLock) { |
|
383 |
interruptTriggered = true; |
|
384 |
} |
|
385 |
continue; |
|
386 |
} |
|
387 |
MapEntry me = fdMap.get(desc); |
|
388 |
// If me is null, the key was deregistered in the previous |
|
389 |
// processDeregisterQueue. |
|
390 |
if (me == null) |
|
391 |
continue; |
|
392 |
SelectionKeyImpl sk = me.ski; |
|
5983
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
393 |
|
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
394 |
// The descriptor may be in the exceptfds set because there is |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
395 |
// OOB data queued to the socket. If there is OOB data then it |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
396 |
// is discarded and the key is not added to the selected set. |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
397 |
if (isExceptFds && |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
398 |
(sk.channel() instanceof SocketChannelImpl) && |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
399 |
discardUrgentData(desc)) |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
400 |
{ |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
401 |
continue; |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
402 |
} |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
403 |
|
2 | 404 |
if (selectedKeys.contains(sk)) { // Key in selected set |
49527
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
405 |
if (sk.translateAndUpdateReadyOps(rOps)) { |
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
406 |
if (me.updateCount != updateCount) { |
2 | 407 |
me.updateCount = updateCount; |
408 |
numKeysUpdated++; |
|
409 |
} |
|
410 |
} |
|
411 |
} else { // Key is not in selected set yet |
|
49527
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
412 |
sk.translateAndSetReadyOps(rOps); |
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
413 |
if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0) { |
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
414 |
selectedKeys.add(sk); |
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
415 |
me.updateCount = updateCount; |
5aa40f834b50
8200458: (se) Readiness information previously recorded in the ready set not preserved
alanb
parents:
49526
diff
changeset
|
416 |
numKeysUpdated++; |
2 | 417 |
} |
418 |
} |
|
419 |
} |
|
420 |
return numKeysUpdated; |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
// Represents a helper thread used for select. |
|
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
32649
diff
changeset
|
425 |
private final class SelectThread extends Thread { |
2445
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
426 |
private final int index; // index of this thread |
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
427 |
final SubSelector subSelector; |
2 | 428 |
private long lastRun = 0; // last run number |
2445
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
429 |
private volatile boolean zombie; |
2 | 430 |
// Creates a new thread |
431 |
private SelectThread(int i) { |
|
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
32649
diff
changeset
|
432 |
super(null, null, "SelectorHelper", 0, false); |
2 | 433 |
this.index = i; |
434 |
this.subSelector = new SubSelector(i); |
|
435 |
//make sure we wait for next round of poll |
|
436 |
this.lastRun = startLock.runsCounter; |
|
437 |
} |
|
2445
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
438 |
void makeZombie() { |
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
439 |
zombie = true; |
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
440 |
} |
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
441 |
boolean isZombie() { |
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
442 |
return zombie; |
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
443 |
} |
2 | 444 |
public void run() { |
445 |
while (true) { // poll loop |
|
446 |
// wait for the start of poll. If this thread has become |
|
447 |
// redundant, then exit. |
|
448 |
if (startLock.waitForStart(this)) |
|
449 |
return; |
|
450 |
// call poll() |
|
451 |
try { |
|
452 |
subSelector.poll(index); |
|
453 |
} catch (IOException e) { |
|
454 |
// Save this exception and let other threads finish. |
|
455 |
finishLock.setException(e); |
|
456 |
} |
|
457 |
// notify main thread, that this thread has finished, and |
|
458 |
// wakeup others, if this thread is the first to finish. |
|
459 |
finishLock.threadFinished(); |
|
460 |
} |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
// After some channels registered/deregistered, the number of required |
|
465 |
// helper threads may have changed. Adjust this number. |
|
466 |
private void adjustThreadsCount() { |
|
467 |
if (threadsCount > threads.size()) { |
|
468 |
// More threads needed. Start more threads. |
|
469 |
for (int i = threads.size(); i < threadsCount; i++) { |
|
470 |
SelectThread newThread = new SelectThread(i); |
|
471 |
threads.add(newThread); |
|
472 |
newThread.setDaemon(true); |
|
473 |
newThread.start(); |
|
474 |
} |
|
475 |
} else if (threadsCount < threads.size()) { |
|
476 |
// Some threads become redundant. Remove them from the threads List. |
|
477 |
for (int i = threads.size() - 1 ; i >= threadsCount; i--) |
|
2445
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
478 |
threads.remove(i).makeZombie(); |
2 | 479 |
} |
480 |
} |
|
481 |
||
482 |
// Sets Windows wakeup socket to a signaled state. |
|
483 |
private void setWakeupSocket() { |
|
484 |
setWakeupSocket0(wakeupSinkFd); |
|
485 |
} |
|
486 |
private native void setWakeupSocket0(int wakeupSinkFd); |
|
487 |
||
488 |
// Sets Windows wakeup socket to a non-signaled state. |
|
489 |
private void resetWakeupSocket() { |
|
490 |
synchronized (interruptLock) { |
|
491 |
if (interruptTriggered == false) |
|
492 |
return; |
|
493 |
resetWakeupSocket0(wakeupSourceFd); |
|
494 |
interruptTriggered = false; |
|
495 |
} |
|
496 |
} |
|
497 |
||
498 |
private native void resetWakeupSocket0(int wakeupSourceFd); |
|
499 |
||
5983
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
500 |
private native boolean discardUrgentData(int fd); |
b5bc332cd233
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents:
5506
diff
changeset
|
501 |
|
2 | 502 |
// We increment this counter on each call to updateSelectedKeys() |
503 |
// each entry in SubSelector.fdsMap has a memorized value of |
|
504 |
// updateCount. When we increment numKeysUpdated we set updateCount |
|
505 |
// for the corresponding entry to its current value. This is used to |
|
506 |
// avoid counting the same key more than once - the same key can |
|
507 |
// appear in readfds and writefds. |
|
508 |
private long updateCount = 0; |
|
509 |
||
510 |
// Update ops of the corresponding Channels. Add the ready keys to the |
|
511 |
// ready queue. |
|
512 |
private int updateSelectedKeys() { |
|
513 |
updateCount++; |
|
514 |
int numKeysUpdated = 0; |
|
515 |
numKeysUpdated += subSelector.processSelectedKeys(updateCount); |
|
2445
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
516 |
for (SelectThread t: threads) { |
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
517 |
numKeysUpdated += t.subSelector.processSelectedKeys(updateCount); |
a1fa6863fc50
6823609: (se) Selector.select hangs on Windows under load
alanb
parents:
1639
diff
changeset
|
518 |
} |
2 | 519 |
return numKeysUpdated; |
520 |
} |
|
521 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
522 |
@Override |
2 | 523 |
protected void implClose() throws IOException { |
49493 | 524 |
assert !isOpen(); |
525 |
assert Thread.holdsLock(this); |
|
526 |
||
527 |
// prevent further wakeup |
|
528 |
synchronized (interruptLock) { |
|
529 |
interruptTriggered = true; |
|
2 | 530 |
} |
49493 | 531 |
|
532 |
wakeupPipe.sink().close(); |
|
533 |
wakeupPipe.source().close(); |
|
534 |
pollWrapper.free(); |
|
535 |
||
536 |
// Make all remaining helper threads exit |
|
537 |
for (SelectThread t: threads) |
|
538 |
t.makeZombie(); |
|
539 |
startLock.startThreads(); |
|
2 | 540 |
} |
541 |
||
49493 | 542 |
@Override |
2 | 543 |
protected void implRegister(SelectionKeyImpl ski) { |
49493 | 544 |
ensureOpen(); |
545 |
synchronized (updateLock) { |
|
546 |
newKeys.addLast(ski); |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
547 |
} |
2 | 548 |
} |
549 |
||
550 |
private void growIfNeeded() { |
|
551 |
if (channelArray.length == totalChannels) { |
|
552 |
int newSize = totalChannels * 2; // Make a larger array |
|
553 |
SelectionKeyImpl temp[] = new SelectionKeyImpl[newSize]; |
|
554 |
System.arraycopy(channelArray, 1, temp, 1, totalChannels - 1); |
|
555 |
channelArray = temp; |
|
556 |
pollWrapper.grow(newSize); |
|
557 |
} |
|
558 |
if (totalChannels % MAX_SELECTABLE_FDS == 0) { // more threads needed |
|
559 |
pollWrapper.addWakeupSocket(wakeupSourceFd, totalChannels); |
|
560 |
totalChannels++; |
|
561 |
threadsCount++; |
|
562 |
} |
|
563 |
} |
|
564 |
||
49493 | 565 |
@Override |
566 |
protected void implDereg(SelectionKeyImpl ski) { |
|
567 |
assert !ski.isValid(); |
|
568 |
assert Thread.holdsLock(this); |
|
569 |
||
570 |
if (fdMap.remove(ski) != null) { |
|
571 |
int i = ski.getIndex(); |
|
572 |
assert (i >= 0); |
|
573 |
||
16004
6dcf0b33fe6f
6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
14342
diff
changeset
|
574 |
if (i != totalChannels - 1) { |
6dcf0b33fe6f
6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
14342
diff
changeset
|
575 |
// Copy end one over it |
6dcf0b33fe6f
6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
14342
diff
changeset
|
576 |
SelectionKeyImpl endChannel = channelArray[totalChannels-1]; |
6dcf0b33fe6f
6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
14342
diff
changeset
|
577 |
channelArray[i] = endChannel; |
6dcf0b33fe6f
6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
14342
diff
changeset
|
578 |
endChannel.setIndex(i); |
49493 | 579 |
pollWrapper.replaceEntry(pollWrapper, totalChannels-1, pollWrapper, i); |
16004
6dcf0b33fe6f
6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
14342
diff
changeset
|
580 |
} |
6dcf0b33fe6f
6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
14342
diff
changeset
|
581 |
ski.setIndex(-1); |
49493 | 582 |
|
583 |
channelArray[totalChannels - 1] = null; |
|
2 | 584 |
totalChannels--; |
49493 | 585 |
if (totalChannels != 1 && totalChannels % MAX_SELECTABLE_FDS == 1) { |
586 |
totalChannels--; |
|
587 |
threadsCount--; // The last thread has become redundant. |
|
588 |
} |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
589 |
} |
2 | 590 |
} |
591 |
||
49493 | 592 |
@Override |
49526 | 593 |
public void setEventOps(SelectionKeyImpl ski) { |
49493 | 594 |
ensureOpen(); |
595 |
synchronized (updateLock) { |
|
596 |
updateKeys.addLast(ski); |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
@Override |
|
2 | 601 |
public Selector wakeup() { |
602 |
synchronized (interruptLock) { |
|
603 |
if (!interruptTriggered) { |
|
604 |
setWakeupSocket(); |
|
605 |
interruptTriggered = true; |
|
606 |
} |
|
607 |
} |
|
608 |
return this; |
|
609 |
} |
|
610 |
||
611 |
static { |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
16004
diff
changeset
|
612 |
IOUtil.load(); |
2 | 613 |
} |
614 |
} |