author | alanb |
Fri, 30 Mar 2018 08:28:09 +0100 | |
changeset 49493 | 814bd31f8da0 |
parent 49290 | 07779973cbe2 |
child 49526 | cad4c844902a |
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 |
||
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; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
32649
diff
changeset
|
36 |
import java.util.Collections; |
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.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
|
38 |
import java.util.Iterator; |
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.Set; |
2 | 40 |
|
41 |
||
42 |
/** |
|
43 |
* Base Selector implementation class. |
|
44 |
*/ |
|
45 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
46 |
public abstract class SelectorImpl |
2 | 47 |
extends AbstractSelector |
48 |
{ |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
49 |
// The set of keys registered with this Selector |
49493 | 50 |
protected final Set<SelectionKey> keys; |
2 | 51 |
|
52 |
// The set of keys with data ready for an operation |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
53 |
protected final Set<SelectionKey> selectedKeys; |
2 | 54 |
|
55 |
// Public views of the key sets |
|
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
56 |
private final Set<SelectionKey> publicKeys; // Immutable |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
57 |
private final Set<SelectionKey> publicSelectedKeys; // Removal allowed, but not addition |
2 | 58 |
|
59 |
protected SelectorImpl(SelectorProvider sp) { |
|
60 |
super(sp); |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
61 |
keys = new HashSet<>(); |
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
publicSelectedKeys = Util.ungrowableSet(selectedKeys); |
2 | 65 |
} |
66 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
67 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
68 |
public final Set<SelectionKey> keys() { |
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
|
69 |
if (!isOpen()) |
2 | 70 |
throw new ClosedSelectorException(); |
71 |
return publicKeys; |
|
72 |
} |
|
73 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
74 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
75 |
public final Set<SelectionKey> selectedKeys() { |
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
|
76 |
if (!isOpen()) |
2 | 77 |
throw new ClosedSelectorException(); |
78 |
return publicSelectedKeys; |
|
79 |
} |
|
80 |
||
49290 | 81 |
/** |
82 |
* Returns the public view of the key sets |
|
83 |
*/ |
|
84 |
protected final Set<SelectionKey> nioKeys() { |
|
85 |
return publicKeys; |
|
86 |
} |
|
87 |
protected final Set<SelectionKey> nioSelectedKeys() { |
|
88 |
return publicSelectedKeys; |
|
89 |
} |
|
90 |
||
49493 | 91 |
/** |
92 |
* Marks the beginning of a select operation that might block |
|
93 |
*/ |
|
94 |
protected final void begin(boolean blocking) { |
|
95 |
if (blocking) begin(); |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Marks the end of a select operation that may have blocked |
|
100 |
*/ |
|
101 |
protected final void end(boolean blocking) { |
|
102 |
if (blocking) end(); |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Selects the keys for channels that are ready for I/O operations. |
|
107 |
* |
|
108 |
* @param timeout timeout in milliseconds to wait, 0 to not wait, -1 to |
|
109 |
* wait indefinitely |
|
110 |
*/ |
|
2 | 111 |
protected abstract int doSelect(long timeout) throws IOException; |
112 |
||
113 |
private int lockAndDoSelect(long timeout) throws IOException { |
|
114 |
synchronized (this) { |
|
115 |
if (!isOpen()) |
|
116 |
throw new ClosedSelectorException(); |
|
117 |
synchronized (publicKeys) { |
|
118 |
synchronized (publicSelectedKeys) { |
|
119 |
return doSelect(timeout); |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
125 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
126 |
public final int select(long timeout) |
2 | 127 |
throws IOException |
128 |
{ |
|
129 |
if (timeout < 0) |
|
130 |
throw new IllegalArgumentException("Negative timeout"); |
|
131 |
return lockAndDoSelect((timeout == 0) ? -1 : timeout); |
|
132 |
} |
|
133 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
134 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
135 |
public final int select() throws IOException { |
2 | 136 |
return select(0); |
137 |
} |
|
138 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
139 |
@Override |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
140 |
public final int selectNow() throws IOException { |
2 | 141 |
return lockAndDoSelect(0); |
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 void implCloseSelector() throws IOException { |
2 | 146 |
wakeup(); |
147 |
synchronized (this) { |
|
49493 | 148 |
implClose(); |
2 | 149 |
synchronized (publicKeys) { |
150 |
synchronized (publicSelectedKeys) { |
|
49493 | 151 |
// Deregister channels |
152 |
Iterator<SelectionKey> i = keys.iterator(); |
|
153 |
while (i.hasNext()) { |
|
154 |
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); |
|
155 |
deregister(ski); |
|
156 |
SelectableChannel selch = ski.channel(); |
|
157 |
if (!selch.isOpen() && !selch.isRegistered()) |
|
158 |
((SelChImpl)selch).kill(); |
|
159 |
selectedKeys.remove(ski); |
|
160 |
i.remove(); |
|
161 |
} |
|
162 |
assert selectedKeys.isEmpty() && keys.isEmpty(); |
|
2 | 163 |
} |
164 |
} |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
protected abstract void implClose() throws IOException; |
|
169 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
170 |
@Override |
2 | 171 |
protected final SelectionKey register(AbstractSelectableChannel ch, |
172 |
int ops, |
|
173 |
Object attachment) |
|
174 |
{ |
|
175 |
if (!(ch instanceof SelChImpl)) |
|
176 |
throw new IllegalSelectorException(); |
|
177 |
SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this); |
|
178 |
k.attach(attachment); |
|
49493 | 179 |
// register before adding to key set |
180 |
implRegister(k); |
|
2 | 181 |
synchronized (publicKeys) { |
49493 | 182 |
keys.add(k); |
2 | 183 |
} |
184 |
k.interestOps(ops); |
|
185 |
return k; |
|
186 |
} |
|
187 |
||
188 |
protected abstract void implRegister(SelectionKeyImpl ski); |
|
189 |
||
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
190 |
protected abstract void implDereg(SelectionKeyImpl ski) throws IOException; |
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
191 |
|
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
192 |
protected final void processDeregisterQueue() throws IOException { |
49493 | 193 |
assert Thread.holdsLock(this); |
194 |
assert Thread.holdsLock(publicKeys); |
|
195 |
assert Thread.holdsLock(publicSelectedKeys); |
|
196 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
197 |
Set<SelectionKey> cks = cancelledKeys(); |
2 | 198 |
synchronized (cks) { |
843
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
199 |
if (!cks.isEmpty()) { |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
200 |
Iterator<SelectionKey> i = cks.iterator(); |
843
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
201 |
while (i.hasNext()) { |
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
202 |
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); |
49493 | 203 |
i.remove(); |
204 |
||
205 |
// remove the key from the selector |
|
206 |
implDereg(ski); |
|
207 |
||
208 |
selectedKeys.remove(ski); |
|
209 |
keys.remove(ski); |
|
210 |
||
211 |
// remove from channel's key set |
|
212 |
deregister(ski); |
|
213 |
||
214 |
SelectableChannel ch = ski.channel(); |
|
215 |
if (!ch.isOpen() && !ch.isRegistered()) |
|
216 |
((SelChImpl)ch).kill(); |
|
2 | 217 |
} |
218 |
} |
|
219 |
} |
|
220 |
} |
|
49290 | 221 |
|
222 |
/** |
|
49493 | 223 |
* Change the event set in the selector |
49290 | 224 |
*/ |
49493 | 225 |
protected abstract void putEventOps(SelectionKeyImpl ski, int events); |
2 | 226 |
} |