author | alanb |
Fri, 23 Mar 2018 14:18:18 +0000 | |
changeset 49290 | 07779973cbe2 |
parent 49248 | 15a0e60c8b97 |
child 49493 | 814bd31f8da0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 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; |
|
49290 | 29 |
import java.nio.channels.ClosedSelectorException; |
30 |
import java.nio.channels.SelectableChannel; |
|
31 |
import java.nio.channels.SelectionKey; |
|
32 |
import java.nio.channels.Selector; |
|
33 |
import java.nio.channels.spi.SelectorProvider; |
|
34 |
import java.util.ArrayDeque; |
|
35 |
import java.util.BitSet; |
|
36 |
import java.util.Deque; |
|
37 |
import java.util.HashMap; |
|
38 |
import java.util.Iterator; |
|
39 |
import java.util.Map; |
|
40 |
import java.util.concurrent.TimeUnit; |
|
41 |
||
42 |
import static sun.nio.ch.EPoll.EPOLLIN; |
|
43 |
import static sun.nio.ch.EPoll.EPOLL_CTL_ADD; |
|
44 |
import static sun.nio.ch.EPoll.EPOLL_CTL_DEL; |
|
45 |
import static sun.nio.ch.EPoll.EPOLL_CTL_MOD; |
|
46 |
||
2 | 47 |
|
48 |
/** |
|
49290 | 49 |
* Linux epoll based Selector implementation |
2 | 50 |
*/ |
49290 | 51 |
|
52 |
class EPollSelectorImpl extends SelectorImpl { |
|
53 |
||
54 |
// maximum number of events to poll in one call to epoll_wait |
|
55 |
private static final int NUM_EPOLLEVENTS = Math.min(IOUtil.fdLimit(), 1024); |
|
56 |
||
57 |
// epoll file descriptor |
|
58 |
private final int epfd; |
|
59 |
||
60 |
// address of poll array when polling with epoll_wait |
|
61 |
private final long pollArrayAddress; |
|
62 |
||
63 |
// file descriptors used for interrupt |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
64 |
private final int fd0; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
65 |
private final int fd1; |
2 | 66 |
|
49290 | 67 |
// maps file descriptor to selection key, synchronize on selector |
68 |
private final Map<Integer, SelectionKeyImpl> fdToKey = new HashMap<>(); |
|
2 | 69 |
|
49290 | 70 |
// file descriptors registered with epoll, synchronize on selector |
71 |
private final BitSet registered = new BitSet(); |
|
2 | 72 |
|
49290 | 73 |
// pending new registrations/updates, queued by implRegister and putEventOps |
74 |
private final Object updateLock = new Object(); |
|
75 |
private final Deque<SelectionKeyImpl> newKeys = new ArrayDeque<>(); |
|
76 |
private final Deque<SelectionKeyImpl> updateKeys = new ArrayDeque<>(); |
|
77 |
private final Deque<Integer> updateOps = new ArrayDeque<>(); |
|
2 | 78 |
|
49290 | 79 |
// interrupt triggering and clearing |
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
80 |
private final Object interruptLock = new Object(); |
49290 | 81 |
private boolean interruptTriggered; |
2 | 82 |
|
83 |
/** |
|
84 |
* Package private constructor called by factory method in |
|
85 |
* the abstract superclass Selector. |
|
86 |
*/ |
|
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
87 |
EPollSelectorImpl(SelectorProvider sp) throws IOException { |
2 | 88 |
super(sp); |
49290 | 89 |
|
90 |
this.epfd = EPoll.create(); |
|
91 |
this.pollArrayAddress = EPoll.allocatePollArray(NUM_EPOLLEVENTS); |
|
92 |
||
32856
c654cdf1d685
8138819: (se) File descriptor leak when Selector.open fails
igerasim
parents:
25859
diff
changeset
|
93 |
try { |
49290 | 94 |
long fds = IOUtil.makePipe(false); |
95 |
this.fd0 = (int) (fds >>> 32); |
|
96 |
this.fd1 = (int) fds; |
|
97 |
} catch (IOException ioe) { |
|
98 |
EPoll.freePollArray(pollArrayAddress); |
|
99 |
FileDispatcherImpl.closeIntFD(epfd); |
|
100 |
throw ioe; |
|
32856
c654cdf1d685
8138819: (se) File descriptor leak when Selector.open fails
igerasim
parents:
25859
diff
changeset
|
101 |
} |
49290 | 102 |
|
103 |
// register one end of the socket pair for wakeups |
|
104 |
EPoll.ctl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN); |
|
2 | 105 |
} |
106 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
107 |
private void ensureOpen() { |
49290 | 108 |
if (!isOpen()) |
2 | 109 |
throw new ClosedSelectorException(); |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
110 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
111 |
|
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
112 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
113 |
protected int doSelect(long timeout) throws IOException { |
49290 | 114 |
assert Thread.holdsLock(this); |
115 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
116 |
int numEntries; |
49290 | 117 |
processUpdateQueue(); |
2 | 118 |
processDeregisterQueue(); |
119 |
try { |
|
120 |
begin(); |
|
49290 | 121 |
|
122 |
// epoll_wait timeout is int |
|
123 |
int to = (int) Math.min(timeout, Integer.MAX_VALUE); |
|
124 |
boolean timedPoll = (to > 0); |
|
125 |
do { |
|
126 |
long startTime = timedPoll ? System.nanoTime() : 0; |
|
127 |
numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, to); |
|
128 |
if (numEntries == IOStatus.INTERRUPTED && timedPoll) { |
|
129 |
// timed poll interrupted so need to adjust timeout |
|
130 |
long adjust = System.nanoTime() - startTime; |
|
131 |
to -= TimeUnit.MILLISECONDS.convert(adjust, TimeUnit.NANOSECONDS); |
|
132 |
if (to <= 0) { |
|
133 |
// timeout expired so no retry |
|
134 |
numEntries = 0; |
|
135 |
} |
|
136 |
} |
|
137 |
} while (numEntries == IOStatus.INTERRUPTED); |
|
138 |
assert IOStatus.check(numEntries); |
|
139 |
||
2 | 140 |
} finally { |
141 |
end(); |
|
142 |
} |
|
143 |
processDeregisterQueue(); |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
144 |
return updateSelectedKeys(numEntries); |
2 | 145 |
} |
146 |
||
147 |
/** |
|
49290 | 148 |
* Process new registrations and changes to the interest ops. |
149 |
*/ |
|
150 |
private void processUpdateQueue() { |
|
151 |
assert Thread.holdsLock(this); |
|
152 |
||
153 |
synchronized (updateLock) { |
|
154 |
SelectionKeyImpl ski; |
|
155 |
||
156 |
// new registrations |
|
157 |
while ((ski = newKeys.pollFirst()) != null) { |
|
158 |
if (ski.isValid()) { |
|
159 |
SelChImpl ch = ski.channel; |
|
160 |
int fd = ch.getFDVal(); |
|
161 |
SelectionKeyImpl previous = fdToKey.put(fd, ski); |
|
162 |
assert previous == null; |
|
163 |
assert registered.get(fd) == false; |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
// changes to interest ops |
|
168 |
assert updateKeys.size() == updateOps.size(); |
|
169 |
while ((ski = updateKeys.pollFirst()) != null) { |
|
170 |
int ops = updateOps.pollFirst(); |
|
171 |
int fd = ski.channel.getFDVal(); |
|
172 |
if (ski.isValid() && fdToKey.containsKey(fd)) { |
|
173 |
if (registered.get(fd)) { |
|
174 |
if (ops == 0) { |
|
175 |
// remove from epoll |
|
176 |
EPoll.ctl(epfd, EPOLL_CTL_DEL, fd, 0); |
|
177 |
registered.clear(fd); |
|
178 |
} else { |
|
179 |
// modify events |
|
180 |
EPoll.ctl(epfd, EPOLL_CTL_MOD, fd, ops); |
|
181 |
} |
|
182 |
} else if (ops != 0) { |
|
183 |
// add to epoll |
|
184 |
EPoll.ctl(epfd, EPOLL_CTL_ADD, fd, ops); |
|
185 |
registered.set(fd); |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
/** |
|
2 | 193 |
* Update the keys whose fd's have been selected by the epoll. |
194 |
* Add the ready keys to the ready queue. |
|
195 |
*/ |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
196 |
private int updateSelectedKeys(int numEntries) throws IOException { |
49290 | 197 |
assert Thread.holdsLock(this); |
198 |
assert Thread.holdsLock(nioSelectedKeys()); |
|
199 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
200 |
boolean interrupted = false; |
2 | 201 |
int numKeysUpdated = 0; |
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 event = EPoll.getEvent(pollArrayAddress, i); |
204 |
int fd = EPoll.getDescriptor(event); |
|
205 |
if (fd == fd0) { |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
206 |
interrupted = true; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
207 |
} else { |
49290 | 208 |
SelectionKeyImpl ski = fdToKey.get(fd); |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
209 |
if (ski != null) { |
49290 | 210 |
int rOps = EPoll.getEvents(event); |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
211 |
if (selectedKeys.contains(ski)) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
212 |
if (ski.channel.translateAndSetReadyOps(rOps, ski)) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
213 |
numKeysUpdated++; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
214 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
215 |
} else { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
216 |
ski.channel.translateAndSetReadyOps(rOps, ski); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
217 |
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
218 |
selectedKeys.add(ski); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
219 |
numKeysUpdated++; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
220 |
} |
2 | 221 |
} |
222 |
} |
|
223 |
} |
|
224 |
} |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
225 |
|
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
226 |
if (interrupted) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
227 |
clearInterrupt(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
228 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
229 |
|
2 | 230 |
return numKeysUpdated; |
231 |
} |
|
232 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
233 |
@Override |
2 | 234 |
protected void implClose() throws IOException { |
49290 | 235 |
assert Thread.holdsLock(this); |
236 |
assert Thread.holdsLock(nioKeys()); |
|
2 | 237 |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
238 |
// prevent further wakeup |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
239 |
synchronized (interruptLock) { |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
240 |
interruptTriggered = true; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
241 |
} |
2 | 242 |
|
49290 | 243 |
FileDispatcherImpl.closeIntFD(epfd); |
244 |
EPoll.freePollArray(pollArrayAddress); |
|
245 |
||
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1639
diff
changeset
|
246 |
FileDispatcherImpl.closeIntFD(fd0); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1639
diff
changeset
|
247 |
FileDispatcherImpl.closeIntFD(fd1); |
2 | 248 |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
249 |
// Deregister channels |
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
250 |
Iterator<SelectionKey> i = keys.iterator(); |
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
251 |
while (i.hasNext()) { |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
252 |
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
253 |
deregister(ski); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
254 |
SelectableChannel selch = ski.channel(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
255 |
if (!selch.isOpen() && !selch.isRegistered()) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
256 |
((SelChImpl)selch).kill(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
257 |
i.remove(); |
2 | 258 |
} |
259 |
} |
|
260 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
261 |
@Override |
2 | 262 |
protected void implRegister(SelectionKeyImpl ski) { |
49290 | 263 |
assert Thread.holdsLock(nioKeys()); |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
264 |
ensureOpen(); |
49290 | 265 |
synchronized (updateLock) { |
266 |
newKeys.addLast(ski); |
|
267 |
} |
|
2 | 268 |
keys.add(ski); |
269 |
} |
|
270 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
271 |
@Override |
2 | 272 |
protected void implDereg(SelectionKeyImpl ski) throws IOException { |
49290 | 273 |
assert !ski.isValid(); |
274 |
assert Thread.holdsLock(this); |
|
275 |
assert Thread.holdsLock(nioKeys()); |
|
276 |
assert Thread.holdsLock(nioSelectedKeys()); |
|
277 |
||
278 |
int fd = ski.channel.getFDVal(); |
|
279 |
fdToKey.remove(fd); |
|
280 |
if (registered.get(fd)) { |
|
281 |
EPoll.ctl(epfd, EPOLL_CTL_DEL, fd, 0); |
|
282 |
registered.clear(fd); |
|
283 |
} |
|
284 |
||
285 |
selectedKeys.remove(ski); |
|
2 | 286 |
keys.remove(ski); |
49290 | 287 |
|
288 |
// remove from channel's key set |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
289 |
deregister(ski); |
49290 | 290 |
|
2 | 291 |
SelectableChannel selch = ski.channel(); |
292 |
if (!selch.isOpen() && !selch.isRegistered()) |
|
49290 | 293 |
((SelChImpl) selch).kill(); |
2 | 294 |
} |
295 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
296 |
@Override |
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
297 |
public void putEventOps(SelectionKeyImpl ski, int ops) { |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
298 |
ensureOpen(); |
49290 | 299 |
synchronized (updateLock) { |
300 |
updateOps.addLast(ops); // ops first in case adding the key fails |
|
301 |
updateKeys.addLast(ski); |
|
302 |
} |
|
2 | 303 |
} |
304 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
305 |
@Override |
2 | 306 |
public Selector wakeup() { |
307 |
synchronized (interruptLock) { |
|
308 |
if (!interruptTriggered) { |
|
49290 | 309 |
try { |
310 |
IOUtil.write1(fd1, (byte)0); |
|
311 |
} catch (IOException ioe) { |
|
312 |
throw new InternalError(ioe); |
|
313 |
} |
|
2 | 314 |
interruptTriggered = true; |
315 |
} |
|
316 |
} |
|
317 |
return this; |
|
318 |
} |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
319 |
|
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
320 |
private void clearInterrupt() throws IOException { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
321 |
synchronized (interruptLock) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
322 |
IOUtil.drain(fd0); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
323 |
interruptTriggered = false; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
324 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
325 |
} |
2 | 326 |
} |