author | alanb |
Thu, 25 Apr 2019 10:41:49 +0100 | |
changeset 54620 | 13b67c1420b8 |
parent 52427 | 3c6aa484536c |
child 54754 | 193a8f1a4f3b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, 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 java.nio.channels.spi; |
|
27 |
||
28 |
import java.io.IOException; |
|
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
29 |
import java.nio.channels.AsynchronousCloseException; |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
30 |
import java.nio.channels.Channel; |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
31 |
import java.nio.channels.ClosedByInterruptException; |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
32 |
import java.nio.channels.InterruptibleChannel; |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
33 |
import java.util.concurrent.locks.ReentrantLock; |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
34 |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49001
diff
changeset
|
35 |
import jdk.internal.access.SharedSecrets; |
2 | 36 |
import sun.nio.ch.Interruptible; |
37 |
||
38 |
||
39 |
/** |
|
40 |
* Base implementation class for interruptible channels. |
|
41 |
* |
|
42 |
* <p> This class encapsulates the low-level machinery required to implement |
|
43 |
* the asynchronous closing and interruption of channels. A concrete channel |
|
44 |
* class must invoke the {@link #begin begin} and {@link #end end} methods |
|
45 |
* before and after, respectively, invoking an I/O operation that might block |
|
46 |
* indefinitely. In order to ensure that the {@link #end end} method is always |
|
47 |
* invoked, these methods should be used within a |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
27757
diff
changeset
|
48 |
* {@code try} ... {@code finally} block: |
2 | 49 |
* |
47004 | 50 |
* <blockquote><pre id="be"> |
2 | 51 |
* boolean completed = false; |
52 |
* try { |
|
53 |
* begin(); |
|
54 |
* completed = ...; // Perform blocking I/O operation |
|
55 |
* return ...; // Return result |
|
56 |
* } finally { |
|
57 |
* end(completed); |
|
58 |
* }</pre></blockquote> |
|
59 |
* |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
27757
diff
changeset
|
60 |
* <p> The {@code completed} argument to the {@link #end end} method tells |
2 | 61 |
* whether or not the I/O operation actually completed, that is, whether it had |
62 |
* any effect that would be visible to the invoker. In the case of an |
|
63 |
* operation that reads bytes, for example, this argument should be |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
27757
diff
changeset
|
64 |
* {@code true} if, and only if, some bytes were actually transferred into the |
2 | 65 |
* invoker's target buffer. |
66 |
* |
|
67 |
* <p> A concrete channel class must also implement the {@link |
|
68 |
* #implCloseChannel implCloseChannel} method in such a way that if it is |
|
69 |
* invoked while another thread is blocked in a native I/O operation upon the |
|
70 |
* channel then that operation will immediately return, either by throwing an |
|
71 |
* exception or by returning normally. If a thread is interrupted or the |
|
72 |
* channel upon which it is blocked is asynchronously closed then the channel's |
|
73 |
* {@link #end end} method will throw the appropriate exception. |
|
74 |
* |
|
75 |
* <p> This class performs the synchronization required to implement the {@link |
|
76 |
* java.nio.channels.Channel} specification. Implementations of the {@link |
|
77 |
* #implCloseChannel implCloseChannel} method need not synchronize against |
|
78 |
* other threads that might be attempting to close the channel. </p> |
|
79 |
* |
|
80 |
* |
|
81 |
* @author Mark Reinhold |
|
82 |
* @author JSR-51 Expert Group |
|
83 |
* @since 1.4 |
|
84 |
*/ |
|
85 |
||
86 |
public abstract class AbstractInterruptibleChannel |
|
87 |
implements Channel, InterruptibleChannel |
|
88 |
{ |
|
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
89 |
private final ReentrantLock closeLock = new ReentrantLock(); |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32834
diff
changeset
|
90 |
private volatile boolean closed; |
2 | 91 |
|
92 |
/** |
|
93 |
* Initializes a new instance of this class. |
|
94 |
*/ |
|
95 |
protected AbstractInterruptibleChannel() { } |
|
96 |
||
97 |
/** |
|
98 |
* Closes this channel. |
|
99 |
* |
|
100 |
* <p> If the channel has already been closed then this method returns |
|
101 |
* immediately. Otherwise it marks the channel as closed and then invokes |
|
102 |
* the {@link #implCloseChannel implCloseChannel} method in order to |
|
103 |
* complete the close operation. </p> |
|
104 |
* |
|
105 |
* @throws IOException |
|
106 |
* If an I/O error occurs |
|
107 |
*/ |
|
108 |
public final void close() throws IOException { |
|
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
109 |
closeLock.lock(); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
110 |
try { |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32834
diff
changeset
|
111 |
if (closed) |
2 | 112 |
return; |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32834
diff
changeset
|
113 |
closed = true; |
2 | 114 |
implCloseChannel(); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
115 |
} finally { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
116 |
closeLock.unlock(); |
2 | 117 |
} |
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Closes this channel. |
|
122 |
* |
|
123 |
* <p> This method is invoked by the {@link #close close} method in order |
|
124 |
* to perform the actual work of closing the channel. This method is only |
|
125 |
* invoked if the channel has not yet been closed, and it is never invoked |
|
126 |
* more than once. |
|
127 |
* |
|
128 |
* <p> An implementation of this method must arrange for any other thread |
|
129 |
* that is blocked in an I/O operation upon this channel to return |
|
130 |
* immediately, either by throwing an exception or by returning normally. |
|
131 |
* </p> |
|
132 |
* |
|
133 |
* @throws IOException |
|
134 |
* If an I/O error occurs while closing the channel |
|
135 |
*/ |
|
136 |
protected abstract void implCloseChannel() throws IOException; |
|
137 |
||
138 |
public final boolean isOpen() { |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32834
diff
changeset
|
139 |
return !closed; |
2 | 140 |
} |
141 |
||
142 |
||
143 |
// -- Interruption machinery -- |
|
144 |
||
145 |
private Interruptible interruptor; |
|
7177
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
146 |
private volatile Thread interrupted; |
2 | 147 |
|
148 |
/** |
|
149 |
* Marks the beginning of an I/O operation that might block indefinitely. |
|
150 |
* |
|
151 |
* <p> This method should be invoked in tandem with the {@link #end end} |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
27757
diff
changeset
|
152 |
* method, using a {@code try} ... {@code finally} block as |
2 | 153 |
* shown <a href="#be">above</a>, in order to implement asynchronous |
154 |
* closing and interruption for this channel. </p> |
|
155 |
*/ |
|
156 |
protected final void begin() { |
|
157 |
if (interruptor == null) { |
|
158 |
interruptor = new Interruptible() { |
|
7177
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
159 |
public void interrupt(Thread target) { |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
160 |
closeLock.lock(); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
161 |
try { |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32834
diff
changeset
|
162 |
if (closed) |
2 | 163 |
return; |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32834
diff
changeset
|
164 |
closed = true; |
7177
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
165 |
interrupted = target; |
2 | 166 |
try { |
167 |
AbstractInterruptibleChannel.this.implCloseChannel(); |
|
168 |
} catch (IOException x) { } |
|
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
169 |
} finally { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
52427
diff
changeset
|
170 |
closeLock.unlock(); |
2 | 171 |
} |
172 |
}}; |
|
173 |
} |
|
174 |
blockedOn(interruptor); |
|
7177
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
175 |
Thread me = Thread.currentThread(); |
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
176 |
if (me.isInterrupted()) |
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
177 |
interruptor.interrupt(me); |
2 | 178 |
} |
179 |
||
180 |
/** |
|
181 |
* Marks the end of an I/O operation that might block indefinitely. |
|
182 |
* |
|
183 |
* <p> This method should be invoked in tandem with the {@link #begin |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
27757
diff
changeset
|
184 |
* begin} method, using a {@code try} ... {@code finally} block |
2 | 185 |
* as shown <a href="#be">above</a>, in order to implement asynchronous |
186 |
* closing and interruption for this channel. </p> |
|
187 |
* |
|
188 |
* @param completed |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
27757
diff
changeset
|
189 |
* {@code true} if, and only if, the I/O operation completed |
2 | 190 |
* successfully, that is, had some effect that would be visible to |
191 |
* the operation's invoker |
|
192 |
* |
|
193 |
* @throws AsynchronousCloseException |
|
194 |
* If the channel was asynchronously closed |
|
195 |
* |
|
196 |
* @throws ClosedByInterruptException |
|
197 |
* If the thread blocked in the I/O operation was interrupted |
|
198 |
*/ |
|
199 |
protected final void end(boolean completed) |
|
200 |
throws AsynchronousCloseException |
|
201 |
{ |
|
202 |
blockedOn(null); |
|
7177
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
203 |
Thread interrupted = this.interrupted; |
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
204 |
if (interrupted != null && interrupted == Thread.currentThread()) { |
27757
0317e4973b42
8065720: (ch) AbstractInterruptibleChannel.end sets interrupted to null
alanb
parents:
25859
diff
changeset
|
205 |
this.interrupted = null; |
7177
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
206 |
throw new ClosedByInterruptException(); |
2 | 207 |
} |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32834
diff
changeset
|
208 |
if (!completed && closed) |
7177
0113db4feebc
6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents:
5506
diff
changeset
|
209 |
throw new AsynchronousCloseException(); |
2 | 210 |
} |
211 |
||
212 |
||
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49001
diff
changeset
|
213 |
// -- jdk.internal.access.SharedSecrets -- |
2 | 214 |
static void blockedOn(Interruptible intr) { // package-private |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
47216
diff
changeset
|
215 |
SharedSecrets.getJavaLangAccess().blockedOn(intr); |
2 | 216 |
} |
217 |
} |