mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8211859: Avoid initializing AtomicBoolean from RandomAccessFile
Reviewed-by: alanb
This commit is contained in:
parent
1ac444ad87
commit
02a3be9920
1 changed files with 14 additions and 5 deletions
|
@ -71,7 +71,9 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
|
||||||
*/
|
*/
|
||||||
private final String path;
|
private final String path;
|
||||||
|
|
||||||
private final AtomicBoolean closed = new AtomicBoolean(false);
|
private final Object closeLock = new Object();
|
||||||
|
|
||||||
|
private volatile boolean closed;
|
||||||
|
|
||||||
private static final int O_RDONLY = 1;
|
private static final int O_RDONLY = 1;
|
||||||
private static final int O_RDWR = 2;
|
private static final int O_RDWR = 2;
|
||||||
|
@ -301,7 +303,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
|
||||||
if (fc == null) {
|
if (fc == null) {
|
||||||
this.channel = fc = FileChannelImpl.open(fd, path, true,
|
this.channel = fc = FileChannelImpl.open(fd, path, true,
|
||||||
rw, false, this);
|
rw, false, this);
|
||||||
if (closed.get()) {
|
if (closed) {
|
||||||
try {
|
try {
|
||||||
fc.close();
|
fc.close();
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
|
@ -638,13 +640,20 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
|
||||||
* @spec JSR-51
|
* @spec JSR-51
|
||||||
*/
|
*/
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if (!closed.compareAndSet(false, true)) {
|
if (closed) {
|
||||||
// if compareAndSet() returns false closed was already true
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
synchronized (closeLock) {
|
||||||
|
if (closed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
closed = true;
|
||||||
|
}
|
||||||
|
|
||||||
FileChannel fc = channel;
|
FileChannel fc = channel;
|
||||||
if (fc != null) {
|
if (fc != null) {
|
||||||
|
// possible race with getChannel(), benign since
|
||||||
|
// FileChannel.close is final and idempotent
|
||||||
fc.close();
|
fc.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue