author | alanb |
Tue, 08 Oct 2019 10:58:36 +0100 | |
changeset 58496 | 7f34de3cdfe9 |
parent 50602 | ed8de3d0cd28 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58496
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
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 java.nio.channels; |
|
27 |
||
58496
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
28 |
import java.lang.invoke.MethodHandles; |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
29 |
import java.lang.invoke.VarHandle; |
2 | 30 |
|
31 |
/** |
|
32 |
* A token representing the registration of a {@link SelectableChannel} with a |
|
33 |
* {@link Selector}. |
|
34 |
* |
|
35 |
* <p> A selection key is created each time a channel is registered with a |
|
36 |
* selector. A key remains valid until it is <i>cancelled</i> by invoking its |
|
37 |
* {@link #cancel cancel} method, by closing its channel, or by closing its |
|
38 |
* selector. Cancelling a key does not immediately remove it from its |
|
39 |
* selector; it is instead added to the selector's <a |
|
40 |
* href="Selector.html#ks"><i>cancelled-key set</i></a> for removal during the |
|
41 |
* next selection operation. The validity of a key may be tested by invoking |
|
42 |
* its {@link #isValid isValid} method. |
|
43 |
* |
|
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
34774
diff
changeset
|
44 |
* <a id="opsets"></a> |
2 | 45 |
* |
46 |
* <p> A selection key contains two <i>operation sets</i> represented as |
|
47 |
* integer values. Each bit of an operation set denotes a category of |
|
48 |
* selectable operations that are supported by the key's channel. |
|
49 |
* |
|
50 |
* <ul> |
|
51 |
* |
|
52 |
* <li><p> The <i>interest set</i> determines which operation categories will |
|
53 |
* be tested for readiness the next time one of the selector's selection |
|
54 |
* methods is invoked. The interest set is initialized with the value given |
|
55 |
* when the key is created; it may later be changed via the {@link |
|
56 |
* #interestOps(int)} method. </p></li> |
|
57 |
* |
|
58 |
* <li><p> The <i>ready set</i> identifies the operation categories for which |
|
59 |
* the key's channel has been detected to be ready by the key's selector. |
|
60 |
* The ready set is initialized to zero when the key is created; it may later |
|
61 |
* be updated by the selector during a selection operation, but it cannot be |
|
62 |
* updated directly. </p></li> |
|
63 |
* |
|
64 |
* </ul> |
|
65 |
* |
|
66 |
* <p> That a selection key's ready set indicates that its channel is ready for |
|
67 |
* some operation category is a hint, but not a guarantee, that an operation in |
|
68 |
* such a category may be performed by a thread without causing the thread to |
|
69 |
* block. A ready set is most likely to be accurate immediately after the |
|
70 |
* completion of a selection operation. It is likely to be made inaccurate by |
|
71 |
* external events and by I/O operations that are invoked upon the |
|
72 |
* corresponding channel. |
|
73 |
* |
|
74 |
* <p> This class defines all known operation-set bits, but precisely which |
|
75 |
* bits are supported by a given channel depends upon the type of the channel. |
|
76 |
* Each subclass of {@link SelectableChannel} defines an {@link |
|
77 |
* SelectableChannel#validOps() validOps()} method which returns a set |
|
78 |
* identifying just those operations that are supported by the channel. An |
|
79 |
* attempt to set or test an operation-set bit that is not supported by a key's |
|
80 |
* channel will result in an appropriate run-time exception. |
|
81 |
* |
|
82 |
* <p> It is often necessary to associate some application-specific data with a |
|
83 |
* selection key, for example an object that represents the state of a |
|
84 |
* higher-level protocol and handles readiness notifications in order to |
|
85 |
* implement that protocol. Selection keys therefore support the |
|
86 |
* <i>attachment</i> of a single arbitrary object to a key. An object can be |
|
87 |
* attached via the {@link #attach attach} method and then later retrieved via |
|
88 |
* the {@link #attachment() attachment} method. |
|
89 |
* |
|
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
47216
diff
changeset
|
90 |
* <p> Selection keys are safe for use by multiple concurrent threads. A |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
47216
diff
changeset
|
91 |
* selection operation will always use the interest-set value that was current |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
47216
diff
changeset
|
92 |
* at the moment that the operation began. </p> |
2 | 93 |
* |
94 |
* |
|
95 |
* @author Mark Reinhold |
|
96 |
* @author JSR-51 Expert Group |
|
97 |
* @since 1.4 |
|
98 |
* |
|
99 |
* @see SelectableChannel |
|
100 |
* @see Selector |
|
101 |
*/ |
|
102 |
||
103 |
public abstract class SelectionKey { |
|
104 |
||
105 |
/** |
|
106 |
* Constructs an instance of this class. |
|
107 |
*/ |
|
108 |
protected SelectionKey() { } |
|
109 |
||
110 |
||
111 |
// -- Channel and selector operations -- |
|
112 |
||
113 |
/** |
|
114 |
* Returns the channel for which this key was created. This method will |
|
18164 | 115 |
* continue to return the channel even after the key is cancelled. |
2 | 116 |
* |
117 |
* @return This key's channel |
|
118 |
*/ |
|
119 |
public abstract SelectableChannel channel(); |
|
120 |
||
121 |
/** |
|
122 |
* Returns the selector for which this key was created. This method will |
|
18164 | 123 |
* continue to return the selector even after the key is cancelled. |
2 | 124 |
* |
125 |
* @return This key's selector |
|
126 |
*/ |
|
127 |
public abstract Selector selector(); |
|
128 |
||
129 |
/** |
|
130 |
* Tells whether or not this key is valid. |
|
131 |
* |
|
132 |
* <p> A key is valid upon creation and remains so until it is cancelled, |
|
133 |
* its channel is closed, or its selector is closed. </p> |
|
134 |
* |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
135 |
* @return {@code true} if, and only if, this key is valid |
2 | 136 |
*/ |
137 |
public abstract boolean isValid(); |
|
138 |
||
139 |
/** |
|
140 |
* Requests that the registration of this key's channel with its selector |
|
141 |
* be cancelled. Upon return the key will be invalid and will have been |
|
142 |
* added to its selector's cancelled-key set. The key will be removed from |
|
143 |
* all of the selector's key sets during the next selection operation. |
|
144 |
* |
|
145 |
* <p> If this key has already been cancelled then invoking this method has |
|
146 |
* no effect. Once cancelled, a key remains forever invalid. </p> |
|
147 |
* |
|
148 |
* <p> This method may be invoked at any time. It synchronizes on the |
|
149 |
* selector's cancelled-key set, and therefore may block briefly if invoked |
|
150 |
* concurrently with a cancellation or selection operation involving the |
|
151 |
* same selector. </p> |
|
152 |
*/ |
|
153 |
public abstract void cancel(); |
|
154 |
||
155 |
||
156 |
// -- Operation-set accessors -- |
|
157 |
||
158 |
/** |
|
159 |
* Retrieves this key's interest set. |
|
160 |
* |
|
161 |
* <p> It is guaranteed that the returned set will only contain operation |
|
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
47216
diff
changeset
|
162 |
* bits that are valid for this key's channel. </p> |
2 | 163 |
* |
164 |
* @return This key's interest set |
|
165 |
* |
|
166 |
* @throws CancelledKeyException |
|
167 |
* If this key has been cancelled |
|
168 |
*/ |
|
169 |
public abstract int interestOps(); |
|
170 |
||
171 |
/** |
|
172 |
* Sets this key's interest set to the given value. |
|
173 |
* |
|
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
47216
diff
changeset
|
174 |
* <p> This method may be invoked at any time. If this method is invoked |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
47216
diff
changeset
|
175 |
* while a selection operation is in progress then it has no effect upon |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
47216
diff
changeset
|
176 |
* that operation; the change to the key's interest set will be seen by the |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
47216
diff
changeset
|
177 |
* next selection operation. |
2 | 178 |
* |
179 |
* @param ops The new interest set |
|
180 |
* |
|
181 |
* @return This selection key |
|
182 |
* |
|
183 |
* @throws IllegalArgumentException |
|
184 |
* If a bit in the set does not correspond to an operation that |
|
185 |
* is supported by this key's channel, that is, if |
|
18156 | 186 |
* {@code (ops & ~channel().validOps()) != 0} |
2 | 187 |
* |
188 |
* @throws CancelledKeyException |
|
189 |
* If this key has been cancelled |
|
190 |
*/ |
|
191 |
public abstract SelectionKey interestOps(int ops); |
|
192 |
||
193 |
/** |
|
50439
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
194 |
* Atomically sets this key's interest set to the bitwise union ("or") of |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
195 |
* the existing interest set and the given value. This method is guaranteed |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
196 |
* to be atomic with respect to other concurrent calls to this method or to |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
197 |
* {@link #interestOpsAnd(int)}. |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
198 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
199 |
* <p> This method may be invoked at any time. If this method is invoked |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
200 |
* while a selection operation is in progress then it has no effect upon |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
201 |
* that operation; the change to the key's interest set will be seen by the |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
202 |
* next selection operation. |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
203 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
204 |
* @implSpec The default implementation synchronizes on this key and invokes |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
205 |
* {@code interestOps()} and {@code interestOps(int)} to retrieve and set |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
206 |
* this key's interest set. |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
207 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
208 |
* @param ops The interest set to apply |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
209 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
210 |
* @return The previous interest set |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
211 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
212 |
* @throws IllegalArgumentException |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
213 |
* If a bit in the set does not correspond to an operation that |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
214 |
* is supported by this key's channel, that is, if |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
215 |
* {@code (ops & ~channel().validOps()) != 0} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
216 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
217 |
* @throws CancelledKeyException |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
218 |
* If this key has been cancelled |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
219 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
220 |
* @since 11 |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
221 |
*/ |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
222 |
public int interestOpsOr(int ops) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
223 |
synchronized (this) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
224 |
int oldVal = interestOps(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
225 |
interestOps(oldVal | ops); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
226 |
return oldVal; |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
227 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
228 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
229 |
|
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
230 |
/** |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
231 |
* Atomically sets this key's interest set to the bitwise intersection ("and") |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
232 |
* of the existing interest set and the given value. This method is guaranteed |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
233 |
* to be atomic with respect to other concurrent calls to this method or to |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
234 |
* {@link #interestOpsOr(int)}. |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
235 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
236 |
* <p> This method may be invoked at any time. If this method is invoked |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
237 |
* while a selection operation is in progress then it has no effect upon |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
238 |
* that operation; the change to the key's interest set will be seen by the |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
239 |
* next selection operation. |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
240 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
241 |
* @apiNote Unlike the {@code interestOps(int)} and {@code interestOpsOr(int)} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
242 |
* methods, this method does not throw {@code IllegalArgumentException} when |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
243 |
* invoked with bits in the interest set that do not correspond to an |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
244 |
* operation that is supported by this key's channel. This is to allow |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
245 |
* operation bits in the interest set to be cleared using bitwise complement |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
246 |
* values, e.g., {@code interestOpsAnd(~SelectionKey.OP_READ)} will remove |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
247 |
* the {@code OP_READ} from the interest set without affecting other bits. |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
248 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
249 |
* @implSpec The default implementation synchronizes on this key and invokes |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
250 |
* {@code interestOps()} and {@code interestOps(int)} to retrieve and set |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
251 |
* this key's interest set. |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
252 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
253 |
* @param ops The interest set to apply |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
254 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
255 |
* @return The previous interest set |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
256 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
257 |
* @throws CancelledKeyException |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
258 |
* If this key has been cancelled |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
259 |
* |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
260 |
* @since 11 |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
261 |
*/ |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
262 |
public int interestOpsAnd(int ops) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
263 |
synchronized (this) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
264 |
int oldVal = interestOps(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
265 |
interestOps(oldVal & ops); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
266 |
return oldVal; |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
267 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
268 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
269 |
|
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49802
diff
changeset
|
270 |
/** |
2 | 271 |
* Retrieves this key's ready-operation set. |
272 |
* |
|
273 |
* <p> It is guaranteed that the returned set will only contain operation |
|
274 |
* bits that are valid for this key's channel. </p> |
|
275 |
* |
|
276 |
* @return This key's ready-operation set |
|
277 |
* |
|
278 |
* @throws CancelledKeyException |
|
279 |
* If this key has been cancelled |
|
280 |
*/ |
|
281 |
public abstract int readyOps(); |
|
282 |
||
283 |
||
284 |
// -- Operation bits and bit-testing convenience methods -- |
|
285 |
||
286 |
/** |
|
287 |
* Operation-set bit for read operations. |
|
288 |
* |
|
289 |
* <p> Suppose that a selection key's interest set contains |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
290 |
* {@code OP_READ} at the start of a <a |
2 | 291 |
* href="Selector.html#selop">selection operation</a>. If the selector |
292 |
* detects that the corresponding channel is ready for reading, has reached |
|
293 |
* end-of-stream, has been remotely shut down for further reading, or has |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
294 |
* an error pending, then it will add {@code OP_READ} to the key's |
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
50439
diff
changeset
|
295 |
* ready-operation set. </p> |
2 | 296 |
*/ |
297 |
public static final int OP_READ = 1 << 0; |
|
298 |
||
299 |
/** |
|
18164 | 300 |
* Operation-set bit for write operations. |
2 | 301 |
* |
302 |
* <p> Suppose that a selection key's interest set contains |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
303 |
* {@code OP_WRITE} at the start of a <a |
2 | 304 |
* href="Selector.html#selop">selection operation</a>. If the selector |
305 |
* detects that the corresponding channel is ready for writing, has been |
|
306 |
* remotely shut down for further writing, or has an error pending, then it |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
50439
diff
changeset
|
307 |
* will add {@code OP_WRITE} to the key's ready set. </p> |
2 | 308 |
*/ |
309 |
public static final int OP_WRITE = 1 << 2; |
|
310 |
||
311 |
/** |
|
18164 | 312 |
* Operation-set bit for socket-connect operations. |
2 | 313 |
* |
314 |
* <p> Suppose that a selection key's interest set contains |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
315 |
* {@code OP_CONNECT} at the start of a <a |
2 | 316 |
* href="Selector.html#selop">selection operation</a>. If the selector |
317 |
* detects that the corresponding socket channel is ready to complete its |
|
318 |
* connection sequence, or has an error pending, then it will add |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
50439
diff
changeset
|
319 |
* {@code OP_CONNECT} to the key's ready set. </p> |
2 | 320 |
*/ |
321 |
public static final int OP_CONNECT = 1 << 3; |
|
322 |
||
323 |
/** |
|
18164 | 324 |
* Operation-set bit for socket-accept operations. |
2 | 325 |
* |
326 |
* <p> Suppose that a selection key's interest set contains |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
327 |
* {@code OP_ACCEPT} at the start of a <a |
2 | 328 |
* href="Selector.html#selop">selection operation</a>. If the selector |
329 |
* detects that the corresponding server-socket channel is ready to accept |
|
330 |
* another connection, or has an error pending, then it will add |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
50439
diff
changeset
|
331 |
* {@code OP_ACCEPT} to the key's ready set. </p> |
2 | 332 |
*/ |
333 |
public static final int OP_ACCEPT = 1 << 4; |
|
334 |
||
335 |
/** |
|
336 |
* Tests whether this key's channel is ready for reading. |
|
337 |
* |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
338 |
* <p> An invocation of this method of the form {@code k.isReadable()} |
2 | 339 |
* behaves in exactly the same way as the expression |
340 |
* |
|
18156 | 341 |
* <blockquote><pre>{@code |
342 |
* k.readyOps() & OP_READ != 0 |
|
343 |
* }</pre></blockquote> |
|
2 | 344 |
* |
345 |
* <p> If this key's channel does not support read operations then this |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
346 |
* method always returns {@code false}. </p> |
2 | 347 |
* |
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
348 |
* @return {@code true} if, and only if, |
18156 | 349 |
{@code readyOps() & OP_READ} is nonzero |
2 | 350 |
* |
351 |
* @throws CancelledKeyException |
|
352 |
* If this key has been cancelled |
|
353 |
*/ |
|
354 |
public final boolean isReadable() { |
|
355 |
return (readyOps() & OP_READ) != 0; |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* Tests whether this key's channel is ready for writing. |
|
360 |
* |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
361 |
* <p> An invocation of this method of the form {@code k.isWritable()} |
2 | 362 |
* behaves in exactly the same way as the expression |
363 |
* |
|
18156 | 364 |
* <blockquote><pre>{@code |
365 |
* k.readyOps() & OP_WRITE != 0 |
|
366 |
* }</pre></blockquote> |
|
2 | 367 |
* |
368 |
* <p> If this key's channel does not support write operations then this |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
369 |
* method always returns {@code false}. </p> |
2 | 370 |
* |
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
371 |
* @return {@code true} if, and only if, |
18156 | 372 |
* {@code readyOps() & OP_WRITE} is nonzero |
2 | 373 |
* |
374 |
* @throws CancelledKeyException |
|
375 |
* If this key has been cancelled |
|
376 |
*/ |
|
377 |
public final boolean isWritable() { |
|
378 |
return (readyOps() & OP_WRITE) != 0; |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* Tests whether this key's channel has either finished, or failed to |
|
383 |
* finish, its socket-connection operation. |
|
384 |
* |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
385 |
* <p> An invocation of this method of the form {@code k.isConnectable()} |
2 | 386 |
* behaves in exactly the same way as the expression |
387 |
* |
|
18164 | 388 |
* <blockquote><pre>{@code |
18156 | 389 |
* k.readyOps() & OP_CONNECT != 0 |
390 |
* }</pre></blockquote> |
|
2 | 391 |
* |
392 |
* <p> If this key's channel does not support socket-connect operations |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
393 |
* then this method always returns {@code false}. </p> |
2 | 394 |
* |
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
395 |
* @return {@code true} if, and only if, |
18156 | 396 |
* {@code readyOps() & OP_CONNECT} is nonzero |
2 | 397 |
* |
398 |
* @throws CancelledKeyException |
|
399 |
* If this key has been cancelled |
|
400 |
*/ |
|
401 |
public final boolean isConnectable() { |
|
402 |
return (readyOps() & OP_CONNECT) != 0; |
|
403 |
} |
|
404 |
||
405 |
/** |
|
406 |
* Tests whether this key's channel is ready to accept a new socket |
|
407 |
* connection. |
|
408 |
* |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
409 |
* <p> An invocation of this method of the form {@code k.isAcceptable()} |
2 | 410 |
* behaves in exactly the same way as the expression |
411 |
* |
|
18156 | 412 |
* <blockquote><pre>{@code |
413 |
* k.readyOps() & OP_ACCEPT != 0 |
|
414 |
* }</pre></blockquote> |
|
2 | 415 |
* |
416 |
* <p> If this key's channel does not support socket-accept operations then |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
417 |
* this method always returns {@code false}. </p> |
2 | 418 |
* |
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
419 |
* @return {@code true} if, and only if, |
18156 | 420 |
* {@code readyOps() & OP_ACCEPT} is nonzero |
2 | 421 |
* |
422 |
* @throws CancelledKeyException |
|
423 |
* If this key has been cancelled |
|
424 |
*/ |
|
425 |
public final boolean isAcceptable() { |
|
426 |
return (readyOps() & OP_ACCEPT) != 0; |
|
427 |
} |
|
428 |
||
429 |
||
430 |
// -- Attachments -- |
|
431 |
||
58496
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
432 |
private static final VarHandle ATTACHMENT; |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
433 |
static { |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
434 |
try { |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
435 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
436 |
ATTACHMENT = l.findVarHandle(SelectionKey.class, "attachment", Object.class); |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
437 |
} catch (Exception e) { |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
438 |
throw new InternalError(e); |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
439 |
} |
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
440 |
} |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32143
diff
changeset
|
441 |
private volatile Object attachment; |
2 | 442 |
|
443 |
/** |
|
444 |
* Attaches the given object to this key. |
|
445 |
* |
|
446 |
* <p> An attached object may later be retrieved via the {@link #attachment() |
|
447 |
* attachment} method. Only one object may be attached at a time; invoking |
|
448 |
* this method causes any previous attachment to be discarded. The current |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
449 |
* attachment may be discarded by attaching {@code null}. </p> |
2 | 450 |
* |
451 |
* @param ob |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
452 |
* The object to be attached; may be {@code null} |
2 | 453 |
* |
454 |
* @return The previously-attached object, if any, |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
455 |
* otherwise {@code null} |
2 | 456 |
*/ |
457 |
public final Object attach(Object ob) { |
|
58496
7f34de3cdfe9
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents:
50602
diff
changeset
|
458 |
return ATTACHMENT.getAndSet(this, ob); |
2 | 459 |
} |
460 |
||
461 |
/** |
|
18164 | 462 |
* Retrieves the current attachment. |
2 | 463 |
* |
464 |
* @return The object currently attached to this key, |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
465 |
* or {@code null} if there is no attachment |
2 | 466 |
*/ |
467 |
public final Object attachment() { |
|
468 |
return attachment; |
|
469 |
} |
|
470 |
||
471 |
} |