author | alanb |
Wed, 02 Oct 2019 09:16:18 +0100 | |
changeset 58439 | b25362cec8ce |
parent 50602 | ed8de3d0cd28 |
child 58496 | 7f34de3cdfe9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
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 sun.nio.ch; |
|
27 |
||
28 |
import java.io.IOException; |
|
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
29 |
import java.nio.channels.ClosedSelectorException; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
30 |
import java.nio.channels.IllegalSelectorException; |
49493 | 31 |
import java.nio.channels.SelectableChannel; |
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
32 |
import java.nio.channels.SelectionKey; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
33 |
import java.nio.channels.spi.AbstractSelectableChannel; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
34 |
import java.nio.channels.spi.AbstractSelector; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
35 |
import java.nio.channels.spi.SelectorProvider; |
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
36 |
import java.util.ArrayDeque; |
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
37 |
import java.util.Collections; |
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
38 |
import java.util.Deque; |
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
39 |
import java.util.HashSet; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
40 |
import java.util.Iterator; |
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
41 |
import java.util.Objects; |
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
42 |
import java.util.Set; |
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
43 |
import java.util.concurrent.ConcurrentHashMap; |
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
44 |
import java.util.function.Consumer; |
2 | 45 |
|
46 |
||
47 |
/** |
|
48 |
* Base Selector implementation class. |
|
49 |
*/ |
|
50 |
||
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
51 |
public abstract class SelectorImpl |
2 | 52 |
extends AbstractSelector |
53 |
{ |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
54 |
// The set of keys registered with this Selector |
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
55 |
private final Set<SelectionKey> keys; |
2 | 56 |
|
57 |
// The set of keys with data ready for an operation |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
58 |
private final Set<SelectionKey> selectedKeys; |
2 | 59 |
|
60 |
// Public views of the key sets |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
61 |
private final Set<SelectionKey> publicKeys; // Immutable |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
62 |
private final Set<SelectionKey> publicSelectedKeys; // Removal allowed, but not addition |
2 | 63 |
|
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
64 |
// pending cancelled keys for deregistration |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
65 |
private final Deque<SelectionKeyImpl> cancelledKeys = new ArrayDeque<>(); |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
66 |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
67 |
// used to check for reentrancy |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
68 |
private boolean inSelect; |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
69 |
|
2 | 70 |
protected SelectorImpl(SelectorProvider sp) { |
71 |
super(sp); |
|
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
72 |
keys = ConcurrentHashMap.newKeySet(); |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
73 |
selectedKeys = new HashSet<>(); |
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
74 |
publicKeys = Collections.unmodifiableSet(keys); |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
75 |
publicSelectedKeys = Util.ungrowableSet(selectedKeys); |
2 | 76 |
} |
77 |
||
49526 | 78 |
private void ensureOpen() { |
79 |
if (!isOpen()) |
|
80 |
throw new ClosedSelectorException(); |
|
81 |
} |
|
82 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
83 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
84 |
public final Set<SelectionKey> keys() { |
49526 | 85 |
ensureOpen(); |
2 | 86 |
return publicKeys; |
87 |
} |
|
88 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
89 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
90 |
public final Set<SelectionKey> selectedKeys() { |
49526 | 91 |
ensureOpen(); |
2 | 92 |
return publicSelectedKeys; |
93 |
} |
|
94 |
||
49290 | 95 |
/** |
49493 | 96 |
* Marks the beginning of a select operation that might block |
97 |
*/ |
|
98 |
protected final void begin(boolean blocking) { |
|
99 |
if (blocking) begin(); |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Marks the end of a select operation that may have blocked |
|
104 |
*/ |
|
105 |
protected final void end(boolean blocking) { |
|
106 |
if (blocking) end(); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Selects the keys for channels that are ready for I/O operations. |
|
111 |
* |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
112 |
* @param action the action to perform, can be null |
49493 | 113 |
* @param timeout timeout in milliseconds to wait, 0 to not wait, -1 to |
114 |
* wait indefinitely |
|
115 |
*/ |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
116 |
protected abstract int doSelect(Consumer<SelectionKey> action, long timeout) |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
117 |
throws IOException; |
2 | 118 |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
119 |
private int lockAndDoSelect(Consumer<SelectionKey> action, long timeout) |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
120 |
throws IOException |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
121 |
{ |
2 | 122 |
synchronized (this) { |
49526 | 123 |
ensureOpen(); |
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
124 |
if (inSelect) |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
125 |
throw new IllegalStateException("select in progress"); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
126 |
inSelect = true; |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
127 |
try { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
128 |
synchronized (publicSelectedKeys) { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
129 |
return doSelect(action, timeout); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
130 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
131 |
} finally { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
132 |
inSelect = false; |
2 | 133 |
} |
134 |
} |
|
135 |
} |
|
136 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
137 |
@Override |
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
138 |
public final int select(long timeout) throws IOException { |
2 | 139 |
if (timeout < 0) |
140 |
throw new IllegalArgumentException("Negative timeout"); |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
141 |
return lockAndDoSelect(null, (timeout == 0) ? -1 : timeout); |
2 | 142 |
} |
143 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
144 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
145 |
public final int select() throws IOException { |
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
146 |
return lockAndDoSelect(null, -1); |
2 | 147 |
} |
148 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
149 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
150 |
public final int selectNow() throws IOException { |
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
151 |
return lockAndDoSelect(null, 0); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
152 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
153 |
|
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
154 |
@Override |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
155 |
public final int select(Consumer<SelectionKey> action, long timeout) |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
156 |
throws IOException |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
157 |
{ |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
158 |
Objects.requireNonNull(action); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
159 |
if (timeout < 0) |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
160 |
throw new IllegalArgumentException("Negative timeout"); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
161 |
return lockAndDoSelect(action, (timeout == 0) ? -1 : timeout); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
162 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
163 |
|
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
164 |
@Override |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
165 |
public final int select(Consumer<SelectionKey> action) throws IOException { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
166 |
Objects.requireNonNull(action); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
167 |
return lockAndDoSelect(action, -1); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
168 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
169 |
|
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
170 |
@Override |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
171 |
public final int selectNow(Consumer<SelectionKey> action) throws IOException { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
172 |
Objects.requireNonNull(action); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
173 |
return lockAndDoSelect(action, 0); |
2 | 174 |
} |
175 |
||
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
176 |
/** |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
177 |
* Invoked by implCloseSelector to close the selector. |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
178 |
*/ |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
179 |
protected abstract void implClose() throws IOException; |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
180 |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
181 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
182 |
public final void implCloseSelector() throws IOException { |
2 | 183 |
wakeup(); |
184 |
synchronized (this) { |
|
49493 | 185 |
implClose(); |
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
186 |
synchronized (publicSelectedKeys) { |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
187 |
// Deregister channels |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
188 |
Iterator<SelectionKey> i = keys.iterator(); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
189 |
while (i.hasNext()) { |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
190 |
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
191 |
deregister(ski); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
192 |
SelectableChannel selch = ski.channel(); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
193 |
if (!selch.isOpen() && !selch.isRegistered()) |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
194 |
((SelChImpl)selch).kill(); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
195 |
selectedKeys.remove(ski); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
196 |
i.remove(); |
2 | 197 |
} |
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
198 |
assert selectedKeys.isEmpty() && keys.isEmpty(); |
2 | 199 |
} |
200 |
} |
|
201 |
} |
|
202 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
203 |
@Override |
2 | 204 |
protected final SelectionKey register(AbstractSelectableChannel ch, |
205 |
int ops, |
|
206 |
Object attachment) |
|
207 |
{ |
|
208 |
if (!(ch instanceof SelChImpl)) |
|
209 |
throw new IllegalSelectorException(); |
|
210 |
SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this); |
|
211 |
k.attach(attachment); |
|
49526 | 212 |
|
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
213 |
// register (if needed) before adding to key set |
49493 | 214 |
implRegister(k); |
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
215 |
|
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
216 |
// add to the selector's key set, removing it immediately if the selector |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
217 |
// is closed. The key is not in the channel's key set at this point but |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
218 |
// it may be observed by a thread iterating over the selector's key set. |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
219 |
keys.add(k); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
220 |
try { |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
221 |
k.interestOps(ops); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
222 |
} catch (ClosedSelectorException e) { |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
223 |
assert ch.keyFor(this) == null; |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
224 |
keys.remove(k); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
225 |
k.cancel(); |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
226 |
throw e; |
2 | 227 |
} |
228 |
return k; |
|
229 |
} |
|
230 |
||
49526 | 231 |
/** |
232 |
* Register the key in the selector. |
|
233 |
* |
|
234 |
* The default implementation checks if the selector is open. It should |
|
235 |
* be overridden by selector implementations as needed. |
|
236 |
*/ |
|
237 |
protected void implRegister(SelectionKeyImpl ski) { |
|
238 |
ensureOpen(); |
|
239 |
} |
|
2 | 240 |
|
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
241 |
/** |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
242 |
* Removes the key from the selector |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
243 |
*/ |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
244 |
protected abstract void implDereg(SelectionKeyImpl ski) throws IOException; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
245 |
|
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
246 |
/** |
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
247 |
* Queue a cancelled key for the next selection operation |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
248 |
*/ |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
249 |
public void cancel(SelectionKeyImpl ski) { |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
250 |
synchronized (cancelledKeys) { |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
251 |
cancelledKeys.addLast(ski); |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
252 |
} |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
253 |
} |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
254 |
|
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
255 |
/** |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
256 |
* Invoked by selection operations to process the cancelled keys |
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
257 |
*/ |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
258 |
protected final void processDeregisterQueue() throws IOException { |
49493 | 259 |
assert Thread.holdsLock(this); |
260 |
assert Thread.holdsLock(publicSelectedKeys); |
|
261 |
||
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
262 |
synchronized (cancelledKeys) { |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
263 |
SelectionKeyImpl ski; |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
264 |
while ((ski = cancelledKeys.pollFirst()) != null) { |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
265 |
// remove the key from the selector |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
266 |
implDereg(ski); |
49493 | 267 |
|
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
268 |
selectedKeys.remove(ski); |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
269 |
keys.remove(ski); |
49493 | 270 |
|
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
271 |
// remove from channel's key set |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
272 |
deregister(ski); |
49493 | 273 |
|
58439
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
274 |
SelectableChannel ch = ski.channel(); |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
275 |
if (!ch.isOpen() && !ch.isRegistered()) |
b25362cec8ce
8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents:
50602
diff
changeset
|
276 |
((SelChImpl)ch).kill(); |
2 | 277 |
} |
278 |
} |
|
279 |
} |
|
49290 | 280 |
|
281 |
/** |
|
50602
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
282 |
* Invoked by selection operations to handle ready events. If an action |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
283 |
* is specified then it is invoked to handle the key, otherwise the key |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
284 |
* is added to the selected-key set (or updated when it is already in the |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
285 |
* set). |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
286 |
*/ |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
287 |
protected final int processReadyEvents(int rOps, |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
288 |
SelectionKeyImpl ski, |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
289 |
Consumer<SelectionKey> action) { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
290 |
if (action != null) { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
291 |
ski.translateAndSetReadyOps(rOps); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
292 |
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
293 |
action.accept(ski); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
294 |
ensureOpen(); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
295 |
return 1; |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
296 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
297 |
} else { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
298 |
assert Thread.holdsLock(publicSelectedKeys); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
299 |
if (selectedKeys.contains(ski)) { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
300 |
if (ski.translateAndUpdateReadyOps(rOps)) { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
301 |
return 1; |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
302 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
303 |
} else { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
304 |
ski.translateAndSetReadyOps(rOps); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
305 |
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) { |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
306 |
selectedKeys.add(ski); |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
307 |
return 1; |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
308 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
309 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
310 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
311 |
return 0; |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
312 |
} |
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
313 |
|
ed8de3d0cd28
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents:
49802
diff
changeset
|
314 |
/** |
49802
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
315 |
* Invoked by interestOps to ensure the interest ops are updated at the |
8ac08fa69f00
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
49526
diff
changeset
|
316 |
* next selection operation. |
49290 | 317 |
*/ |
49526 | 318 |
protected abstract void setEventOps(SelectionKeyImpl ski); |
2 | 319 |
} |