Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/nio/ch/SelectionKeyImpl.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, 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.FileDescriptor;
29
import java.lang.invoke.ConstantBootstraps;
30
import java.lang.invoke.MethodHandles;
31
import java.lang.invoke.VarHandle;
32
import java.nio.channels.CancelledKeyException;
33
import java.nio.channels.SelectableChannel;
34
import java.nio.channels.SelectionKey;
35
import java.nio.channels.Selector;
36
import java.nio.channels.spi.AbstractSelectionKey;
37
38
39
/**
40
* An implementation of SelectionKey.
41
*/
42
43
public final class SelectionKeyImpl
44
extends AbstractSelectionKey
45
{
46
private static final VarHandle INTERESTOPS =
47
ConstantBootstraps.fieldVarHandle(
48
MethodHandles.lookup(),
49
"interestOps",
50
VarHandle.class,
51
SelectionKeyImpl.class, int.class);
52
53
private final SelChImpl channel;
54
private final SelectorImpl selector;
55
56
private volatile int interestOps;
57
private volatile int readyOps;
58
59
// registered events in kernel, used by some Selector implementations
60
private int registeredEvents;
61
62
// registered events need to be reset, used by some Selector implementations
63
private volatile boolean reset;
64
65
// index of key in pollfd array, used by some Selector implementations
66
private int index;
67
68
SelectionKeyImpl(SelChImpl ch, SelectorImpl sel) {
69
channel = ch;
70
selector = sel;
71
}
72
73
private void ensureValid() {
74
if (!isValid())
75
throw new CancelledKeyException();
76
}
77
78
FileDescriptor getFD() {
79
return channel.getFD();
80
}
81
82
int getFDVal() {
83
return channel.getFDVal();
84
}
85
86
@Override
87
public SelectableChannel channel() {
88
return (SelectableChannel)channel;
89
}
90
91
@Override
92
public Selector selector() {
93
return selector;
94
}
95
96
@Override
97
public int interestOps() {
98
ensureValid();
99
return interestOps;
100
}
101
102
@Override
103
public SelectionKey interestOps(int ops) {
104
ensureValid();
105
if ((ops & ~channel().validOps()) != 0)
106
throw new IllegalArgumentException();
107
int oldOps = (int) INTERESTOPS.getAndSet(this, ops);
108
if (ops != oldOps) {
109
selector.setEventOps(this);
110
}
111
return this;
112
}
113
114
@Override
115
public int interestOpsOr(int ops) {
116
ensureValid();
117
if ((ops & ~channel().validOps()) != 0)
118
throw new IllegalArgumentException();
119
int oldVal = (int) INTERESTOPS.getAndBitwiseOr(this, ops);
120
if (oldVal != (oldVal | ops)) {
121
selector.setEventOps(this);
122
}
123
return oldVal;
124
}
125
126
@Override
127
public int interestOpsAnd(int ops) {
128
ensureValid();
129
int oldVal = (int) INTERESTOPS.getAndBitwiseAnd(this, ops);
130
if (oldVal != (oldVal & ops)) {
131
selector.setEventOps(this);
132
}
133
return oldVal;
134
}
135
136
@Override
137
public int readyOps() {
138
ensureValid();
139
return readyOps;
140
}
141
142
// The nio versions of these operations do not care if a key
143
// has been invalidated. They are for internal use by nio code.
144
145
public void nioReadyOps(int ops) {
146
readyOps = ops;
147
}
148
149
public int nioReadyOps() {
150
return readyOps;
151
}
152
153
public SelectionKey nioInterestOps(int ops) {
154
if ((ops & ~channel().validOps()) != 0)
155
throw new IllegalArgumentException();
156
interestOps = ops;
157
selector.setEventOps(this);
158
return this;
159
}
160
161
public int nioInterestOps() {
162
return interestOps;
163
}
164
165
int translateInterestOps() {
166
return channel.translateInterestOps(interestOps);
167
}
168
169
boolean translateAndSetReadyOps(int ops) {
170
return channel.translateAndSetReadyOps(ops, this);
171
}
172
173
boolean translateAndUpdateReadyOps(int ops) {
174
return channel.translateAndUpdateReadyOps(ops, this);
175
}
176
177
void registeredEvents(int events) {
178
// assert Thread.holdsLock(selector);
179
this.registeredEvents = events;
180
}
181
182
int registeredEvents() {
183
// assert Thread.holdsLock(selector);
184
return registeredEvents;
185
}
186
187
int getIndex() {
188
return index;
189
}
190
191
void setIndex(int i) {
192
index = i;
193
}
194
195
/**
196
* Sets the reset flag, re-queues the key, and wakeups up the Selector
197
*/
198
void reset() {
199
reset = true;
200
selector.setEventOps(this);
201
selector.wakeup();
202
}
203
204
/**
205
* Clears the reset flag, returning the previous value of the flag
206
*/
207
boolean getAndClearReset() {
208
assert Thread.holdsLock(selector);
209
boolean r = reset;
210
if (r)
211
reset = false;
212
return r;
213
}
214
215
@Override
216
public String toString() {
217
StringBuilder sb = new StringBuilder();
218
sb.append("channel=")
219
.append(channel)
220
.append(", selector=")
221
.append(selector);
222
if (isValid()) {
223
sb.append(", interestOps=")
224
.append(interestOps)
225
.append(", readyOps=")
226
.append(readyOps);
227
} else {
228
sb.append(", invalid");
229
}
230
return sb.toString();
231
}
232
233
// used by Selector implementations to record when the key was selected
234
int lastPolled;
235
}
236
237