Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java
38829 views
1
/*
2
* Copyright (c) 2011, 2015, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
54
// Map from a file descriptor to an entry containing the selection key
55
private HashMap<Integer,MapEntry> fdMap;
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
64
// used by updateSelectedKeys to handle cases where the same file
65
// descriptor is polled by more than one filter
66
private long updateCount;
67
68
// Used to map file descriptors to a selection key and "update count"
69
// (see updateSelectedKeys for usage).
70
private static class MapEntry {
71
SelectionKeyImpl ski;
72
long updateCount;
73
MapEntry(SelectionKeyImpl ski) {
74
this.ski = ski;
75
}
76
}
77
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
try {
88
kqueueWrapper = new KQueueArrayWrapper();
89
kqueueWrapper.initInterrupt(fd0, fd1);
90
fdMap = new HashMap<>();
91
totalChannels = 1;
92
} catch (Throwable t) {
93
try {
94
FileDispatcherImpl.closeIntFD(fd0);
95
} catch (IOException ioe0) {
96
t.addSuppressed(ioe0);
97
}
98
try {
99
FileDispatcherImpl.closeIntFD(fd1);
100
} catch (IOException ioe1) {
101
t.addSuppressed(ioe1);
102
}
103
throw t;
104
}
105
}
106
107
108
protected int doSelect(long timeout)
109
throws IOException
110
{
111
int entries = 0;
112
if (closed)
113
throw new ClosedSelectorException();
114
processDeregisterQueue();
115
try {
116
begin();
117
entries = kqueueWrapper.poll(timeout);
118
} finally {
119
end();
120
}
121
processDeregisterQueue();
122
return updateSelectedKeys(entries);
123
}
124
125
/**
126
* Update the keys whose fd's have been selected by kqueue.
127
* Add the ready keys to the selected key set.
128
* If the interrupt fd has been selected, drain it and clear the interrupt.
129
*/
130
private int updateSelectedKeys(int entries)
131
throws IOException
132
{
133
int numKeysUpdated = 0;
134
boolean interrupted = false;
135
136
// A file descriptor may be registered with kqueue with more than one
137
// filter and so there may be more than one event for a fd. The update
138
// count in the MapEntry tracks when the fd was last updated and this
139
// ensures that the ready ops are updated rather than replaced by a
140
// second or subsequent event.
141
updateCount++;
142
143
for (int i = 0; i < entries; i++) {
144
int nextFD = kqueueWrapper.getDescriptor(i);
145
if (nextFD == fd0) {
146
interrupted = true;
147
} else {
148
MapEntry me = fdMap.get(Integer.valueOf(nextFD));
149
150
// entry is null in the case of an interrupt
151
if (me != null) {
152
int rOps = kqueueWrapper.getReventOps(i);
153
SelectionKeyImpl ski = me.ski;
154
if (selectedKeys.contains(ski)) {
155
// first time this file descriptor has been encountered on this
156
// update?
157
if (me.updateCount != updateCount) {
158
if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
159
numKeysUpdated++;
160
me.updateCount = updateCount;
161
}
162
} else {
163
// ready ops have already been set on this update
164
ski.channel.translateAndUpdateReadyOps(rOps, ski);
165
}
166
} else {
167
ski.channel.translateAndSetReadyOps(rOps, ski);
168
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
169
selectedKeys.add(ski);
170
numKeysUpdated++;
171
me.updateCount = updateCount;
172
}
173
}
174
}
175
}
176
}
177
178
if (interrupted) {
179
// Clear the wakeup pipe
180
synchronized (interruptLock) {
181
IOUtil.drain(fd0);
182
interruptTriggered = false;
183
}
184
}
185
return numKeysUpdated;
186
}
187
188
189
protected void implClose() throws IOException {
190
if (!closed) {
191
closed = true;
192
193
// prevent further wakeup
194
synchronized (interruptLock) {
195
interruptTriggered = true;
196
}
197
198
FileDispatcherImpl.closeIntFD(fd0);
199
FileDispatcherImpl.closeIntFD(fd1);
200
if (kqueueWrapper != null) {
201
kqueueWrapper.close();
202
kqueueWrapper = null;
203
selectedKeys = null;
204
205
// Deregister channels
206
Iterator<SelectionKey> i = keys.iterator();
207
while (i.hasNext()) {
208
SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
209
deregister(ski);
210
SelectableChannel selch = ski.channel();
211
if (!selch.isOpen() && !selch.isRegistered())
212
((SelChImpl)selch).kill();
213
i.remove();
214
}
215
totalChannels = 0;
216
}
217
fd0 = -1;
218
fd1 = -1;
219
}
220
}
221
222
223
protected void implRegister(SelectionKeyImpl ski) {
224
if (closed)
225
throw new ClosedSelectorException();
226
int fd = IOUtil.fdVal(ski.channel.getFD());
227
fdMap.put(Integer.valueOf(fd), new MapEntry(ski));
228
totalChannels++;
229
keys.add(ski);
230
}
231
232
233
protected void implDereg(SelectionKeyImpl ski) throws IOException {
234
int fd = ski.channel.getFDVal();
235
fdMap.remove(Integer.valueOf(fd));
236
kqueueWrapper.release(ski.channel);
237
totalChannels--;
238
keys.remove(ski);
239
selectedKeys.remove(ski);
240
deregister((AbstractSelectionKey)ski);
241
SelectableChannel selch = ski.channel();
242
if (!selch.isOpen() && !selch.isRegistered())
243
((SelChImpl)selch).kill();
244
}
245
246
247
public void putEventOps(SelectionKeyImpl ski, int ops) {
248
if (closed)
249
throw new ClosedSelectorException();
250
kqueueWrapper.setInterest(ski.channel, ops);
251
}
252
253
254
public Selector wakeup() {
255
synchronized (interruptLock) {
256
if (!interruptTriggered) {
257
kqueueWrapper.interrupt();
258
interruptTriggered = true;
259
}
260
}
261
return this;
262
}
263
}
264
265