mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-15 08:34:30 +02:00
8196535: Remove support for pre-Java 6 non-JVM-wide file locking
Reviewed-by: alanb, rriggs
This commit is contained in:
parent
7f04dec594
commit
5f7839ae5a
3 changed files with 30 additions and 150 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -136,7 +136,7 @@ abstract class AsynchronousFileChannelImpl
|
||||||
if (fileLockTable == null) {
|
if (fileLockTable == null) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (fileLockTable == null) {
|
if (fileLockTable == null) {
|
||||||
fileLockTable = FileLockTable.newSharedFileLockTable(this, fdObj);
|
fileLockTable = new FileLockTable(this, fdObj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -1083,49 +1083,19 @@ public class FileChannelImpl
|
||||||
|
|
||||||
// -- Locks --
|
// -- Locks --
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// keeps track of locks on this file
|
// keeps track of locks on this file
|
||||||
private volatile FileLockTable fileLockTable;
|
private volatile FileLockTable fileLockTable;
|
||||||
|
|
||||||
// indicates if file locks are maintained system-wide (as per spec)
|
|
||||||
private static boolean isSharedFileLockTable;
|
|
||||||
|
|
||||||
// indicates if the disableSystemWideOverlappingFileLockCheck property
|
|
||||||
// has been checked
|
|
||||||
private static volatile boolean propertyChecked;
|
|
||||||
|
|
||||||
// The lock list in J2SE 1.4/5.0 was local to each FileChannel instance so
|
|
||||||
// the overlap check wasn't system wide when there were multiple channels to
|
|
||||||
// the same file. This property is used to get 1.4/5.0 behavior if desired.
|
|
||||||
private static boolean isSharedFileLockTable() {
|
|
||||||
if (!propertyChecked) {
|
|
||||||
synchronized (FileChannelImpl.class) {
|
|
||||||
if (!propertyChecked) {
|
|
||||||
String value = GetPropertyAction.privilegedGetProperty(
|
|
||||||
"sun.nio.ch.disableSystemWideOverlappingFileLockCheck");
|
|
||||||
isSharedFileLockTable = ((value == null) || value.equals("false"));
|
|
||||||
propertyChecked = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return isSharedFileLockTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
private FileLockTable fileLockTable() throws IOException {
|
private FileLockTable fileLockTable() throws IOException {
|
||||||
if (fileLockTable == null) {
|
if (fileLockTable == null) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (fileLockTable == null) {
|
if (fileLockTable == null) {
|
||||||
if (isSharedFileLockTable()) {
|
int ti = threads.add();
|
||||||
int ti = threads.add();
|
try {
|
||||||
try {
|
ensureOpen();
|
||||||
ensureOpen();
|
fileLockTable = new FileLockTable(this, fd);
|
||||||
fileLockTable = FileLockTable.newSharedFileLockTable(this, fd);
|
} finally {
|
||||||
} finally {
|
threads.remove(ti);
|
||||||
threads.remove(ti);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fileLockTable = new SimpleFileLockTable();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1229,59 +1199,6 @@ public class FileChannelImpl
|
||||||
fileLockTable.remove(fli);
|
fileLockTable.remove(fli);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- File lock support --
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple file lock table that maintains a list of FileLocks obtained by a
|
|
||||||
* FileChannel. Use to get 1.4/5.0 behaviour.
|
|
||||||
*/
|
|
||||||
private static class SimpleFileLockTable extends FileLockTable {
|
|
||||||
// synchronize on list for access
|
|
||||||
private final List<FileLock> lockList = new ArrayList<FileLock>(2);
|
|
||||||
|
|
||||||
public SimpleFileLockTable() {
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkList(long position, long size)
|
|
||||||
throws OverlappingFileLockException
|
|
||||||
{
|
|
||||||
assert Thread.holdsLock(lockList);
|
|
||||||
for (FileLock fl: lockList) {
|
|
||||||
if (fl.overlaps(position, size)) {
|
|
||||||
throw new OverlappingFileLockException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void add(FileLock fl) throws OverlappingFileLockException {
|
|
||||||
synchronized (lockList) {
|
|
||||||
checkList(fl.position(), fl.size());
|
|
||||||
lockList.add(fl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove(FileLock fl) {
|
|
||||||
synchronized (lockList) {
|
|
||||||
lockList.remove(fl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<FileLock> removeAll() {
|
|
||||||
synchronized(lockList) {
|
|
||||||
List<FileLock> result = new ArrayList<FileLock>(lockList);
|
|
||||||
lockList.clear();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void replace(FileLock fl1, FileLock fl2) {
|
|
||||||
synchronized (lockList) {
|
|
||||||
lockList.remove(fl1);
|
|
||||||
lockList.add(fl2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// -- Native methods --
|
// -- Native methods --
|
||||||
|
|
||||||
// Creates a new mapping
|
// Creates a new mapping
|
||||||
|
|
|
@ -25,64 +25,27 @@
|
||||||
|
|
||||||
package sun.nio.ch;
|
package sun.nio.ch;
|
||||||
|
|
||||||
import java.nio.channels.*;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.lang.ref.*;
|
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.ref.ReferenceQueue;
|
||||||
abstract class FileLockTable {
|
import java.lang.ref.WeakReference;
|
||||||
protected FileLockTable() {
|
import java.nio.channels.Channel;
|
||||||
}
|
import java.nio.channels.FileLock;
|
||||||
|
import java.nio.channels.OverlappingFileLockException;
|
||||||
/**
|
import java.util.ArrayList;
|
||||||
* Creates and returns a file lock table for a channel that is connected to
|
import java.util.HashSet;
|
||||||
* the a system-wide map of all file locks for the Java virtual machine.
|
import java.util.List;
|
||||||
*/
|
import java.util.Set;
|
||||||
public static FileLockTable newSharedFileLockTable(Channel channel,
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
FileDescriptor fd)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
return new SharedFileLockTable(channel, fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a file lock to the table.
|
|
||||||
*
|
|
||||||
* @throws OverlappingFileLockException if the file lock overlaps
|
|
||||||
* with an existing file lock in the table
|
|
||||||
*/
|
|
||||||
public abstract void add(FileLock fl) throws OverlappingFileLockException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an existing file lock from the table.
|
|
||||||
*/
|
|
||||||
public abstract void remove(FileLock fl);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes all file locks from the table.
|
|
||||||
*
|
|
||||||
* @return The list of file locks removed
|
|
||||||
*/
|
|
||||||
public abstract List<FileLock> removeAll();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Replaces an existing file lock in the table.
|
|
||||||
*/
|
|
||||||
public abstract void replace(FileLock fl1, FileLock fl2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A file lock table that is over a system-wide map of all file locks.
|
* A file lock table that is over a system-wide map of all file locks.
|
||||||
*/
|
*/
|
||||||
class SharedFileLockTable extends FileLockTable {
|
class FileLockTable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A weak reference to a FileLock.
|
* A weak reference to a FileLock.
|
||||||
* <p>
|
* <p>
|
||||||
* SharedFileLockTable uses a list of file lock references to avoid keeping the
|
* FileLockTable uses a list of file lock references to avoid keeping the
|
||||||
* FileLock (and FileChannel) alive.
|
* FileLock (and FileChannel) alive.
|
||||||
*/
|
*/
|
||||||
private static class FileLockReference extends WeakReference<FileLock> {
|
private static class FileLockReference extends WeakReference<FileLock> {
|
||||||
|
@ -118,14 +81,17 @@ class SharedFileLockTable extends FileLockTable {
|
||||||
// Locks obtained for this channel
|
// Locks obtained for this channel
|
||||||
private final Set<FileLock> locks;
|
private final Set<FileLock> locks;
|
||||||
|
|
||||||
SharedFileLockTable(Channel channel, FileDescriptor fd) throws IOException {
|
/**
|
||||||
|
* Creates a file lock table for a channel that is connected to the
|
||||||
|
* system-wide map of all file locks for the Java virtual machine.
|
||||||
|
*/
|
||||||
|
FileLockTable(Channel channel, FileDescriptor fd) throws IOException {
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
this.fileKey = FileKey.create(fd);
|
this.fileKey = FileKey.create(fd);
|
||||||
this.locks = new HashSet<FileLock>();
|
this.locks = new HashSet<FileLock>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
void add(FileLock fl) throws OverlappingFileLockException {
|
||||||
public void add(FileLock fl) throws OverlappingFileLockException {
|
|
||||||
List<FileLockReference> list = lockMap.get(fileKey);
|
List<FileLockReference> list = lockMap.get(fileKey);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
@ -176,8 +142,7 @@ class SharedFileLockTable extends FileLockTable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
void remove(FileLock fl) {
|
||||||
public void remove(FileLock fl) {
|
|
||||||
assert fl != null;
|
assert fl != null;
|
||||||
|
|
||||||
// the lock must exist so the list of locks must be present
|
// the lock must exist so the list of locks must be present
|
||||||
|
@ -201,8 +166,7 @@ class SharedFileLockTable extends FileLockTable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
List<FileLock> removeAll() {
|
||||||
public List<FileLock> removeAll() {
|
|
||||||
List<FileLock> result = new ArrayList<FileLock>();
|
List<FileLock> result = new ArrayList<FileLock>();
|
||||||
List<FileLockReference> list = lockMap.get(fileKey);
|
List<FileLockReference> list = lockMap.get(fileKey);
|
||||||
if (list != null) {
|
if (list != null) {
|
||||||
|
@ -234,8 +198,7 @@ class SharedFileLockTable extends FileLockTable {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
void replace(FileLock fromLock, FileLock toLock) {
|
||||||
public void replace(FileLock fromLock, FileLock toLock) {
|
|
||||||
// the lock must exist so there must be a list
|
// the lock must exist so there must be a list
|
||||||
List<FileLockReference> list = lockMap.get(fileKey);
|
List<FileLockReference> list = lockMap.get(fileKey);
|
||||||
assert list != null;
|
assert list != null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue