12047
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* KQueueSelectorImpl.java
|
|
26 |
* Implementation of Selector using FreeBSD / Mac OS X kqueues
|
|
27 |
* Derived from Sun's DevPollSelectorImpl
|
|
28 |
*/
|
|
29 |
|
|
30 |
package sun.nio.ch;
|
|
31 |
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.FileDescriptor;
|
|
34 |
import java.nio.channels.*;
|
|
35 |
import java.nio.channels.spi.*;
|
|
36 |
import java.util.*;
|
|
37 |
import sun.misc.*;
|
|
38 |
|
|
39 |
class KQueueSelectorImpl
|
|
40 |
extends SelectorImpl
|
|
41 |
{
|
|
42 |
// File descriptors used for interrupt
|
|
43 |
protected int fd0;
|
|
44 |
protected int fd1;
|
|
45 |
|
|
46 |
// The kqueue manipulator
|
|
47 |
KQueueArrayWrapper kqueueWrapper;
|
|
48 |
|
|
49 |
// Count of registered descriptors (including interrupt)
|
|
50 |
private int totalChannels;
|
|
51 |
|
|
52 |
// Map from file descriptors to selection keys
|
|
53 |
private HashMap<Integer,SelectionKeyImpl> fdToKey;
|
|
54 |
|
|
55 |
// True if this Selector has been closed
|
|
56 |
private boolean closed = false;
|
|
57 |
|
|
58 |
// Lock for interrupt triggering and clearing
|
|
59 |
private Object interruptLock = new Object();
|
|
60 |
private boolean interruptTriggered = false;
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Package private constructor called by factory method in
|
|
64 |
* the abstract superclass Selector.
|
|
65 |
*/
|
|
66 |
KQueueSelectorImpl(SelectorProvider sp) {
|
|
67 |
super(sp);
|
|
68 |
long fds = IOUtil.makePipe(false);
|
|
69 |
fd0 = (int)(fds >>> 32);
|
|
70 |
fd1 = (int)fds;
|
|
71 |
kqueueWrapper = new KQueueArrayWrapper();
|
|
72 |
kqueueWrapper.initInterrupt(fd0, fd1);
|
|
73 |
fdToKey = new HashMap<>();
|
|
74 |
totalChannels = 1;
|
|
75 |
}
|
|
76 |
|
|
77 |
|
|
78 |
protected int doSelect(long timeout)
|
|
79 |
throws IOException
|
|
80 |
{
|
|
81 |
int entries = 0;
|
|
82 |
if (closed)
|
|
83 |
throw new ClosedSelectorException();
|
|
84 |
processDeregisterQueue();
|
|
85 |
if (timeout == 0 && totalChannels == 1)
|
|
86 |
return 0;
|
|
87 |
try {
|
|
88 |
begin();
|
|
89 |
entries = kqueueWrapper.poll(timeout);
|
|
90 |
} finally {
|
|
91 |
end();
|
|
92 |
}
|
|
93 |
processDeregisterQueue();
|
|
94 |
return updateSelectedKeys(entries);
|
|
95 |
}
|
|
96 |
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Update the keys whose fd's have been selected by the devpoll
|
|
100 |
* driver. Add the ready keys to the ready queue.
|
|
101 |
* If the interrupt fd has been selected, drain it and clear the interrupt.
|
|
102 |
*/
|
|
103 |
private int updateSelectedKeys(int entries)
|
|
104 |
throws IOException
|
|
105 |
{
|
|
106 |
int numKeysUpdated = 0;
|
|
107 |
boolean interrupted = false;
|
|
108 |
|
|
109 |
for (int i = 0; i < entries; i++) {
|
|
110 |
int nextFD = kqueueWrapper.getDescriptor(i);
|
|
111 |
if (nextFD == fd0) {
|
|
112 |
interrupted = true;
|
|
113 |
} else {
|
|
114 |
SelectionKeyImpl ski = fdToKey.get(new Integer(nextFD));
|
|
115 |
// ski is null in the case of an interrupt
|
|
116 |
if (ski != null) {
|
|
117 |
int rOps = kqueueWrapper.getReventOps(i);
|
|
118 |
if (selectedKeys.contains(ski)) {
|
|
119 |
if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
|
|
120 |
numKeysUpdated++;
|
|
121 |
}
|
|
122 |
} else {
|
|
123 |
ski.channel.translateAndSetReadyOps(rOps, ski);
|
|
124 |
if ((ski.readyOps() & ski.interestOps()) != 0) {
|
|
125 |
selectedKeys.add(ski);
|
|
126 |
numKeysUpdated++;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
if (interrupted) {
|
|
134 |
// Clear the wakeup pipe
|
|
135 |
synchronized (interruptLock) {
|
|
136 |
IOUtil.drain(fd0);
|
|
137 |
interruptTriggered = false;
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
return numKeysUpdated;
|
|
142 |
}
|
|
143 |
|
|
144 |
|
|
145 |
protected void implClose() throws IOException {
|
|
146 |
if (!closed) {
|
|
147 |
closed = true;
|
|
148 |
FileDispatcherImpl.closeIntFD(fd0);
|
|
149 |
FileDispatcherImpl.closeIntFD(fd1);
|
|
150 |
if (kqueueWrapper != null) {
|
|
151 |
kqueueWrapper.release(fd0);
|
|
152 |
kqueueWrapper.close();
|
|
153 |
kqueueWrapper = null;
|
|
154 |
selectedKeys = null;
|
|
155 |
|
|
156 |
// Deregister channels
|
|
157 |
Iterator<SelectionKey> i = keys.iterator();
|
|
158 |
while (i.hasNext()) {
|
|
159 |
SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
|
|
160 |
deregister(ski);
|
|
161 |
SelectableChannel selch = ski.channel();
|
|
162 |
if (!selch.isOpen() && !selch.isRegistered())
|
|
163 |
((SelChImpl)selch).kill();
|
|
164 |
i.remove();
|
|
165 |
}
|
|
166 |
totalChannels = 0;
|
|
167 |
}
|
|
168 |
fd0 = -1;
|
|
169 |
fd1 = -1;
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
|
|
174 |
protected void implRegister(SelectionKeyImpl ski) {
|
|
175 |
int fd = IOUtil.fdVal(ski.channel.getFD());
|
|
176 |
fdToKey.put(new Integer(fd), ski);
|
|
177 |
totalChannels++;
|
|
178 |
keys.add(ski);
|
|
179 |
}
|
|
180 |
|
|
181 |
|
|
182 |
protected void implDereg(SelectionKeyImpl ski) throws IOException {
|
|
183 |
int fd = ski.channel.getFDVal();
|
|
184 |
fdToKey.remove(new Integer(fd));
|
|
185 |
kqueueWrapper.release(fd);
|
|
186 |
totalChannels--;
|
|
187 |
keys.remove(ski);
|
|
188 |
selectedKeys.remove(ski);
|
|
189 |
deregister((AbstractSelectionKey)ski);
|
|
190 |
SelectableChannel selch = ski.channel();
|
|
191 |
if (!selch.isOpen() && !selch.isRegistered())
|
|
192 |
((SelChImpl)selch).kill();
|
|
193 |
}
|
|
194 |
|
|
195 |
|
|
196 |
public void putEventOps(SelectionKeyImpl ski, int ops) {
|
|
197 |
int fd = IOUtil.fdVal(ski.channel.getFD());
|
|
198 |
kqueueWrapper.setInterest(fd, ops);
|
|
199 |
}
|
|
200 |
|
|
201 |
|
|
202 |
public Selector wakeup() {
|
|
203 |
synchronized (interruptLock) {
|
|
204 |
if (!interruptTriggered) {
|
|
205 |
kqueueWrapper.interrupt();
|
|
206 |
interruptTriggered = true;
|
|
207 |
}
|
|
208 |
}
|
|
209 |
return this;
|
|
210 |
}
|
|
211 |
|
|
212 |
|
|
213 |
static {
|
|
214 |
Util.load();
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|