author | alanb |
Wed, 28 Feb 2018 09:54:38 +0000 | |
changeset 49001 | ce06058197a4 |
parent 48750 | ffbb784a8873 |
child 49493 | 814bd31f8da0 |
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! |
|
56 |
private final Object stateLock = new Object(); |
|
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; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
63 |
private static final int ST_KILLPENDING = 2; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
64 |
private static final int ST_KILLED = 3; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
65 |
private int state; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
66 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
67 |
// 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
|
68 |
private long thread; |
2 | 69 |
|
70 |
// -- End of fields protected by stateLock |
|
71 |
||
72 |
||
73 |
public FileDescriptor getFD() { |
|
74 |
return fd; |
|
75 |
} |
|
76 |
||
77 |
public int getFDVal() { |
|
78 |
return fdVal; |
|
79 |
} |
|
80 |
||
81 |
SourceChannelImpl(SelectorProvider sp, FileDescriptor fd) { |
|
82 |
super(sp); |
|
83 |
this.fd = fd; |
|
84 |
this.fdVal = IOUtil.fdVal(fd); |
|
85 |
} |
|
86 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
87 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
88 |
* Invoked by implCloseChannel to close the channel. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
89 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
90 |
@Override |
2 | 91 |
protected void implCloseSelectableChannel() throws IOException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
92 |
assert !isOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
93 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
94 |
boolean interrupted = false; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
95 |
boolean blocking; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
96 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
97 |
// set state to ST_CLOSING |
2 | 98 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
99 |
assert state < ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
100 |
state = ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
101 |
blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
102 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
103 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
104 |
// wait for any outstanding read to complete |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
105 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
106 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
107 |
assert state == ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
108 |
long th = thread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
109 |
if (th != 0) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
110 |
nd.preClose(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
111 |
NativeThread.signal(th); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
112 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
113 |
// wait for read operation to end |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
114 |
while (thread != 0) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
115 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
116 |
stateLock.wait(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
117 |
} catch (InterruptedException e) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
118 |
interrupted = true; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
119 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
120 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
121 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
122 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
123 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
124 |
// non-blocking mode: wait for read to complete |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
125 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
126 |
readLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
127 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
128 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
129 |
// set state to ST_KILLPENDING |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
130 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
131 |
assert state == ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
132 |
state = ST_KILLPENDING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
133 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
134 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
135 |
// close socket if not registered with Selector |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
136 |
if (!isRegistered()) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
137 |
kill(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
138 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
139 |
// restore interrupt status |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
140 |
if (interrupted) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
141 |
Thread.currentThread().interrupt(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
142 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
143 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
144 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
145 |
public void kill() throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
146 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
147 |
assert thread == 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
148 |
if (state == ST_KILLPENDING) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
149 |
state = ST_KILLED; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
150 |
nd.close(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
151 |
} |
2 | 152 |
} |
153 |
} |
|
154 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
155 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
156 |
protected void implConfigureBlocking(boolean block) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
157 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
158 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
159 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
160 |
IOUtil.configureBlocking(fd, block); |
2 | 161 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
162 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
163 |
readLock.unlock(); |
2 | 164 |
} |
165 |
} |
|
166 |
||
167 |
public boolean translateReadyOps(int ops, int initialOps, |
|
168 |
SelectionKeyImpl sk) { |
|
169 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
170 |
int oldOps = sk.nioReadyOps(); |
|
171 |
int newOps = initialOps; |
|
172 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
173 |
if ((ops & Net.POLLNVAL) != 0) |
2 | 174 |
throw new Error("POLLNVAL detected"); |
175 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
176 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 177 |
newOps = intOps; |
178 |
sk.nioReadyOps(newOps); |
|
179 |
return (newOps & ~oldOps) != 0; |
|
180 |
} |
|
181 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
182 |
if (((ops & Net.POLLIN) != 0) && |
2 | 183 |
((intOps & SelectionKey.OP_READ) != 0)) |
184 |
newOps |= SelectionKey.OP_READ; |
|
185 |
||
186 |
sk.nioReadyOps(newOps); |
|
187 |
return (newOps & ~oldOps) != 0; |
|
188 |
} |
|
189 |
||
190 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
191 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
192 |
} |
|
193 |
||
194 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
195 |
return translateReadyOps(ops, 0, sk); |
|
196 |
} |
|
197 |
||
198 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
199 |
if (ops == SelectionKey.OP_READ) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
200 |
ops = Net.POLLIN; |
2 | 201 |
sk.selector.putEventOps(sk, ops); |
202 |
} |
|
203 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
204 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
205 |
* 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
|
206 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
207 |
* @throws ClosedChannelException if the channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
208 |
* @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
|
209 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
210 |
private void beginRead(boolean blocking) throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
211 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
212 |
// set hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
213 |
begin(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
214 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
215 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
216 |
if (!isOpen()) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
217 |
throw new ClosedChannelException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
218 |
if (blocking) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
219 |
thread = NativeThread.current(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
220 |
} |
2 | 221 |
} |
222 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
223 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
224 |
* 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
|
225 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
226 |
* @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
|
227 |
* 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
|
228 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
229 |
private void endRead(boolean blocking, boolean completed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
230 |
throws AsynchronousCloseException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
231 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
232 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
233 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
234 |
thread = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
235 |
// notify any thread waiting in implCloseSelectableChannel |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
236 |
if (state == ST_CLOSING) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
237 |
stateLock.notifyAll(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
238 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
239 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
240 |
// remove hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
241 |
end(completed); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
242 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
243 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
244 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
245 |
@Override |
2 | 246 |
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
|
247 |
Objects.requireNonNull(dst); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
248 |
|
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
249 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
250 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
251 |
boolean blocking = isBlocking(); |
2 | 252 |
int n = 0; |
253 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
254 |
beginRead(blocking); |
2 | 255 |
do { |
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
12440
diff
changeset
|
256 |
n = IOUtil.read(fd, dst, -1, nd); |
2 | 257 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
258 |
} finally { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
259 |
endRead(blocking, n > 0); |
2 | 260 |
assert IOStatus.check(n); |
261 |
} |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
262 |
return IOStatus.normalize(n); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
263 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
264 |
readLock.unlock(); |
2 | 265 |
} |
266 |
} |
|
267 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
268 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
269 |
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
|
270 |
Objects.checkFromIndexSize(offset, length, dsts.length); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
271 |
|
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
272 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
273 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
274 |
boolean blocking = isBlocking(); |
2 | 275 |
long n = 0; |
276 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
277 |
beginRead(blocking); |
2 | 278 |
do { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
279 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
2 | 280 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
281 |
} finally { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
282 |
endRead(blocking, n > 0); |
2 | 283 |
assert IOStatus.check(n); |
284 |
} |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
285 |
return IOStatus.normalize(n); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
286 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
287 |
readLock.unlock(); |
2 | 288 |
} |
289 |
} |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
290 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
291 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
292 |
public long read(ByteBuffer[] dsts) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
293 |
return read(dsts, 0, dsts.length); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48750
diff
changeset
|
294 |
} |
2 | 295 |
} |