author | alanb |
Thu, 05 Apr 2018 15:01:57 +0100 | |
changeset 49526 | cad4c844902a |
parent 49493 | 814bd31f8da0 |
child 49527 | 5aa40f834b50 |
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 |
||
49526 | 65 |
// pending new registrations/updates, queued by setEventOps |
49290 | 66 |
private final Object updateLock = new Object(); |
67 |
private final Deque<SelectionKeyImpl> updateKeys = new ArrayDeque<>(); |
|
12047 | 68 |
|
49290 | 69 |
// interrupt triggering and clearing |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
70 |
private final Object interruptLock = new Object(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
71 |
private boolean interruptTriggered; |
12047 | 72 |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
73 |
// 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
|
74 |
// descriptor is polled by more than one filter |
49290 | 75 |
private int pollCount; |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
76 |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
77 |
KQueueSelectorImpl(SelectorProvider sp) throws IOException { |
12047 | 78 |
super(sp); |
49290 | 79 |
|
80 |
this.kqfd = KQueue.create(); |
|
81 |
this.pollArrayAddress = KQueue.allocatePollArray(MAX_KEVENTS); |
|
82 |
||
32856
c654cdf1d685
8138819: (se) File descriptor leak when Selector.open fails
igerasim
parents:
25859
diff
changeset
|
83 |
try { |
49290 | 84 |
long fds = IOUtil.makePipe(false); |
85 |
this.fd0 = (int) (fds >>> 32); |
|
86 |
this.fd1 = (int) fds; |
|
87 |
} catch (IOException ioe) { |
|
88 |
KQueue.freePollArray(pollArrayAddress); |
|
89 |
FileDispatcherImpl.closeIntFD(kqfd); |
|
90 |
throw ioe; |
|
32856
c654cdf1d685
8138819: (se) File descriptor leak when Selector.open fails
igerasim
parents:
25859
diff
changeset
|
91 |
} |
49290 | 92 |
|
93 |
// register one end of the socket pair for wakeups |
|
94 |
KQueue.register(kqfd, fd0, EVFILT_READ, EV_ADD); |
|
12047 | 95 |
} |
96 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
97 |
private void ensureOpen() { |
49290 | 98 |
if (!isOpen()) |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
99 |
throw new ClosedSelectorException(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
100 |
} |
12047 | 101 |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
102 |
@Override |
49290 | 103 |
protected int doSelect(long timeout) throws IOException { |
104 |
assert Thread.holdsLock(this); |
|
105 |
||
49493 | 106 |
long to = Math.min(timeout, Integer.MAX_VALUE); // max kqueue timeout |
107 |
boolean blocking = (to != 0); |
|
108 |
boolean timedPoll = (to > 0); |
|
109 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
110 |
int numEntries; |
49290 | 111 |
processUpdateQueue(); |
12047 | 112 |
processDeregisterQueue(); |
113 |
try { |
|
49493 | 114 |
begin(blocking); |
49290 | 115 |
|
116 |
do { |
|
117 |
long startTime = timedPoll ? System.nanoTime() : 0; |
|
118 |
numEntries = KQueue.poll(kqfd, pollArrayAddress, MAX_KEVENTS, to); |
|
119 |
if (numEntries == IOStatus.INTERRUPTED && timedPoll) { |
|
120 |
// timed poll interrupted so need to adjust timeout |
|
121 |
long adjust = System.nanoTime() - startTime; |
|
122 |
to -= TimeUnit.MILLISECONDS.convert(adjust, TimeUnit.NANOSECONDS); |
|
123 |
if (to <= 0) { |
|
124 |
// timeout expired so no retry |
|
125 |
numEntries = 0; |
|
126 |
} |
|
127 |
} |
|
128 |
} while (numEntries == IOStatus.INTERRUPTED); |
|
129 |
assert IOStatus.check(numEntries); |
|
130 |
||
12047 | 131 |
} finally { |
49493 | 132 |
end(blocking); |
12047 | 133 |
} |
134 |
processDeregisterQueue(); |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
135 |
return updateSelectedKeys(numEntries); |
12047 | 136 |
} |
137 |
||
138 |
/** |
|
49526 | 139 |
* Process changes to the interest ops. |
49290 | 140 |
*/ |
141 |
private void processUpdateQueue() { |
|
142 |
assert Thread.holdsLock(this); |
|
143 |
||
144 |
synchronized (updateLock) { |
|
145 |
SelectionKeyImpl ski; |
|
49526 | 146 |
while ((ski = updateKeys.pollFirst()) != null) { |
49290 | 147 |
if (ski.isValid()) { |
49526 | 148 |
int fd = ski.getFDVal(); |
149 |
// add to fdToKey if needed |
|
150 |
SelectionKeyImpl previous = fdToKey.putIfAbsent(fd, ski); |
|
151 |
assert (previous == null) || (previous == ski); |
|
49290 | 152 |
|
49526 | 153 |
int newEvents = ski.translateInterestOps(); |
49493 | 154 |
int registeredEvents = ski.registeredEvents(); |
155 |
if (newEvents != registeredEvents) { |
|
156 |
||
157 |
// add or delete interest in read events |
|
158 |
if ((registeredEvents & Net.POLLIN) != 0) { |
|
159 |
if ((newEvents & Net.POLLIN) == 0) { |
|
160 |
KQueue.register(kqfd, fd, EVFILT_READ, EV_DELETE); |
|
161 |
} |
|
162 |
} else if ((newEvents & Net.POLLIN) != 0) { |
|
163 |
KQueue.register(kqfd, fd, EVFILT_READ, EV_ADD); |
|
49290 | 164 |
} |
165 |
||
49493 | 166 |
// add or delete interest in write events |
167 |
if ((registeredEvents & Net.POLLOUT) != 0) { |
|
168 |
if ((newEvents & Net.POLLOUT) == 0) { |
|
169 |
KQueue.register(kqfd, fd, EVFILT_WRITE, EV_DELETE); |
|
170 |
} |
|
171 |
} else if ((newEvents & Net.POLLOUT) != 0) { |
|
172 |
KQueue.register(kqfd, fd, EVFILT_WRITE, EV_ADD); |
|
49290 | 173 |
} |
49493 | 174 |
|
175 |
ski.registeredEvents(newEvents); |
|
49290 | 176 |
} |
177 |
} |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
/** |
|
49493 | 183 |
* Update the keys of file descriptors that were polled and add them to |
184 |
* the selected-key set. |
|
12047 | 185 |
* If the interrupt fd has been selected, drain it and clear the interrupt. |
186 |
*/ |
|
49290 | 187 |
private int updateSelectedKeys(int numEntries) throws IOException { |
188 |
assert Thread.holdsLock(this); |
|
189 |
assert Thread.holdsLock(nioSelectedKeys()); |
|
190 |
||
12047 | 191 |
int numKeysUpdated = 0; |
192 |
boolean interrupted = false; |
|
193 |
||
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
194 |
// A file descriptor may be registered with kqueue with more than one |
49290 | 195 |
// filter and so there may be more than one event for a fd. The poll |
196 |
// count is incremented here and compared against the SelectionKey's |
|
197 |
// "lastPolled" field. This ensures that the ready ops is updated rather |
|
198 |
// than replaced when a file descriptor is polled by both the read and |
|
199 |
// write filter. |
|
200 |
pollCount++; |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
201 |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
202 |
for (int i = 0; i < numEntries; i++) { |
49290 | 203 |
long kevent = KQueue.getEvent(pollArrayAddress, i); |
204 |
int fd = KQueue.getDescriptor(kevent); |
|
205 |
if (fd == fd0) { |
|
12047 | 206 |
interrupted = true; |
207 |
} else { |
|
49290 | 208 |
SelectionKeyImpl ski = fdToKey.get(fd); |
209 |
if (ski != null) { |
|
210 |
int rOps = 0; |
|
211 |
short filter = KQueue.getFilter(kevent); |
|
212 |
if (filter == EVFILT_READ) { |
|
213 |
rOps |= Net.POLLIN; |
|
214 |
} else if (filter == EVFILT_WRITE) { |
|
215 |
rOps |= Net.POLLOUT; |
|
216 |
} |
|
217 |
||
12047 | 218 |
if (selectedKeys.contains(ski)) { |
49290 | 219 |
// file descriptor may be polled more than once per poll |
220 |
if (ski.lastPolled != pollCount) { |
|
49526 | 221 |
if (ski.translateAndSetReadyOps(rOps)) { |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
222 |
numKeysUpdated++; |
49290 | 223 |
ski.lastPolled = pollCount; |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
224 |
} |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
225 |
} else { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
226 |
// ready ops have already been set on this update |
49526 | 227 |
ski.translateAndUpdateReadyOps(rOps); |
12047 | 228 |
} |
229 |
} else { |
|
49526 | 230 |
ski.translateAndSetReadyOps(rOps); |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
231 |
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) { |
12047 | 232 |
selectedKeys.add(ski); |
233 |
numKeysUpdated++; |
|
49290 | 234 |
ski.lastPolled = pollCount; |
12047 | 235 |
} |
236 |
} |
|
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
if (interrupted) { |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
242 |
clearInterrupt(); |
12047 | 243 |
} |
244 |
return numKeysUpdated; |
|
245 |
} |
|
246 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
247 |
@Override |
12047 | 248 |
protected void implClose() throws IOException { |
49290 | 249 |
assert !isOpen(); |
250 |
assert Thread.holdsLock(this); |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
251 |
|
49290 | 252 |
// prevent further wakeup |
253 |
synchronized (interruptLock) { |
|
254 |
interruptTriggered = true; |
|
255 |
} |
|
256 |
||
257 |
FileDispatcherImpl.closeIntFD(kqfd); |
|
258 |
KQueue.freePollArray(pollArrayAddress); |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
259 |
|
49290 | 260 |
FileDispatcherImpl.closeIntFD(fd0); |
261 |
FileDispatcherImpl.closeIntFD(fd1); |
|
12047 | 262 |
} |
263 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
264 |
@Override |
12047 | 265 |
protected void implDereg(SelectionKeyImpl ski) throws IOException { |
49290 | 266 |
assert !ski.isValid(); |
267 |
assert Thread.holdsLock(this); |
|
268 |
||
49526 | 269 |
int fd = ski.getFDVal(); |
49493 | 270 |
int registeredEvents = ski.registeredEvents(); |
271 |
if (fdToKey.remove(fd) != null) { |
|
272 |
if (registeredEvents != 0) { |
|
273 |
if ((registeredEvents & Net.POLLIN) != 0) |
|
274 |
KQueue.register(kqfd, fd, EVFILT_READ, EV_DELETE); |
|
275 |
if ((registeredEvents & Net.POLLOUT) != 0) |
|
276 |
KQueue.register(kqfd, fd, EVFILT_WRITE, EV_DELETE); |
|
277 |
ski.registeredEvents(0); |
|
278 |
} |
|
279 |
} else { |
|
280 |
assert registeredEvents == 0; |
|
49290 | 281 |
} |
12047 | 282 |
} |
283 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
284 |
@Override |
49526 | 285 |
public void setEventOps(SelectionKeyImpl ski) { |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
286 |
ensureOpen(); |
49290 | 287 |
synchronized (updateLock) { |
288 |
updateKeys.addLast(ski); |
|
289 |
} |
|
12047 | 290 |
} |
291 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
292 |
@Override |
12047 | 293 |
public Selector wakeup() { |
294 |
synchronized (interruptLock) { |
|
295 |
if (!interruptTriggered) { |
|
49290 | 296 |
try { |
297 |
IOUtil.write1(fd1, (byte)0); |
|
298 |
} catch (IOException ioe) { |
|
299 |
throw new InternalError(ioe); |
|
300 |
} |
|
12047 | 301 |
interruptTriggered = true; |
302 |
} |
|
303 |
} |
|
304 |
return this; |
|
305 |
} |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
306 |
|
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
307 |
private void clearInterrupt() throws IOException { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
308 |
synchronized (interruptLock) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
309 |
IOUtil.drain(fd0); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
310 |
interruptTriggered = false; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
311 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
312 |
} |
12047 | 313 |
} |