author | alanb |
Wed, 28 Feb 2018 09:54:38 +0000 | |
changeset 49001 | ce06058197a4 |
parent 48761 | 74c1fa26435a |
child 49290 | 07779973cbe2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
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.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
30 |
import java.net.InetSocketAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
31 |
import java.net.ProtocolFamily; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
32 |
import java.net.ServerSocket; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
33 |
import java.net.SocketAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
34 |
import java.net.SocketOption; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
35 |
import java.net.StandardProtocolFamily; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
36 |
import java.net.StandardSocketOptions; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
37 |
import java.nio.channels.AlreadyBoundException; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
38 |
import java.nio.channels.AsynchronousCloseException; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
39 |
import java.nio.channels.ClosedChannelException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
40 |
import java.nio.channels.NotYetBoundException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
41 |
import java.nio.channels.SelectionKey; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
42 |
import java.nio.channels.ServerSocketChannel; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
43 |
import java.nio.channels.SocketChannel; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
44 |
import java.nio.channels.spi.SelectorProvider; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
45 |
import java.util.Collections; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
46 |
import java.util.HashSet; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
47 |
import java.util.Objects; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
48 |
import java.util.Set; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
49 |
import java.util.concurrent.locks.ReentrantLock; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
50 |
|
2446 | 51 |
import sun.net.NetHooks; |
2 | 52 |
|
53 |
/** |
|
54 |
* An implementation of ServerSocketChannels |
|
55 |
*/ |
|
56 |
||
57 |
class ServerSocketChannelImpl |
|
58 |
extends ServerSocketChannel |
|
59 |
implements SelChImpl |
|
60 |
{ |
|
61 |
// Used to make native close and configure calls |
|
62 |
private static NativeDispatcher nd; |
|
63 |
||
64 |
// Our file descriptor |
|
65 |
private final FileDescriptor fd; |
|
48761 | 66 |
private final int fdVal; |
2 | 67 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
68 |
// Lock held by thread currently blocked on this channel |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
69 |
private final ReentrantLock acceptLock = new ReentrantLock(); |
2 | 70 |
|
71 |
// Lock held by any thread that modifies the state fields declared below |
|
72 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
73 |
private final Object stateLock = new Object(); |
|
74 |
||
75 |
// -- The following fields are protected by stateLock |
|
76 |
||
77 |
// Channel state, increases monotonically |
|
78 |
private static final int ST_INUSE = 0; |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
79 |
private static final int ST_CLOSING = 1; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
80 |
private static final int ST_KILLPENDING = 2; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
81 |
private static final int ST_KILLED = 3; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
82 |
private int state; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
83 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
84 |
// ID of native thread currently blocked in this channel, for signalling |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
85 |
private long thread; |
2 | 86 |
|
87 |
// Binding |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
88 |
private InetSocketAddress localAddress; // null => unbound |
2 | 89 |
|
18192 | 90 |
// set true when exclusive binding is on and SO_REUSEADDR is emulated |
91 |
private boolean isReuseAddress; |
|
92 |
||
2 | 93 |
// Our socket adaptor, if any |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
94 |
private ServerSocket socket; |
2 | 95 |
|
96 |
// -- End of fields protected by stateLock |
|
97 |
||
98 |
||
6525 | 99 |
ServerSocketChannelImpl(SelectorProvider sp) throws IOException { |
2 | 100 |
super(sp); |
101 |
this.fd = Net.serverSocket(true); |
|
102 |
this.fdVal = IOUtil.fdVal(fd); |
|
103 |
} |
|
104 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
105 |
ServerSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound) |
2 | 106 |
throws IOException |
107 |
{ |
|
108 |
super(sp); |
|
109 |
this.fd = fd; |
|
110 |
this.fdVal = IOUtil.fdVal(fd); |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
111 |
if (bound) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
112 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
113 |
localAddress = Net.localAddress(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
114 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
115 |
} |
2 | 116 |
} |
117 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
118 |
// @throws ClosedChannelException if channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
119 |
private void ensureOpen() throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
120 |
if (!isOpen()) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
121 |
throw new ClosedChannelException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
122 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
123 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
124 |
@Override |
2 | 125 |
public ServerSocket socket() { |
126 |
synchronized (stateLock) { |
|
127 |
if (socket == null) |
|
128 |
socket = ServerSocketAdaptor.create(this); |
|
129 |
return socket; |
|
130 |
} |
|
131 |
} |
|
132 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
133 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
134 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
135 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
136 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
137 |
return (localAddress == null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
138 |
? null |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
139 |
: Net.getRevealedLocalAddress(localAddress); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
140 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
141 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
142 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
143 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
144 |
public <T> ServerSocketChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
145 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
146 |
{ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
147 |
Objects.requireNonNull(name); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
148 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
149 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
150 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
151 |
ensureOpen(); |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
152 |
|
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
153 |
if (name == StandardSocketOptions.IP_TOS) { |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
154 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
155 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
156 |
Net.setSocketOption(fd, family, name, value); |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
157 |
return this; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
158 |
} |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
159 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
160 |
if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { |
18192 | 161 |
// SO_REUSEADDR emulated when using exclusive bind |
162 |
isReuseAddress = (Boolean)value; |
|
163 |
} else { |
|
164 |
// no options that require special handling |
|
165 |
Net.setSocketOption(fd, Net.UNSPEC, name, value); |
|
166 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
169 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
170 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
171 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
172 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
173 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
174 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
{ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
176 |
Objects.requireNonNull(name); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
177 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
178 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
180 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
181 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
182 |
if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { |
18192 | 183 |
// SO_REUSEADDR emulated when using exclusive bind |
184 |
return (T)Boolean.valueOf(isReuseAddress); |
|
185 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
186 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
187 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
188 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
189 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
190 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
191 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
192 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
193 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
194 |
private static Set<SocketOption<?>> defaultOptions() { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
195 |
HashSet<SocketOption<?>> set = new HashSet<>(); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
196 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
197 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
36115 | 198 |
if (Net.isReusePortAvailable()) { |
199 |
set.add(StandardSocketOptions.SO_REUSEPORT); |
|
200 |
} |
|
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
201 |
set.add(StandardSocketOptions.IP_TOS); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
202 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
203 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
204 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
205 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
206 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
207 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
208 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
209 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
210 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
211 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
212 |
public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
213 |
acceptLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
214 |
try { |
2 | 215 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
216 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
217 |
if (localAddress != null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
218 |
throw new AlreadyBoundException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
219 |
InetSocketAddress isa = (local == null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
220 |
? new InetSocketAddress(0) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
221 |
: Net.checkAddress(local); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
222 |
SecurityManager sm = System.getSecurityManager(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
223 |
if (sm != null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
224 |
sm.checkListen(isa.getPort()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
225 |
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
226 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
227 |
Net.listen(fd, backlog < 1 ? 50 : backlog); |
2 | 228 |
localAddress = Net.localAddress(fd); |
229 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
230 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
231 |
acceptLock.unlock(); |
2 | 232 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
233 |
return this; |
2 | 234 |
} |
235 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
236 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
237 |
* Marks the beginning of an I/O operation that might block. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
238 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
239 |
* @throws ClosedChannelException if the channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
240 |
* @throws NotYetBoundException if the channel's socket has not been bound yet |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
241 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
242 |
private void begin(boolean blocking) throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
243 |
if (blocking) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
244 |
begin(); // set blocker to close channel if interrupted |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
245 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
246 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
247 |
if (localAddress == null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
248 |
throw new NotYetBoundException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
249 |
if (blocking) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
250 |
thread = NativeThread.current(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
251 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
252 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
253 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
254 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
255 |
* Marks the end of an I/O operation that may have blocked. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
256 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
257 |
* @throws AsynchronousCloseException if the channel was closed due to this |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
258 |
* thread being interrupted on a blocking I/O operation. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
259 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
260 |
private void end(boolean blocking, boolean completed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
261 |
throws AsynchronousCloseException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
262 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
263 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
264 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
265 |
thread = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
266 |
// notify any thread waiting in implCloseSelectableChannel |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
267 |
if (state == ST_CLOSING) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
268 |
stateLock.notifyAll(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
269 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
270 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
271 |
end(completed); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
272 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
273 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
274 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
275 |
@Override |
2 | 276 |
public SocketChannel accept() throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
277 |
acceptLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
278 |
try { |
2 | 279 |
int n = 0; |
280 |
FileDescriptor newfd = new FileDescriptor(); |
|
281 |
InetSocketAddress[] isaa = new InetSocketAddress[1]; |
|
282 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
283 |
boolean blocking = isBlocking(); |
2 | 284 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
285 |
begin(blocking); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
286 |
do { |
27300
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
287 |
n = accept(this.fd, newfd, isaa); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
288 |
} while (n == IOStatus.INTERRUPTED && isOpen()); |
2 | 289 |
} finally { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
290 |
end(blocking, n > 0); |
2 | 291 |
assert IOStatus.check(n); |
292 |
} |
|
293 |
||
294 |
if (n < 1) |
|
295 |
return null; |
|
296 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
297 |
// newly accepted socket is initially in blocking mode |
2 | 298 |
IOUtil.configureBlocking(newfd, true); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
299 |
|
2 | 300 |
InetSocketAddress isa = isaa[0]; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
301 |
SocketChannel sc = new SocketChannelImpl(provider(), newfd, isa); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
302 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
303 |
// check permitted to accept connections from the remote address |
2 | 304 |
SecurityManager sm = System.getSecurityManager(); |
305 |
if (sm != null) { |
|
306 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
307 |
sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort()); |
2 | 308 |
} catch (SecurityException x) { |
309 |
sc.close(); |
|
310 |
throw x; |
|
311 |
} |
|
312 |
} |
|
313 |
return sc; |
|
314 |
||
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
315 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
316 |
acceptLock.unlock(); |
2 | 317 |
} |
318 |
} |
|
319 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
320 |
@Override |
2 | 321 |
protected void implConfigureBlocking(boolean block) throws IOException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
322 |
acceptLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
323 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
324 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
325 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
326 |
IOUtil.configureBlocking(fd, block); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
327 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
328 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
329 |
acceptLock.unlock(); |
2 | 330 |
} |
331 |
} |
|
332 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
333 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
334 |
* Invoked by implCloseChannel to close the channel. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
335 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
336 |
* This method waits for outstanding I/O operations to complete. When in |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
337 |
* blocking mode, the socket is pre-closed and the threads in blocking I/O |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
338 |
* operations are signalled to ensure that the outstanding I/O operations |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
339 |
* complete quickly. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
340 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
341 |
* The socket is closed by this method when it is not registered with a |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
342 |
* Selector. Note that a channel configured blocking may be registered with |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
343 |
* a Selector. This arises when a key is canceled and the channel configured |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
344 |
* to blocking mode before the key is flushed from the Selector. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
345 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
346 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
347 |
protected void implCloseSelectableChannel() throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
348 |
assert !isOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
349 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
350 |
boolean interrupted = false; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
351 |
boolean blocking; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
352 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
353 |
// set state to ST_CLOSING |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
354 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
355 |
assert state < ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
356 |
state = ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
357 |
blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
358 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
359 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
360 |
// wait for any outstanding accept to complete |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
361 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
362 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
363 |
assert state == ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
364 |
long th = thread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
365 |
if (th != 0) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
366 |
nd.preClose(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
367 |
NativeThread.signal(th); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
368 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
369 |
// wait for accept operation to end |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
370 |
while (thread != 0) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
371 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
372 |
stateLock.wait(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
373 |
} catch (InterruptedException e) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
374 |
interrupted = true; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
375 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
376 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
377 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
378 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
379 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
380 |
// non-blocking mode: wait for accept to complete |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
381 |
acceptLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
382 |
acceptLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
383 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
384 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
385 |
// set state to ST_KILLPENDING |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
386 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
387 |
assert state == ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
388 |
state = ST_KILLPENDING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
389 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
390 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
391 |
// close socket if not registered with Selector |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
392 |
if (!isRegistered()) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
393 |
kill(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
394 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
395 |
// restore interrupt status |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
396 |
if (interrupted) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
397 |
Thread.currentThread().interrupt(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
398 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
399 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
400 |
@Override |
2 | 401 |
public void kill() throws IOException { |
402 |
synchronized (stateLock) { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
403 |
if (state == ST_KILLPENDING) { |
2 | 404 |
state = ST_KILLED; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
405 |
nd.close(fd); |
2 | 406 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
407 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
408 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
409 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
410 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
411 |
* Returns true if channel's socket is bound |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
412 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
413 |
boolean isBound() { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
414 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
415 |
return localAddress != null; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
416 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
417 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
418 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
419 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
420 |
* Returns the local address, or null if not bound |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
421 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
422 |
InetSocketAddress localAddress() { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
423 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
424 |
return localAddress; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
425 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
426 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
427 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
428 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
429 |
* Poll this channel's socket for a new connection up to the given timeout. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
430 |
* @return {@code true} if there is a connection to accept |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
431 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
432 |
boolean pollAccept(long timeout) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
433 |
assert Thread.holdsLock(blockingLock()) && isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
434 |
acceptLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
435 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
436 |
boolean polled = false; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
437 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
438 |
begin(true); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
439 |
int n = Net.poll(fd, Net.POLLIN, timeout); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
440 |
polled = (n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
441 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
442 |
end(true, polled); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
443 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
444 |
return polled; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
445 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
446 |
acceptLock.unlock(); |
2 | 447 |
} |
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* Translates native poll revent set into a ready operation set |
|
452 |
*/ |
|
453 |
public boolean translateReadyOps(int ops, int initialOps, |
|
454 |
SelectionKeyImpl sk) { |
|
455 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
456 |
int oldOps = sk.nioReadyOps(); |
|
457 |
int newOps = initialOps; |
|
458 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
459 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 460 |
// This should only happen if this channel is pre-closed while a |
461 |
// selection operation is in progress |
|
462 |
// ## Throw an error if this channel has not been pre-closed |
|
463 |
return false; |
|
464 |
} |
|
465 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
466 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 467 |
newOps = intOps; |
468 |
sk.nioReadyOps(newOps); |
|
469 |
return (newOps & ~oldOps) != 0; |
|
470 |
} |
|
471 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
472 |
if (((ops & Net.POLLIN) != 0) && |
2 | 473 |
((intOps & SelectionKey.OP_ACCEPT) != 0)) |
474 |
newOps |= SelectionKey.OP_ACCEPT; |
|
475 |
||
476 |
sk.nioReadyOps(newOps); |
|
477 |
return (newOps & ~oldOps) != 0; |
|
478 |
} |
|
479 |
||
480 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
481 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
482 |
} |
|
483 |
||
484 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
485 |
return translateReadyOps(ops, 0, sk); |
|
486 |
} |
|
487 |
||
488 |
/** |
|
489 |
* Translates an interest operation set into a native poll event set |
|
490 |
*/ |
|
491 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
492 |
int newOps = 0; |
|
493 |
||
494 |
// Translate ops |
|
495 |
if ((ops & SelectionKey.OP_ACCEPT) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
496 |
newOps |= Net.POLLIN; |
2 | 497 |
// Place ops into pollfd array |
498 |
sk.selector.putEventOps(sk, newOps); |
|
499 |
} |
|
500 |
||
501 |
public FileDescriptor getFD() { |
|
502 |
return fd; |
|
503 |
} |
|
504 |
||
505 |
public int getFDVal() { |
|
506 |
return fdVal; |
|
507 |
} |
|
508 |
||
509 |
public String toString() { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
510 |
StringBuilder sb = new StringBuilder(); |
2 | 511 |
sb.append(this.getClass().getName()); |
512 |
sb.append('['); |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
513 |
if (!isOpen()) { |
2 | 514 |
sb.append("closed"); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
515 |
} else { |
2 | 516 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
517 |
InetSocketAddress addr = localAddress; |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
518 |
if (addr == null) { |
2 | 519 |
sb.append("unbound"); |
520 |
} else { |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
521 |
sb.append(Net.getRevealedLocalAddressAsString(addr)); |
2 | 522 |
} |
523 |
} |
|
524 |
} |
|
525 |
sb.append(']'); |
|
526 |
return sb.toString(); |
|
527 |
} |
|
528 |
||
27300
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
529 |
/** |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
530 |
* Accept a connection on a socket. |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
531 |
* |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
532 |
* @implNote Wrap native call to allow instrumentation. |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
533 |
*/ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
534 |
private int accept(FileDescriptor ssfd, |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
535 |
FileDescriptor newfd, |
27300
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
536 |
InetSocketAddress[] isaa) |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
537 |
throws IOException |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
538 |
{ |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
539 |
return accept0(ssfd, newfd, isaa); |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
540 |
} |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
541 |
|
2 | 542 |
// -- Native methods -- |
543 |
||
544 |
// Accepts a new connection, setting the given file descriptor to refer to |
|
545 |
// the new socket and setting isaa[0] to the socket's remote address. |
|
546 |
// Returns 1 on success, or IOStatus.UNAVAILABLE (if non-blocking and no |
|
547 |
// connections are pending) or IOStatus.INTERRUPTED. |
|
548 |
// |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
549 |
private native int accept0(FileDescriptor ssfd, |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
550 |
FileDescriptor newfd, |
2 | 551 |
InetSocketAddress[] isaa) |
552 |
throws IOException; |
|
553 |
||
554 |
private static native void initIDs(); |
|
555 |
||
556 |
static { |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
557 |
IOUtil.load(); |
2 | 558 |
initIDs(); |
559 |
nd = new SocketDispatcher(); |
|
560 |
} |
|
561 |
||
562 |
} |