author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 46094 | jdk/src/java.base/share/classes/sun/nio/ch/SelectorImpl.java@0c23b05caf7d |
child 49248 | 15a0e60c8b97 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
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
|
2 |
* Copyright (c) 2000, 2017, 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; |
|
29 |
import java.net.SocketException; |
|
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
|
30 |
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
|
31 |
import java.nio.channels.IllegalSelectorException; |
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.Selector; |
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.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
|
35 |
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
|
36 |
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
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
import java.util.Set; |
2 | 41 |
|
42 |
||
43 |
/** |
|
44 |
* Base Selector implementation class. |
|
45 |
*/ |
|
46 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
47 |
public abstract class SelectorImpl |
2 | 48 |
extends AbstractSelector |
49 |
{ |
|
50 |
||
51 |
// The set of keys with data ready for an operation |
|
895 | 52 |
protected Set<SelectionKey> selectedKeys; |
2 | 53 |
|
54 |
// The set of keys registered with this Selector |
|
895 | 55 |
protected HashSet<SelectionKey> keys; |
2 | 56 |
|
57 |
// Public views of the key sets |
|
895 | 58 |
private Set<SelectionKey> publicKeys; // Immutable |
59 |
private Set<SelectionKey> publicSelectedKeys; // Removal allowed, but not addition |
|
2 | 60 |
|
61 |
protected SelectorImpl(SelectorProvider sp) { |
|
62 |
super(sp); |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
63 |
keys = new HashSet<>(); |
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
publicSelectedKeys = Util.ungrowableSet(selectedKeys); |
2 | 67 |
} |
68 |
||
895 | 69 |
public 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
|
70 |
if (!isOpen()) |
2 | 71 |
throw new ClosedSelectorException(); |
72 |
return publicKeys; |
|
73 |
} |
|
74 |
||
895 | 75 |
public 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 |
||
81 |
protected abstract int doSelect(long timeout) throws IOException; |
|
82 |
||
83 |
private int lockAndDoSelect(long timeout) throws IOException { |
|
84 |
synchronized (this) { |
|
85 |
if (!isOpen()) |
|
86 |
throw new ClosedSelectorException(); |
|
87 |
synchronized (publicKeys) { |
|
88 |
synchronized (publicSelectedKeys) { |
|
89 |
return doSelect(timeout); |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
public int select(long timeout) |
|
96 |
throws IOException |
|
97 |
{ |
|
98 |
if (timeout < 0) |
|
99 |
throw new IllegalArgumentException("Negative timeout"); |
|
100 |
return lockAndDoSelect((timeout == 0) ? -1 : timeout); |
|
101 |
} |
|
102 |
||
103 |
public int select() throws IOException { |
|
104 |
return select(0); |
|
105 |
} |
|
106 |
||
107 |
public int selectNow() throws IOException { |
|
108 |
return lockAndDoSelect(0); |
|
109 |
} |
|
110 |
||
111 |
public void implCloseSelector() throws IOException { |
|
112 |
wakeup(); |
|
113 |
synchronized (this) { |
|
114 |
synchronized (publicKeys) { |
|
115 |
synchronized (publicSelectedKeys) { |
|
116 |
implClose(); |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
protected abstract void implClose() throws IOException; |
|
123 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
124 |
public void putEventOps(SelectionKeyImpl sk, int ops) { } |
2 | 125 |
|
126 |
protected final SelectionKey register(AbstractSelectableChannel ch, |
|
127 |
int ops, |
|
128 |
Object attachment) |
|
129 |
{ |
|
130 |
if (!(ch instanceof SelChImpl)) |
|
131 |
throw new IllegalSelectorException(); |
|
132 |
SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this); |
|
133 |
k.attach(attachment); |
|
134 |
synchronized (publicKeys) { |
|
135 |
implRegister(k); |
|
136 |
} |
|
137 |
k.interestOps(ops); |
|
138 |
return k; |
|
139 |
} |
|
140 |
||
141 |
protected abstract void implRegister(SelectionKeyImpl ski); |
|
142 |
||
143 |
void processDeregisterQueue() throws IOException { |
|
144 |
// Precondition: Synchronized on this, keys, and selectedKeys |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
145 |
Set<SelectionKey> cks = cancelledKeys(); |
2 | 146 |
synchronized (cks) { |
843
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
147 |
if (!cks.isEmpty()) { |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
148 |
Iterator<SelectionKey> i = cks.iterator(); |
843
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
149 |
while (i.hasNext()) { |
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
150 |
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); |
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
151 |
try { |
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
152 |
implDereg(ski); |
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
153 |
} catch (SocketException se) { |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
154 |
throw new IOException("Error deregistering key", se); |
843
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
155 |
} finally { |
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
156 |
i.remove(); |
e50aa264deca
6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents:
2
diff
changeset
|
157 |
} |
2 | 158 |
} |
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
protected abstract void implDereg(SelectionKeyImpl ski) throws IOException; |
|
164 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
29986
diff
changeset
|
165 |
public abstract Selector wakeup(); |
2 | 166 |
|
167 |
} |