author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 23010 | jdk/src/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java@6dadb192ad81 |
child 32856 | c654cdf1d685 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
19607
diff
changeset
|
2 |
* Copyright (c) 2011, 2013, 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 |
/* |
|
27 |
* KQueueSelectorImpl.java |
|
28 |
* Implementation of Selector using FreeBSD / Mac OS X kqueues |
|
29 |
* Derived from Sun's DevPollSelectorImpl |
|
30 |
*/ |
|
31 |
||
32 |
package sun.nio.ch; |
|
33 |
||
34 |
import java.io.IOException; |
|
35 |
import java.io.FileDescriptor; |
|
36 |
import java.nio.channels.*; |
|
37 |
import java.nio.channels.spi.*; |
|
38 |
import java.util.*; |
|
39 |
import sun.misc.*; |
|
40 |
||
41 |
class KQueueSelectorImpl |
|
42 |
extends SelectorImpl |
|
43 |
{ |
|
44 |
// File descriptors used for interrupt |
|
45 |
protected int fd0; |
|
46 |
protected int fd1; |
|
47 |
||
48 |
// The kqueue manipulator |
|
49 |
KQueueArrayWrapper kqueueWrapper; |
|
50 |
||
51 |
// Count of registered descriptors (including interrupt) |
|
52 |
private int totalChannels; |
|
53 |
||
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
54 |
// Map from a file descriptor to an entry containing the selection key |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
55 |
private HashMap<Integer,MapEntry> fdMap; |
12047 | 56 |
|
57 |
// True if this Selector has been closed |
|
58 |
private boolean closed = false; |
|
59 |
||
60 |
// Lock for interrupt triggering and clearing |
|
61 |
private Object interruptLock = new Object(); |
|
62 |
private boolean interruptTriggered = false; |
|
63 |
||
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
64 |
// 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
|
65 |
// descriptor is polled by more than one filter |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
66 |
private long updateCount; |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
67 |
|
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
68 |
// Used to map file descriptors to a selection key and "update count" |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
69 |
// (see updateSelectedKeys for usage). |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
70 |
private static class MapEntry { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
71 |
SelectionKeyImpl ski; |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
72 |
long updateCount; |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
73 |
MapEntry(SelectionKeyImpl ski) { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
74 |
this.ski = ski; |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
75 |
} |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
76 |
} |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
77 |
|
12047 | 78 |
/** |
79 |
* Package private constructor called by factory method in |
|
80 |
* the abstract superclass Selector. |
|
81 |
*/ |
|
82 |
KQueueSelectorImpl(SelectorProvider sp) { |
|
83 |
super(sp); |
|
84 |
long fds = IOUtil.makePipe(false); |
|
85 |
fd0 = (int)(fds >>> 32); |
|
86 |
fd1 = (int)fds; |
|
87 |
kqueueWrapper = new KQueueArrayWrapper(); |
|
88 |
kqueueWrapper.initInterrupt(fd0, fd1); |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
89 |
fdMap = new HashMap<>(); |
12047 | 90 |
totalChannels = 1; |
91 |
} |
|
92 |
||
93 |
||
94 |
protected int doSelect(long timeout) |
|
95 |
throws IOException |
|
96 |
{ |
|
97 |
int entries = 0; |
|
98 |
if (closed) |
|
99 |
throw new ClosedSelectorException(); |
|
100 |
processDeregisterQueue(); |
|
101 |
try { |
|
102 |
begin(); |
|
103 |
entries = kqueueWrapper.poll(timeout); |
|
104 |
} finally { |
|
105 |
end(); |
|
106 |
} |
|
107 |
processDeregisterQueue(); |
|
108 |
return updateSelectedKeys(entries); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
112 |
* Update the keys whose fd's have been selected by kqueue. |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
113 |
* Add the ready keys to the selected key set. |
12047 | 114 |
* If the interrupt fd has been selected, drain it and clear the interrupt. |
115 |
*/ |
|
116 |
private int updateSelectedKeys(int entries) |
|
117 |
throws IOException |
|
118 |
{ |
|
119 |
int numKeysUpdated = 0; |
|
120 |
boolean interrupted = false; |
|
121 |
||
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
122 |
// A file descriptor may be registered with kqueue with more than one |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
123 |
// filter and so there may be more than one event for a fd. The update |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
124 |
// count in the MapEntry tracks when the fd was last updated and this |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
125 |
// ensures that the ready ops are updated rather than replaced by a |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
126 |
// second or subsequent event. |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
127 |
updateCount++; |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
128 |
|
12047 | 129 |
for (int i = 0; i < entries; i++) { |
130 |
int nextFD = kqueueWrapper.getDescriptor(i); |
|
131 |
if (nextFD == fd0) { |
|
132 |
interrupted = true; |
|
133 |
} else { |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
134 |
MapEntry me = fdMap.get(Integer.valueOf(nextFD)); |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
135 |
|
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
136 |
// entry is null in the case of an interrupt |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
137 |
if (me != null) { |
12047 | 138 |
int rOps = kqueueWrapper.getReventOps(i); |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
139 |
SelectionKeyImpl ski = me.ski; |
12047 | 140 |
if (selectedKeys.contains(ski)) { |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
141 |
// first time this file descriptor has been encountered on this |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
142 |
// update? |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
143 |
if (me.updateCount != updateCount) { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
144 |
if (ski.channel.translateAndSetReadyOps(rOps, ski)) { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
145 |
numKeysUpdated++; |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
146 |
me.updateCount = updateCount; |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
147 |
} |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
148 |
} else { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
149 |
// 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
|
150 |
ski.channel.translateAndUpdateReadyOps(rOps, ski); |
12047 | 151 |
} |
152 |
} else { |
|
153 |
ski.channel.translateAndSetReadyOps(rOps, ski); |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
154 |
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) { |
12047 | 155 |
selectedKeys.add(ski); |
156 |
numKeysUpdated++; |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
157 |
me.updateCount = updateCount; |
12047 | 158 |
} |
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
if (interrupted) { |
|
165 |
// Clear the wakeup pipe |
|
166 |
synchronized (interruptLock) { |
|
167 |
IOUtil.drain(fd0); |
|
168 |
interruptTriggered = false; |
|
169 |
} |
|
170 |
} |
|
171 |
return numKeysUpdated; |
|
172 |
} |
|
173 |
||
174 |
||
175 |
protected void implClose() throws IOException { |
|
176 |
if (!closed) { |
|
177 |
closed = true; |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
178 |
|
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
179 |
// prevent further wakeup |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
180 |
synchronized (interruptLock) { |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
181 |
interruptTriggered = true; |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
182 |
} |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
183 |
|
12047 | 184 |
FileDispatcherImpl.closeIntFD(fd0); |
185 |
FileDispatcherImpl.closeIntFD(fd1); |
|
186 |
if (kqueueWrapper != null) { |
|
187 |
kqueueWrapper.close(); |
|
188 |
kqueueWrapper = null; |
|
189 |
selectedKeys = null; |
|
190 |
||
191 |
// Deregister channels |
|
192 |
Iterator<SelectionKey> i = keys.iterator(); |
|
193 |
while (i.hasNext()) { |
|
194 |
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); |
|
195 |
deregister(ski); |
|
196 |
SelectableChannel selch = ski.channel(); |
|
197 |
if (!selch.isOpen() && !selch.isRegistered()) |
|
198 |
((SelChImpl)selch).kill(); |
|
199 |
i.remove(); |
|
200 |
} |
|
201 |
totalChannels = 0; |
|
202 |
} |
|
203 |
fd0 = -1; |
|
204 |
fd1 = -1; |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
||
209 |
protected void implRegister(SelectionKeyImpl ski) { |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
210 |
if (closed) |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
211 |
throw new ClosedSelectorException(); |
12047 | 212 |
int fd = IOUtil.fdVal(ski.channel.getFD()); |
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
213 |
fdMap.put(Integer.valueOf(fd), new MapEntry(ski)); |
12047 | 214 |
totalChannels++; |
215 |
keys.add(ski); |
|
216 |
} |
|
217 |
||
218 |
||
219 |
protected void implDereg(SelectionKeyImpl ski) throws IOException { |
|
220 |
int fd = ski.channel.getFDVal(); |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
221 |
fdMap.remove(Integer.valueOf(fd)); |
13585
4e446fa0c1c0
7191587: (se) SelectionKey.interestOps does not defer changing the interest set to the next select [macosx]
alanb
parents:
12438
diff
changeset
|
222 |
kqueueWrapper.release(ski.channel); |
12047 | 223 |
totalChannels--; |
224 |
keys.remove(ski); |
|
225 |
selectedKeys.remove(ski); |
|
226 |
deregister((AbstractSelectionKey)ski); |
|
227 |
SelectableChannel selch = ski.channel(); |
|
228 |
if (!selch.isOpen() && !selch.isRegistered()) |
|
229 |
((SelChImpl)selch).kill(); |
|
230 |
} |
|
231 |
||
232 |
||
233 |
public void putEventOps(SelectionKeyImpl ski, int ops) { |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
234 |
if (closed) |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
235 |
throw new ClosedSelectorException(); |
13585
4e446fa0c1c0
7191587: (se) SelectionKey.interestOps does not defer changing the interest set to the next select [macosx]
alanb
parents:
12438
diff
changeset
|
236 |
kqueueWrapper.setInterest(ski.channel, ops); |
12047 | 237 |
} |
238 |
||
239 |
||
240 |
public Selector wakeup() { |
|
241 |
synchronized (interruptLock) { |
|
242 |
if (!interruptTriggered) { |
|
243 |
kqueueWrapper.interrupt(); |
|
244 |
interruptTriggered = true; |
|
245 |
} |
|
246 |
} |
|
247 |
return this; |
|
248 |
} |
|
249 |
} |