author | alanb |
Wed, 28 Aug 2013 15:50:03 +0100 | |
changeset 19607 | bee007586d06 |
parent 16474 | 93db93a49848 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
11823
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, 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; |
|
29 |
import java.nio.channels.*; |
|
30 |
import java.nio.channels.spi.*; |
|
31 |
import java.util.*; |
|
32 |
import sun.misc.*; |
|
33 |
||
34 |
/** |
|
35 |
* An implementation of Selector for Linux 2.6+ kernels that uses |
|
36 |
* the epoll event notification facility. |
|
37 |
*/ |
|
38 |
class EPollSelectorImpl |
|
39 |
extends SelectorImpl |
|
40 |
{ |
|
41 |
||
42 |
// File descriptors used for interrupt |
|
43 |
protected int fd0; |
|
44 |
protected int fd1; |
|
45 |
||
46 |
// The poll object |
|
47 |
EPollArrayWrapper pollWrapper; |
|
48 |
||
49 |
// Maps from file descriptors to keys |
|
895 | 50 |
private Map<Integer,SelectionKeyImpl> fdToKey; |
2 | 51 |
|
52 |
// True if this Selector has been closed |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
53 |
private volatile boolean closed = false; |
2 | 54 |
|
55 |
// Lock for 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
|
56 |
private final Object interruptLock = new Object(); |
2 | 57 |
private boolean interruptTriggered = false; |
58 |
||
59 |
/** |
|
60 |
* Package private constructor called by factory method in |
|
61 |
* the abstract superclass Selector. |
|
62 |
*/ |
|
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
63 |
EPollSelectorImpl(SelectorProvider sp) throws IOException { |
2 | 64 |
super(sp); |
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
65 |
long pipeFds = IOUtil.makePipe(false); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
66 |
fd0 = (int) (pipeFds >>> 32); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
67 |
fd1 = (int) pipeFds; |
2 | 68 |
pollWrapper = new EPollArrayWrapper(); |
69 |
pollWrapper.initInterrupt(fd0, fd1); |
|
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
70 |
fdToKey = new HashMap<>(); |
2 | 71 |
} |
72 |
||
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
73 |
protected int doSelect(long timeout) throws IOException { |
2 | 74 |
if (closed) |
75 |
throw new ClosedSelectorException(); |
|
76 |
processDeregisterQueue(); |
|
77 |
try { |
|
78 |
begin(); |
|
79 |
pollWrapper.poll(timeout); |
|
80 |
} finally { |
|
81 |
end(); |
|
82 |
} |
|
83 |
processDeregisterQueue(); |
|
84 |
int numKeysUpdated = updateSelectedKeys(); |
|
85 |
if (pollWrapper.interrupted()) { |
|
86 |
// Clear the wakeup pipe |
|
87 |
pollWrapper.putEventOps(pollWrapper.interruptedIndex(), 0); |
|
88 |
synchronized (interruptLock) { |
|
89 |
pollWrapper.clearInterrupted(); |
|
90 |
IOUtil.drain(fd0); |
|
91 |
interruptTriggered = false; |
|
92 |
} |
|
93 |
} |
|
94 |
return numKeysUpdated; |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Update the keys whose fd's have been selected by the epoll. |
|
99 |
* Add the ready keys to the ready queue. |
|
100 |
*/ |
|
101 |
private int updateSelectedKeys() { |
|
102 |
int entries = pollWrapper.updated; |
|
103 |
int numKeysUpdated = 0; |
|
104 |
for (int i=0; i<entries; i++) { |
|
105 |
int nextFD = pollWrapper.getDescriptor(i); |
|
895 | 106 |
SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD)); |
2 | 107 |
// ski is null in the case of an interrupt |
108 |
if (ski != null) { |
|
109 |
int rOps = pollWrapper.getEventOps(i); |
|
110 |
if (selectedKeys.contains(ski)) { |
|
111 |
if (ski.channel.translateAndSetReadyOps(rOps, ski)) { |
|
112 |
numKeysUpdated++; |
|
113 |
} |
|
114 |
} else { |
|
115 |
ski.channel.translateAndSetReadyOps(rOps, ski); |
|
116 |
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) { |
|
117 |
selectedKeys.add(ski); |
|
118 |
numKeysUpdated++; |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
return numKeysUpdated; |
|
124 |
} |
|
125 |
||
126 |
protected void implClose() throws IOException { |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
127 |
if (closed) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
128 |
return; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
129 |
closed = true; |
2 | 130 |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
131 |
// prevent further wakeup |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
132 |
synchronized (interruptLock) { |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
133 |
interruptTriggered = true; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
134 |
} |
2 | 135 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1639
diff
changeset
|
136 |
FileDispatcherImpl.closeIntFD(fd0); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1639
diff
changeset
|
137 |
FileDispatcherImpl.closeIntFD(fd1); |
2 | 138 |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
139 |
pollWrapper.closeEPollFD(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
140 |
// it is possible |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
141 |
selectedKeys = null; |
2 | 142 |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
143 |
// Deregister channels |
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
144 |
Iterator<SelectionKey> i = keys.iterator(); |
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
145 |
while (i.hasNext()) { |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
146 |
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
147 |
deregister(ski); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
148 |
SelectableChannel selch = ski.channel(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
149 |
if (!selch.isOpen() && !selch.isRegistered()) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
150 |
((SelChImpl)selch).kill(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
151 |
i.remove(); |
2 | 152 |
} |
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
153 |
|
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
154 |
fd0 = -1; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
155 |
fd1 = -1; |
2 | 156 |
} |
157 |
||
158 |
protected void implRegister(SelectionKeyImpl ski) { |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
159 |
if (closed) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
160 |
throw new ClosedSelectorException(); |
2433
9f0ab7f726b8
6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
2057
diff
changeset
|
161 |
SelChImpl ch = ski.channel; |
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
162 |
int fd = Integer.valueOf(ch.getFDVal()); |
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
163 |
fdToKey.put(fd, ski); |
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
164 |
pollWrapper.add(fd); |
2 | 165 |
keys.add(ski); |
166 |
} |
|
167 |
||
168 |
protected void implDereg(SelectionKeyImpl ski) throws IOException { |
|
169 |
assert (ski.getIndex() >= 0); |
|
2433
9f0ab7f726b8
6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
2057
diff
changeset
|
170 |
SelChImpl ch = ski.channel; |
9f0ab7f726b8
6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
2057
diff
changeset
|
171 |
int fd = ch.getFDVal(); |
895 | 172 |
fdToKey.remove(Integer.valueOf(fd)); |
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
173 |
pollWrapper.remove(fd); |
2 | 174 |
ski.setIndex(-1); |
175 |
keys.remove(ski); |
|
176 |
selectedKeys.remove(ski); |
|
177 |
deregister((AbstractSelectionKey)ski); |
|
178 |
SelectableChannel selch = ski.channel(); |
|
179 |
if (!selch.isOpen() && !selch.isRegistered()) |
|
180 |
((SelChImpl)selch).kill(); |
|
181 |
} |
|
182 |
||
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
183 |
public void putEventOps(SelectionKeyImpl ski, int ops) { |
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
184 |
if (closed) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
1247
diff
changeset
|
185 |
throw new ClosedSelectorException(); |
16474
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
186 |
SelChImpl ch = ski.channel; |
93db93a49848
8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents:
14342
diff
changeset
|
187 |
pollWrapper.setInterest(ch.getFDVal(), ops); |
2 | 188 |
} |
189 |
||
190 |
public Selector wakeup() { |
|
191 |
synchronized (interruptLock) { |
|
192 |
if (!interruptTriggered) { |
|
193 |
pollWrapper.interrupt(); |
|
194 |
interruptTriggered = true; |
|
195 |
} |
|
196 |
} |
|
197 |
return this; |
|
198 |
} |
|
199 |
} |