author | alanb |
Wed, 20 Nov 2019 08:35:53 +0000 | |
changeset 59146 | 455612b3161a |
parent 50439 | c5c827f3bf72 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
50439
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
28 |
import java.lang.invoke.ConstantBootstraps; |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
29 |
import java.lang.invoke.MethodHandles; |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
30 |
import java.lang.invoke.VarHandle; |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
31 |
import java.nio.channels.CancelledKeyException; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
32 |
import java.nio.channels.SelectableChannel; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
33 |
import java.nio.channels.SelectionKey; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
34 |
import java.nio.channels.Selector; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
35 |
import java.nio.channels.spi.AbstractSelectionKey; |
2 | 36 |
|
37 |
||
38 |
/** |
|
49290 | 39 |
* An implementation of SelectionKey. |
2 | 40 |
*/ |
41 |
||
49290 | 42 |
public final class SelectionKeyImpl |
2 | 43 |
extends AbstractSelectionKey |
44 |
{ |
|
50439
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
45 |
private static final VarHandle INTERESTOPS = |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
46 |
ConstantBootstraps.fieldVarHandle( |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
47 |
MethodHandles.lookup(), |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
48 |
"interestOps", |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
49 |
VarHandle.class, |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
50 |
SelectionKeyImpl.class, int.class); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
51 |
|
49526 | 52 |
private final SelChImpl channel; |
49493 | 53 |
private final SelectorImpl selector; |
2 | 54 |
|
55 |
private volatile int interestOps; |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
56 |
private volatile int readyOps; |
2 | 57 |
|
49493 | 58 |
// registered events in kernel, used by some Selector implementations |
59 |
private int registeredEvents; |
|
60 |
||
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
61 |
// registered events need to be reset, used by some Selector implementations |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
62 |
private volatile boolean reset; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
63 |
|
49493 | 64 |
// index of key in pollfd array, used by some Selector implementations |
65 |
private int index; |
|
66 |
||
2 | 67 |
SelectionKeyImpl(SelChImpl ch, SelectorImpl sel) { |
68 |
channel = ch; |
|
69 |
selector = sel; |
|
70 |
} |
|
71 |
||
49493 | 72 |
private void ensureValid() { |
73 |
if (!isValid()) |
|
74 |
throw new CancelledKeyException(); |
|
75 |
} |
|
76 |
||
49526 | 77 |
int getFDVal() { |
78 |
return channel.getFDVal(); |
|
79 |
} |
|
80 |
||
49290 | 81 |
@Override |
2 | 82 |
public SelectableChannel channel() { |
83 |
return (SelectableChannel)channel; |
|
84 |
} |
|
85 |
||
49290 | 86 |
@Override |
2 | 87 |
public Selector selector() { |
49493 | 88 |
return selector; |
2 | 89 |
} |
90 |
||
49290 | 91 |
@Override |
2 | 92 |
public int interestOps() { |
93 |
ensureValid(); |
|
94 |
return interestOps; |
|
95 |
} |
|
96 |
||
49290 | 97 |
@Override |
2 | 98 |
public SelectionKey interestOps(int ops) { |
99 |
ensureValid(); |
|
50439
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
100 |
if ((ops & ~channel().validOps()) != 0) |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
101 |
throw new IllegalArgumentException(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
102 |
int oldOps = (int) INTERESTOPS.getAndSet(this, ops); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
103 |
if (ops != oldOps) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
104 |
selector.setEventOps(this); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
105 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
106 |
return this; |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
107 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
108 |
|
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
109 |
@Override |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
110 |
public int interestOpsOr(int ops) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
111 |
ensureValid(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
112 |
if ((ops & ~channel().validOps()) != 0) |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
113 |
throw new IllegalArgumentException(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
114 |
int oldVal = (int) INTERESTOPS.getAndBitwiseOr(this, ops); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
115 |
if (oldVal != (oldVal | ops)) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
116 |
selector.setEventOps(this); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
117 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
118 |
return oldVal; |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
119 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
120 |
|
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
121 |
@Override |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
122 |
public int interestOpsAnd(int ops) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
123 |
ensureValid(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
124 |
int oldVal = (int) INTERESTOPS.getAndBitwiseAnd(this, ops); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
125 |
if (oldVal != (oldVal & ops)) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
126 |
selector.setEventOps(this); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
127 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
128 |
return oldVal; |
2 | 129 |
} |
130 |
||
49290 | 131 |
@Override |
2 | 132 |
public int readyOps() { |
133 |
ensureValid(); |
|
134 |
return readyOps; |
|
135 |
} |
|
136 |
||
137 |
// The nio versions of these operations do not care if a key |
|
138 |
// has been invalidated. They are for internal use by nio code. |
|
139 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
140 |
public void nioReadyOps(int ops) { |
2 | 141 |
readyOps = ops; |
142 |
} |
|
143 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
144 |
public int nioReadyOps() { |
2 | 145 |
return readyOps; |
146 |
} |
|
147 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
148 |
public SelectionKey nioInterestOps(int ops) { |
2 | 149 |
if ((ops & ~channel().validOps()) != 0) |
150 |
throw new IllegalArgumentException(); |
|
151 |
interestOps = ops; |
|
49526 | 152 |
selector.setEventOps(this); |
2 | 153 |
return this; |
154 |
} |
|
155 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
156 |
public int nioInterestOps() { |
2 | 157 |
return interestOps; |
158 |
} |
|
159 |
||
49526 | 160 |
int translateInterestOps() { |
161 |
return channel.translateInterestOps(interestOps); |
|
162 |
} |
|
163 |
||
164 |
boolean translateAndSetReadyOps(int ops) { |
|
165 |
return channel.translateAndSetReadyOps(ops, this); |
|
166 |
} |
|
167 |
||
168 |
boolean translateAndUpdateReadyOps(int ops) { |
|
169 |
return channel.translateAndUpdateReadyOps(ops, this); |
|
170 |
} |
|
171 |
||
49493 | 172 |
void registeredEvents(int events) { |
173 |
// assert Thread.holdsLock(selector); |
|
174 |
this.registeredEvents = events; |
|
175 |
} |
|
176 |
||
177 |
int registeredEvents() { |
|
178 |
// assert Thread.holdsLock(selector); |
|
179 |
return registeredEvents; |
|
180 |
} |
|
181 |
||
182 |
int getIndex() { |
|
183 |
return index; |
|
184 |
} |
|
185 |
||
186 |
void setIndex(int i) { |
|
187 |
index = i; |
|
188 |
} |
|
189 |
||
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
190 |
/** |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
191 |
* Sets the reset flag, re-queues the key, and wakeups up the Selector |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
192 |
*/ |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
193 |
void reset() { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
194 |
reset = true; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
195 |
selector.setEventOps(this); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
196 |
selector.wakeup(); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
197 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
198 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
199 |
/** |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
200 |
* Clears the reset flag, returning the previous value of the flag |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
201 |
*/ |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
202 |
boolean getAndClearReset() { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
203 |
assert Thread.holdsLock(selector); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
204 |
boolean r = reset; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
205 |
if (r) |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
206 |
reset = false; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
207 |
return r; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
208 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
50439
diff
changeset
|
209 |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
210 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
211 |
public String toString() { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
212 |
StringBuilder sb = new StringBuilder(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
213 |
sb.append("channel=") |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
214 |
.append(channel) |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
215 |
.append(", selector=") |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
216 |
.append(selector); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
217 |
if (isValid()) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
218 |
sb.append(", interestOps=") |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
219 |
.append(interestOps) |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
220 |
.append(", readyOps=") |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
221 |
.append(readyOps); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
222 |
} else { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
223 |
sb.append(", invalid"); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
224 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
225 |
return sb.toString(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
226 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
227 |
|
49290 | 228 |
// used by Selector implementations to record when the key was selected |
229 |
int lastPolled; |
|
2 | 230 |
} |