author | alanb |
Fri, 30 Mar 2018 08:28:09 +0100 | |
changeset 49493 | 814bd31f8da0 |
parent 49290 | 07779973cbe2 |
child 49526 | cad4c844902a |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. |
12047 | 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 |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
12047 | 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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.io.IOException; |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
29 |
import java.nio.channels.ClosedSelectorException; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
30 |
import java.nio.channels.Selector; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
31 |
import java.nio.channels.spi.SelectorProvider; |
49290 | 32 |
import java.util.ArrayDeque; |
33 |
import java.util.Deque; |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
34 |
import java.util.HashMap; |
49290 | 35 |
import java.util.Map; |
36 |
import java.util.concurrent.TimeUnit; |
|
12047 | 37 |
|
49290 | 38 |
import static sun.nio.ch.KQueue.EVFILT_READ; |
39 |
import static sun.nio.ch.KQueue.EVFILT_WRITE; |
|
40 |
import static sun.nio.ch.KQueue.EV_ADD; |
|
41 |
import static sun.nio.ch.KQueue.EV_DELETE; |
|
42 |
||
43 |
/** |
|
44 |
* KQueue based Selector implementation for macOS |
|
45 |
*/ |
|
46 |
||
47 |
class KQueueSelectorImpl extends SelectorImpl { |
|
48 |
||
49 |
// maximum number of events to poll in one call to kqueue |
|
50 |
private static final int MAX_KEVENTS = 256; |
|
51 |
||
52 |
// kqueue file descriptor |
|
53 |
private final int kqfd; |
|
54 |
||
55 |
// address of poll array (event list) when polling for pending events |
|
56 |
private final long pollArrayAddress; |
|
57 |
||
58 |
// file descriptors used for interrupt |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
59 |
private final int fd0; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
60 |
private final int fd1; |
12047 | 61 |
|
49290 | 62 |
// maps file descriptor to selection key, synchronize on selector |
63 |
private final Map<Integer, SelectionKeyImpl> fdToKey = new HashMap<>(); |
|
64 |
||
65 |
// pending new registrations/updates, queued by implRegister and putEventOps |
|
66 |
private final Object updateLock = new Object(); |
|
67 |
private final Deque<SelectionKeyImpl> newKeys = new ArrayDeque<>(); |
|
68 |
private final Deque<SelectionKeyImpl> updateKeys = new ArrayDeque<>(); |
|
49493 | 69 |
private final Deque<Integer> updateEvents = new ArrayDeque<>(); |
12047 | 70 |
|
49290 | 71 |
// interrupt triggering and clearing |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
72 |
private final Object interruptLock = new Object(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
73 |
private boolean interruptTriggered; |
12047 | 74 |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
75 |
// used by updateSelectedKeys to handle cases where the same file |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
76 |
// descriptor is polled by more than one filter |
49290 | 77 |
private int pollCount; |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
78 |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
79 |
KQueueSelectorImpl(SelectorProvider sp) throws IOException { |
12047 | 80 |
super(sp); |
49290 | 81 |
|
82 |
this.kqfd = KQueue.create(); |
|
83 |
this.pollArrayAddress = KQueue.allocatePollArray(MAX_KEVENTS); |
|
84 |
||
32856
c654cdf1d685
8138819: (se) File descriptor leak when Selector.open fails
igerasim
parents:
25859
diff
changeset
|
85 |
try { |
49290 | 86 |
long fds = IOUtil.makePipe(false); |
87 |
this.fd0 = (int) (fds >>> 32); |
|
88 |
this.fd1 = (int) fds; |
|
89 |
} catch (IOException ioe) { |
|
90 |
KQueue.freePollArray(pollArrayAddress); |
|
91 |
FileDispatcherImpl.closeIntFD(kqfd); |
|
92 |
throw ioe; |
|
32856
c654cdf1d685
8138819: (se) File descriptor leak when Selector.open fails
igerasim
parents:
25859
diff
changeset
|
93 |
} |
49290 | 94 |
|
95 |
// register one end of the socket pair for wakeups |
|
96 |
KQueue.register(kqfd, fd0, EVFILT_READ, EV_ADD); |
|
12047 | 97 |
} |
98 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
99 |
private void ensureOpen() { |
49290 | 100 |
if (!isOpen()) |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
101 |
throw new ClosedSelectorException(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
102 |
} |
12047 | 103 |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
104 |
@Override |
49290 | 105 |
protected int doSelect(long timeout) throws IOException { |
106 |
assert Thread.holdsLock(this); |
|
107 |
||
49493 | 108 |
long to = Math.min(timeout, Integer.MAX_VALUE); // max kqueue timeout |
109 |
boolean blocking = (to != 0); |
|
110 |
boolean timedPoll = (to > 0); |
|
111 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
112 |
int numEntries; |
49290 | 113 |
processUpdateQueue(); |
12047 | 114 |
processDeregisterQueue(); |
115 |
try { |
|
49493 | 116 |
begin(blocking); |
49290 | 117 |
|
118 |
do { |
|
119 |
long startTime = timedPoll ? System.nanoTime() : 0; |
|
120 |
numEntries = KQueue.poll(kqfd, pollArrayAddress, MAX_KEVENTS, to); |
|
121 |
if (numEntries == IOStatus.INTERRUPTED && timedPoll) { |
|
122 |
// timed poll interrupted so need to adjust timeout |
|
123 |
long adjust = System.nanoTime() - startTime; |
|
124 |
to -= TimeUnit.MILLISECONDS.convert(adjust, TimeUnit.NANOSECONDS); |
|
125 |
if (to <= 0) { |
|
126 |
// timeout expired so no retry |
|
127 |
numEntries = 0; |
|
128 |
} |
|
129 |
} |
|
130 |
} while (numEntries == IOStatus.INTERRUPTED); |
|
131 |
assert IOStatus.check(numEntries); |
|
132 |
||
12047 | 133 |
} finally { |
49493 | 134 |
end(blocking); |
12047 | 135 |
} |
136 |
processDeregisterQueue(); |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
137 |
return updateSelectedKeys(numEntries); |
12047 | 138 |
} |
139 |
||
140 |
/** |
|
49290 | 141 |
* Process new registrations and changes to the interest ops. |
142 |
*/ |
|
143 |
private void processUpdateQueue() { |
|
144 |
assert Thread.holdsLock(this); |
|
145 |
||
146 |
synchronized (updateLock) { |
|
147 |
SelectionKeyImpl ski; |
|
148 |
||
149 |
// new registrations |
|
150 |
while ((ski = newKeys.pollFirst()) != null) { |
|
151 |
if (ski.isValid()) { |
|
49493 | 152 |
int fd = ski.channel.getFDVal(); |
49290 | 153 |
SelectionKeyImpl previous = fdToKey.put(fd, ski); |
154 |
assert previous == null; |
|
49493 | 155 |
assert ski.registeredEvents() == 0; |
49290 | 156 |
} |
157 |
} |
|
158 |
||
159 |
// changes to interest ops |
|
49493 | 160 |
assert updateKeys.size() == updateKeys.size(); |
49290 | 161 |
while ((ski = updateKeys.pollFirst()) != null) { |
49493 | 162 |
int newEvents = updateEvents.pollFirst(); |
49290 | 163 |
int fd = ski.channel.getFDVal(); |
164 |
if (ski.isValid() && fdToKey.containsKey(fd)) { |
|
49493 | 165 |
int registeredEvents = ski.registeredEvents(); |
166 |
if (newEvents != registeredEvents) { |
|
167 |
||
168 |
// add or delete interest in read events |
|
169 |
if ((registeredEvents & Net.POLLIN) != 0) { |
|
170 |
if ((newEvents & Net.POLLIN) == 0) { |
|
171 |
KQueue.register(kqfd, fd, EVFILT_READ, EV_DELETE); |
|
172 |
} |
|
173 |
} else if ((newEvents & Net.POLLIN) != 0) { |
|
174 |
KQueue.register(kqfd, fd, EVFILT_READ, EV_ADD); |
|
49290 | 175 |
} |
176 |
||
49493 | 177 |
// add or delete interest in write events |
178 |
if ((registeredEvents & Net.POLLOUT) != 0) { |
|
179 |
if ((newEvents & Net.POLLOUT) == 0) { |
|
180 |
KQueue.register(kqfd, fd, EVFILT_WRITE, EV_DELETE); |
|
181 |
} |
|
182 |
} else if ((newEvents & Net.POLLOUT) != 0) { |
|
183 |
KQueue.register(kqfd, fd, EVFILT_WRITE, EV_ADD); |
|
49290 | 184 |
} |
49493 | 185 |
|
186 |
ski.registeredEvents(newEvents); |
|
49290 | 187 |
} |
188 |
} |
|
189 |
} |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
/** |
|
49493 | 194 |
* Update the keys of file descriptors that were polled and add them to |
195 |
* the selected-key set. |
|
12047 | 196 |
* If the interrupt fd has been selected, drain it and clear the interrupt. |
197 |
*/ |
|
49290 | 198 |
private int updateSelectedKeys(int numEntries) throws IOException { |
199 |
assert Thread.holdsLock(this); |
|
200 |
assert Thread.holdsLock(nioSelectedKeys()); |
|
201 |
||
12047 | 202 |
int numKeysUpdated = 0; |
203 |
boolean interrupted = false; |
|
204 |
||
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
205 |
// A file descriptor may be registered with kqueue with more than one |
49290 | 206 |
// filter and so there may be more than one event for a fd. The poll |
207 |
// count is incremented here and compared against the SelectionKey's |
|
208 |
// "lastPolled" field. This ensures that the ready ops is updated rather |
|
209 |
// than replaced when a file descriptor is polled by both the read and |
|
210 |
// write filter. |
|
211 |
pollCount++; |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
212 |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
213 |
for (int i = 0; i < numEntries; i++) { |
49290 | 214 |
long kevent = KQueue.getEvent(pollArrayAddress, i); |
215 |
int fd = KQueue.getDescriptor(kevent); |
|
216 |
if (fd == fd0) { |
|
12047 | 217 |
interrupted = true; |
218 |
} else { |
|
49290 | 219 |
SelectionKeyImpl ski = fdToKey.get(fd); |
220 |
if (ski != null) { |
|
221 |
int rOps = 0; |
|
222 |
short filter = KQueue.getFilter(kevent); |
|
223 |
if (filter == EVFILT_READ) { |
|
224 |
rOps |= Net.POLLIN; |
|
225 |
} else if (filter == EVFILT_WRITE) { |
|
226 |
rOps |= Net.POLLOUT; |
|
227 |
} |
|
228 |
||
12047 | 229 |
if (selectedKeys.contains(ski)) { |
49290 | 230 |
// file descriptor may be polled more than once per poll |
231 |
if (ski.lastPolled != pollCount) { |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
232 |
if (ski.channel.translateAndSetReadyOps(rOps, ski)) { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
233 |
numKeysUpdated++; |
49290 | 234 |
ski.lastPolled = pollCount; |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
235 |
} |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
236 |
} else { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
237 |
// ready ops have already been set on this update |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
238 |
ski.channel.translateAndUpdateReadyOps(rOps, ski); |
12047 | 239 |
} |
240 |
} else { |
|
241 |
ski.channel.translateAndSetReadyOps(rOps, ski); |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
242 |
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) { |
12047 | 243 |
selectedKeys.add(ski); |
244 |
numKeysUpdated++; |
|
49290 | 245 |
ski.lastPolled = pollCount; |
12047 | 246 |
} |
247 |
} |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
if (interrupted) { |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
253 |
clearInterrupt(); |
12047 | 254 |
} |
255 |
return numKeysUpdated; |
|
256 |
} |
|
257 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
258 |
@Override |
12047 | 259 |
protected void implClose() throws IOException { |
49290 | 260 |
assert !isOpen(); |
261 |
assert Thread.holdsLock(this); |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
262 |
|
49290 | 263 |
// prevent further wakeup |
264 |
synchronized (interruptLock) { |
|
265 |
interruptTriggered = true; |
|
266 |
} |
|
267 |
||
268 |
FileDispatcherImpl.closeIntFD(kqfd); |
|
269 |
KQueue.freePollArray(pollArrayAddress); |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
270 |
|
49290 | 271 |
FileDispatcherImpl.closeIntFD(fd0); |
272 |
FileDispatcherImpl.closeIntFD(fd1); |
|
12047 | 273 |
} |
274 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
275 |
@Override |
12047 | 276 |
protected void implRegister(SelectionKeyImpl ski) { |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
277 |
ensureOpen(); |
49290 | 278 |
synchronized (updateLock) { |
279 |
newKeys.addLast(ski); |
|
280 |
} |
|
12047 | 281 |
} |
282 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
283 |
@Override |
12047 | 284 |
protected void implDereg(SelectionKeyImpl ski) throws IOException { |
49290 | 285 |
assert !ski.isValid(); |
286 |
assert Thread.holdsLock(this); |
|
287 |
||
12047 | 288 |
int fd = ski.channel.getFDVal(); |
49493 | 289 |
int registeredEvents = ski.registeredEvents(); |
290 |
if (fdToKey.remove(fd) != null) { |
|
291 |
if (registeredEvents != 0) { |
|
292 |
if ((registeredEvents & Net.POLLIN) != 0) |
|
293 |
KQueue.register(kqfd, fd, EVFILT_READ, EV_DELETE); |
|
294 |
if ((registeredEvents & Net.POLLOUT) != 0) |
|
295 |
KQueue.register(kqfd, fd, EVFILT_WRITE, EV_DELETE); |
|
296 |
ski.registeredEvents(0); |
|
297 |
} |
|
298 |
} else { |
|
299 |
assert registeredEvents == 0; |
|
49290 | 300 |
} |
12047 | 301 |
} |
302 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
303 |
@Override |
49493 | 304 |
public void putEventOps(SelectionKeyImpl ski, int events) { |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
305 |
ensureOpen(); |
49290 | 306 |
synchronized (updateLock) { |
49493 | 307 |
updateEvents.addLast(events); // events first in case adding key fails |
49290 | 308 |
updateKeys.addLast(ski); |
309 |
} |
|
12047 | 310 |
} |
311 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
312 |
@Override |
12047 | 313 |
public Selector wakeup() { |
314 |
synchronized (interruptLock) { |
|
315 |
if (!interruptTriggered) { |
|
49290 | 316 |
try { |
317 |
IOUtil.write1(fd1, (byte)0); |
|
318 |
} catch (IOException ioe) { |
|
319 |
throw new InternalError(ioe); |
|
320 |
} |
|
12047 | 321 |
interruptTriggered = true; |
322 |
} |
|
323 |
} |
|
324 |
return this; |
|
325 |
} |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
326 |
|
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
327 |
private void clearInterrupt() throws IOException { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
328 |
synchronized (interruptLock) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
329 |
IOUtil.drain(fd0); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
330 |
interruptTriggered = false; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
331 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
332 |
} |
12047 | 333 |
} |