Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java
32288 views
1
/*
2
* Copyright (c) 2001, 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
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
/**
36
* An implementation of Selector for Solaris.
37
*/
38
class DevPollSelectorImpl
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
DevPollArrayWrapper pollWrapper;
48
49
// Maps from file descriptors to keys
50
private Map<Integer,SelectionKeyImpl> fdToKey;
51
52
// True if this Selector has been closed
53
private boolean closed = false;
54
55
// Lock for close/cleanup
56
private Object closeLock = new Object();
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
DevPollSelectorImpl(SelectorProvider sp) {
67
super(sp);
68
long pipeFds = IOUtil.makePipe(false);
69
fd0 = (int) (pipeFds >>> 32);
70
fd1 = (int) pipeFds;
71
try {
72
pollWrapper = new DevPollArrayWrapper();
73
pollWrapper.initInterrupt(fd0, fd1);
74
fdToKey = new HashMap<>();
75
} catch (Throwable t) {
76
try {
77
FileDispatcherImpl.closeIntFD(fd0);
78
} catch (IOException ioe0) {
79
t.addSuppressed(ioe0);
80
}
81
try {
82
FileDispatcherImpl.closeIntFD(fd1);
83
} catch (IOException ioe1) {
84
t.addSuppressed(ioe1);
85
}
86
throw t;
87
}
88
}
89
90
protected int doSelect(long timeout)
91
throws IOException
92
{
93
if (closed)
94
throw new ClosedSelectorException();
95
processDeregisterQueue();
96
try {
97
begin();
98
pollWrapper.poll(timeout);
99
} finally {
100
end();
101
}
102
processDeregisterQueue();
103
int numKeysUpdated = updateSelectedKeys();
104
if (pollWrapper.interrupted()) {
105
// Clear the wakeup pipe
106
pollWrapper.putReventOps(pollWrapper.interruptedIndex(), 0);
107
synchronized (interruptLock) {
108
pollWrapper.clearInterrupted();
109
IOUtil.drain(fd0);
110
interruptTriggered = false;
111
}
112
}
113
return numKeysUpdated;
114
}
115
116
/**
117
* Update the keys whose fd's have been selected by the devpoll
118
* driver. Add the ready keys to the ready queue.
119
*/
120
private int updateSelectedKeys() {
121
int entries = pollWrapper.updated;
122
int numKeysUpdated = 0;
123
for (int i=0; i<entries; i++) {
124
int nextFD = pollWrapper.getDescriptor(i);
125
SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
126
// ski is null in the case of an interrupt
127
if (ski != null) {
128
int rOps = pollWrapper.getReventOps(i);
129
if (selectedKeys.contains(ski)) {
130
if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
131
numKeysUpdated++;
132
}
133
} else {
134
ski.channel.translateAndSetReadyOps(rOps, ski);
135
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
136
selectedKeys.add(ski);
137
numKeysUpdated++;
138
}
139
}
140
}
141
}
142
return numKeysUpdated;
143
}
144
145
protected void implClose() throws IOException {
146
if (closed)
147
return;
148
closed = true;
149
150
// prevent further wakeup
151
synchronized (interruptLock) {
152
interruptTriggered = true;
153
}
154
155
FileDispatcherImpl.closeIntFD(fd0);
156
FileDispatcherImpl.closeIntFD(fd1);
157
158
pollWrapper.release(fd0);
159
pollWrapper.closeDevPollFD();
160
selectedKeys = null;
161
162
// Deregister channels
163
Iterator<SelectionKey> i = keys.iterator();
164
while (i.hasNext()) {
165
SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
166
deregister(ski);
167
SelectableChannel selch = ski.channel();
168
if (!selch.isOpen() && !selch.isRegistered())
169
((SelChImpl)selch).kill();
170
i.remove();
171
}
172
fd0 = -1;
173
fd1 = -1;
174
}
175
176
protected void implRegister(SelectionKeyImpl ski) {
177
int fd = IOUtil.fdVal(ski.channel.getFD());
178
fdToKey.put(Integer.valueOf(fd), ski);
179
keys.add(ski);
180
}
181
182
protected void implDereg(SelectionKeyImpl ski) throws IOException {
183
int i = ski.getIndex();
184
assert (i >= 0);
185
int fd = ski.channel.getFDVal();
186
fdToKey.remove(Integer.valueOf(fd));
187
pollWrapper.release(fd);
188
ski.setIndex(-1);
189
keys.remove(ski);
190
selectedKeys.remove(ski);
191
deregister((AbstractSelectionKey)ski);
192
SelectableChannel selch = ski.channel();
193
if (!selch.isOpen() && !selch.isRegistered())
194
((SelChImpl)selch).kill();
195
}
196
197
public void putEventOps(SelectionKeyImpl sk, int ops) {
198
if (closed)
199
throw new ClosedSelectorException();
200
int fd = IOUtil.fdVal(sk.channel.getFD());
201
pollWrapper.setInterest(fd, ops);
202
}
203
204
public Selector wakeup() {
205
synchronized (interruptLock) {
206
if (!interruptTriggered) {
207
pollWrapper.interrupt();
208
interruptTriggered = true;
209
}
210
}
211
return this;
212
}
213
}
214
215