author | alanb |
Wed, 08 May 2019 08:15:04 +0100 | |
changeset 54754 | 193a8f1a4f3b |
parent 54620 | 13b67c1420b8 |
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 |
||
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
28 |
import java.io.FileDescriptor; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
29 |
import java.io.IOException; |
2 | 30 |
import java.nio.ByteBuffer; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
31 |
import java.nio.channels.AsynchronousCloseException; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
32 |
import java.nio.channels.ClosedChannelException; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
33 |
import java.nio.channels.NotYetConnectedException; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
34 |
import java.nio.channels.Pipe; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
35 |
import java.nio.channels.SelectionKey; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
36 |
import java.nio.channels.spi.SelectorProvider; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
37 |
import java.util.Objects; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
38 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 39 |
|
40 |
class SourceChannelImpl |
|
41 |
extends Pipe.SourceChannel |
|
42 |
implements SelChImpl |
|
43 |
{ |
|
44 |
// Used to make native read and write calls |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
16921
diff
changeset
|
45 |
private static final NativeDispatcher nd = new FileDispatcherImpl(); |
2 | 46 |
|
47 |
// The file descriptor associated with this channel |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
48 |
private final FileDescriptor fd; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
49 |
private final int fdVal; |
2 | 50 |
|
51 |
// Lock held by current reading thread |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
52 |
private final ReentrantLock readLock = new ReentrantLock(); |
2 | 53 |
|
54 |
// Lock held by any thread that modifies the state fields declared below |
|
55 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
56 |
private final Object stateLock = new Object(); |
2 | 57 |
|
58 |
// -- The following fields are protected by stateLock |
|
59 |
||
60 |
// Channel state |
|
61 |
private static final int ST_INUSE = 0; |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
62 |
private static final int ST_CLOSING = 1; |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
63 |
private static final int ST_CLOSED = 2; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
64 |
private int state; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
65 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
66 |
// ID of native thread doing read, for signalling |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
67 |
private long thread; |
2 | 68 |
|
69 |
// -- End of fields protected by stateLock |
|
70 |
||
71 |
||
72 |
public FileDescriptor getFD() { |
|
73 |
return fd; |
|
74 |
} |
|
75 |
||
76 |
public int getFDVal() { |
|
77 |
return fdVal; |
|
78 |
} |
|
79 |
||
80 |
SourceChannelImpl(SelectorProvider sp, FileDescriptor fd) { |
|
81 |
super(sp); |
|
82 |
this.fd = fd; |
|
83 |
this.fdVal = IOUtil.fdVal(fd); |
|
84 |
} |
|
85 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
86 |
/** |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
87 |
* Closes the read end of the pipe if there are no read operation in |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
88 |
* progress and the channel is not registered with a Selector. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
89 |
*/ |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
90 |
private boolean tryClose() throws IOException { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
91 |
assert Thread.holdsLock(stateLock) && state == ST_CLOSING; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
92 |
if (thread == 0 && !isRegistered()) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
93 |
state = ST_CLOSED; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
94 |
nd.close(fd); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
95 |
return true; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
96 |
} else { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
97 |
return false; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
98 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
99 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
100 |
|
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
101 |
/** |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
102 |
* Invokes tryClose to attempt to close the read end of the pipe. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
103 |
* |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
104 |
* This method is used for deferred closing by I/O and Selector operations. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
105 |
*/ |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
106 |
private void tryFinishClose() { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
107 |
try { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
108 |
tryClose(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
109 |
} catch (IOException ignore) { } |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
110 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
111 |
|
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
112 |
/** |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
113 |
* Closes this channel when configured in blocking mode. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
114 |
* |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
115 |
* If there is a read operation in progress then the read-end of the pipe |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
116 |
* is pre-closed and the reader is signalled, in which case the final close |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
117 |
* is deferred until the reader aborts. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
118 |
*/ |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
119 |
private void implCloseBlockingMode() throws IOException { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
120 |
synchronized (stateLock) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
121 |
assert state < ST_CLOSING; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
122 |
state = ST_CLOSING; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
123 |
if (!tryClose()) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
124 |
long th = thread; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
125 |
if (th != 0) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
126 |
nd.preClose(fd); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
127 |
NativeThread.signal(th); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
128 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
129 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
130 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
131 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
132 |
|
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
133 |
/** |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
134 |
* Closes this channel when configured in non-blocking mode. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
135 |
* |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
136 |
* If the channel is registered with a Selector then the close is deferred |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
137 |
* until the channel is flushed from all Selectors. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
138 |
*/ |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
139 |
private void implCloseNonBlockingMode() throws IOException { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
140 |
synchronized (stateLock) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
141 |
assert state < ST_CLOSING; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
142 |
state = ST_CLOSING; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
143 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
144 |
// wait for any read operation to complete before trying to close |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
145 |
readLock.lock(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
146 |
readLock.unlock(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
147 |
synchronized (stateLock) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
148 |
if (state == ST_CLOSING) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
149 |
tryClose(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
150 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
151 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
152 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
153 |
|
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
154 |
/** |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
155 |
* Invoked by implCloseChannel to close the channel. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
156 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
157 |
@Override |
2 | 158 |
protected void implCloseSelectableChannel() throws IOException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
159 |
assert !isOpen(); |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
160 |
if (isBlocking()) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
161 |
implCloseBlockingMode(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
162 |
} else { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
163 |
implCloseNonBlockingMode(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
164 |
} |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
165 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
166 |
@Override |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
167 |
public void kill() { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
168 |
synchronized (stateLock) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
169 |
assert !isOpen(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
170 |
if (state == ST_CLOSING) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
171 |
tryFinishClose(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
172 |
} |
2 | 173 |
} |
174 |
} |
|
175 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
176 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
177 |
protected void implConfigureBlocking(boolean block) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
178 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
179 |
try { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
180 |
synchronized (stateLock) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
181 |
if (!isOpen()) |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
182 |
throw new ClosedChannelException(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
183 |
IOUtil.configureBlocking(fd, block); |
2 | 184 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
185 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
186 |
readLock.unlock(); |
2 | 187 |
} |
188 |
} |
|
189 |
||
49493 | 190 |
public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) { |
191 |
int intOps = ski.nioInterestOps(); |
|
192 |
int oldOps = ski.nioReadyOps(); |
|
2 | 193 |
int newOps = initialOps; |
194 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
195 |
if ((ops & Net.POLLNVAL) != 0) |
2 | 196 |
throw new Error("POLLNVAL detected"); |
197 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
198 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 199 |
newOps = intOps; |
49493 | 200 |
ski.nioReadyOps(newOps); |
2 | 201 |
return (newOps & ~oldOps) != 0; |
202 |
} |
|
203 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
204 |
if (((ops & Net.POLLIN) != 0) && |
2 | 205 |
((intOps & SelectionKey.OP_READ) != 0)) |
206 |
newOps |= SelectionKey.OP_READ; |
|
207 |
||
49493 | 208 |
ski.nioReadyOps(newOps); |
2 | 209 |
return (newOps & ~oldOps) != 0; |
210 |
} |
|
211 |
||
49493 | 212 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) { |
213 |
return translateReadyOps(ops, ski.nioReadyOps(), ski); |
|
2 | 214 |
} |
215 |
||
49493 | 216 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) { |
217 |
return translateReadyOps(ops, 0, ski); |
|
2 | 218 |
} |
219 |
||
49493 | 220 |
public int translateInterestOps(int ops) { |
221 |
int newOps = 0; |
|
2 | 222 |
if (ops == SelectionKey.OP_READ) |
49493 | 223 |
newOps |= Net.POLLIN; |
224 |
return newOps; |
|
2 | 225 |
} |
226 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
227 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
228 |
* Marks the beginning of a read operation that might block. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
229 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
230 |
* @throws ClosedChannelException if the channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
231 |
* @throws NotYetConnectedException if the channel is not yet connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
232 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
233 |
private void beginRead(boolean blocking) throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
234 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
235 |
// set hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
236 |
begin(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
237 |
} |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
238 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
239 |
if (!isOpen()) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
240 |
throw new ClosedChannelException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
241 |
if (blocking) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
242 |
thread = NativeThread.current(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
243 |
} |
2 | 244 |
} |
245 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
246 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
247 |
* Marks the end of a read operation that may have blocked. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
248 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
249 |
* @throws AsynchronousCloseException if the channel was closed due to this |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
250 |
* thread being interrupted on a blocking read operation. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
251 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
252 |
private void endRead(boolean blocking, boolean completed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
253 |
throws AsynchronousCloseException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
254 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
255 |
if (blocking) { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
256 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
257 |
thread = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
258 |
if (state == ST_CLOSING) { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
259 |
tryFinishClose(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
260 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
261 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
262 |
// remove hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
263 |
end(completed); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
264 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
265 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
266 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
267 |
@Override |
2 | 268 |
public int read(ByteBuffer dst) throws IOException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
269 |
Objects.requireNonNull(dst); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
270 |
|
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
271 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
272 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
273 |
boolean blocking = isBlocking(); |
2 | 274 |
int n = 0; |
275 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
276 |
beginRead(blocking); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
277 |
n = IOUtil.read(fd, dst, -1, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
278 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
279 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
280 |
park(Net.POLLIN); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
281 |
n = IOUtil.read(fd, dst, -1, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
282 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
283 |
} |
2 | 284 |
} finally { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
285 |
endRead(blocking, n > 0); |
2 | 286 |
assert IOStatus.check(n); |
287 |
} |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
288 |
return IOStatus.normalize(n); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
289 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
290 |
readLock.unlock(); |
2 | 291 |
} |
292 |
} |
|
293 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
294 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
295 |
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
296 |
Objects.checkFromIndexSize(offset, length, dsts.length); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
297 |
|
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
298 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
299 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
300 |
boolean blocking = isBlocking(); |
2 | 301 |
long n = 0; |
302 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
303 |
beginRead(blocking); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
304 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
305 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
306 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
307 |
park(Net.POLLIN); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
308 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
309 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49493
diff
changeset
|
310 |
} |
2 | 311 |
} finally { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
312 |
endRead(blocking, n > 0); |
2 | 313 |
assert IOStatus.check(n); |
314 |
} |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
315 |
return IOStatus.normalize(n); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
316 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
317 |
readLock.unlock(); |
2 | 318 |
} |
319 |
} |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
320 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
321 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
322 |
public long read(ByteBuffer[] dsts) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
323 |
return read(dsts, 0, dsts.length); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
324 |
} |
2 | 325 |
} |