8196535: Remove support for pre-Java 6 non-JVM-wide file locking

Reviewed-by: alanb, rriggs
This commit is contained in:
Brian Burkhalter 2018-02-05 15:08:46 -08:00
parent 7f04dec594
commit 5f7839ae5a
3 changed files with 30 additions and 150 deletions

View file

@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@ -136,7 +136,7 @@ abstract class AsynchronousFileChannelImpl
if (fileLockTable == null) {
synchronized (this) {
if (fileLockTable == null) {
fileLockTable = FileLockTable.newSharedFileLockTable(this, fdObj);
fileLockTable = new FileLockTable(this, fdObj);
}
}
}

View file

@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@ -1083,49 +1083,19 @@ public class FileChannelImpl
// -- Locks --
// keeps track of locks on this file
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 {
if (fileLockTable == null) {
synchronized (this) {
if (fileLockTable == null) {
if (isSharedFileLockTable()) {
int ti = threads.add();
try {
ensureOpen();
fileLockTable = FileLockTable.newSharedFileLockTable(this, fd);
} finally {
threads.remove(ti);
}
} else {
fileLockTable = new SimpleFileLockTable();
int ti = threads.add();
try {
ensureOpen();
fileLockTable = new FileLockTable(this, fd);
} finally {
threads.remove(ti);
}
}
}
@ -1229,59 +1199,6 @@ public class FileChannelImpl
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 --
// Creates a new mapping

View file

@ -25,64 +25,27 @@
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.IOException;
abstract class FileLockTable {
protected FileLockTable() {
}
/**
* Creates and returns a file lock table for a channel that is connected to
* the a system-wide map of all file locks for the Java virtual machine.
*/
public static FileLockTable newSharedFileLockTable(Channel channel,
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);
}
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.nio.channels.Channel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* 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.
* <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.
*/
private static class FileLockReference extends WeakReference<FileLock> {
@ -118,14 +81,17 @@ class SharedFileLockTable extends FileLockTable {
// Locks obtained for this channel
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.fileKey = FileKey.create(fd);
this.locks = new HashSet<FileLock>();
}
@Override
public void add(FileLock fl) throws OverlappingFileLockException {
void add(FileLock fl) throws OverlappingFileLockException {
List<FileLockReference> list = lockMap.get(fileKey);
for (;;) {
@ -176,8 +142,7 @@ class SharedFileLockTable extends FileLockTable {
}
}
@Override
public void remove(FileLock fl) {
void remove(FileLock fl) {
assert fl != null;
// the lock must exist so the list of locks must be present
@ -201,8 +166,7 @@ class SharedFileLockTable extends FileLockTable {
}
}
@Override
public List<FileLock> removeAll() {
List<FileLock> removeAll() {
List<FileLock> result = new ArrayList<FileLock>();
List<FileLockReference> list = lockMap.get(fileKey);
if (list != null) {
@ -234,8 +198,7 @@ class SharedFileLockTable extends FileLockTable {
return result;
}
@Override
public void replace(FileLock fromLock, FileLock toLock) {
void replace(FileLock fromLock, FileLock toLock) {
// the lock must exist so there must be a list
List<FileLockReference> list = lockMap.get(fileKey);
assert list != null;