author | igerasim |
Wed, 21 Aug 2019 13:49:16 -0700 | |
changeset 57826 | bf4c808a4488 |
parent 50439 | c5c827f3bf72 |
child 59146 | 455612b3161a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2018, 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 |
||
61 |
// index of key in pollfd array, used by some Selector implementations |
|
62 |
private int index; |
|
63 |
||
2 | 64 |
SelectionKeyImpl(SelChImpl ch, SelectorImpl sel) { |
65 |
channel = ch; |
|
66 |
selector = sel; |
|
67 |
} |
|
68 |
||
49493 | 69 |
private void ensureValid() { |
70 |
if (!isValid()) |
|
71 |
throw new CancelledKeyException(); |
|
72 |
} |
|
73 |
||
49526 | 74 |
int getFDVal() { |
75 |
return channel.getFDVal(); |
|
76 |
} |
|
77 |
||
49290 | 78 |
@Override |
2 | 79 |
public SelectableChannel channel() { |
80 |
return (SelectableChannel)channel; |
|
81 |
} |
|
82 |
||
49290 | 83 |
@Override |
2 | 84 |
public Selector selector() { |
49493 | 85 |
return selector; |
2 | 86 |
} |
87 |
||
49290 | 88 |
@Override |
2 | 89 |
public int interestOps() { |
90 |
ensureValid(); |
|
91 |
return interestOps; |
|
92 |
} |
|
93 |
||
49290 | 94 |
@Override |
2 | 95 |
public SelectionKey interestOps(int ops) { |
96 |
ensureValid(); |
|
50439
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
97 |
if ((ops & ~channel().validOps()) != 0) |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
98 |
throw new IllegalArgumentException(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
99 |
int oldOps = (int) INTERESTOPS.getAndSet(this, ops); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
100 |
if (ops != oldOps) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
101 |
selector.setEventOps(this); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
102 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
103 |
return this; |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
104 |
} |
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 |
@Override |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
107 |
public int interestOpsOr(int ops) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
108 |
ensureValid(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
109 |
if ((ops & ~channel().validOps()) != 0) |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
110 |
throw new IllegalArgumentException(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
111 |
int oldVal = (int) INTERESTOPS.getAndBitwiseOr(this, ops); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
112 |
if (oldVal != (oldVal | ops)) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
113 |
selector.setEventOps(this); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
114 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
115 |
return oldVal; |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
116 |
} |
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 |
@Override |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
119 |
public int interestOpsAnd(int ops) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
120 |
ensureValid(); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
121 |
int oldVal = (int) INTERESTOPS.getAndBitwiseAnd(this, ops); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
122 |
if (oldVal != (oldVal & ops)) { |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
123 |
selector.setEventOps(this); |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
124 |
} |
c5c827f3bf72
6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents:
49526
diff
changeset
|
125 |
return oldVal; |
2 | 126 |
} |
127 |
||
49290 | 128 |
@Override |
2 | 129 |
public int readyOps() { |
130 |
ensureValid(); |
|
131 |
return readyOps; |
|
132 |
} |
|
133 |
||
134 |
// The nio versions of these operations do not care if a key |
|
135 |
// has been invalidated. They are for internal use by nio code. |
|
136 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
137 |
public void nioReadyOps(int ops) { |
2 | 138 |
readyOps = ops; |
139 |
} |
|
140 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
141 |
public int nioReadyOps() { |
2 | 142 |
return readyOps; |
143 |
} |
|
144 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
145 |
public SelectionKey nioInterestOps(int ops) { |
2 | 146 |
if ((ops & ~channel().validOps()) != 0) |
147 |
throw new IllegalArgumentException(); |
|
148 |
interestOps = ops; |
|
49526 | 149 |
selector.setEventOps(this); |
2 | 150 |
return this; |
151 |
} |
|
152 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
153 |
public int nioInterestOps() { |
2 | 154 |
return interestOps; |
155 |
} |
|
156 |
||
49526 | 157 |
int translateInterestOps() { |
158 |
return channel.translateInterestOps(interestOps); |
|
159 |
} |
|
160 |
||
161 |
boolean translateAndSetReadyOps(int ops) { |
|
162 |
return channel.translateAndSetReadyOps(ops, this); |
|
163 |
} |
|
164 |
||
165 |
boolean translateAndUpdateReadyOps(int ops) { |
|
166 |
return channel.translateAndUpdateReadyOps(ops, this); |
|
167 |
} |
|
168 |
||
49493 | 169 |
void registeredEvents(int events) { |
170 |
// assert Thread.holdsLock(selector); |
|
171 |
this.registeredEvents = events; |
|
172 |
} |
|
173 |
||
174 |
int registeredEvents() { |
|
175 |
// assert Thread.holdsLock(selector); |
|
176 |
return registeredEvents; |
|
177 |
} |
|
178 |
||
179 |
int getIndex() { |
|
180 |
return index; |
|
181 |
} |
|
182 |
||
183 |
void setIndex(int i) { |
|
184 |
index = i; |
|
185 |
} |
|
186 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
187 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
188 |
public String toString() { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
189 |
StringBuilder sb = new StringBuilder(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
190 |
sb.append("channel=") |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
191 |
.append(channel) |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
192 |
.append(", selector=") |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
193 |
.append(selector); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
194 |
if (isValid()) { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
195 |
sb.append(", interestOps=") |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
196 |
.append(interestOps) |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
197 |
.append(", readyOps=") |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
198 |
.append(readyOps); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
199 |
} else { |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
200 |
sb.append(", invalid"); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
201 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
202 |
return sb.toString(); |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
203 |
} |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
204 |
|
49290 | 205 |
// used by Selector implementations to record when the key was selected |
206 |
int lastPolled; |
|
2 | 207 |
} |