Browse Source

Remove unnecessary region locking from LockFile

The lock file protocol relies on the atomic creation of a standardized
name in the parent directory of the file being updated.  Since the
creation is atomic, at most one thread in any process can succeed on
this creation, and all others will fail.  While the lock file exists,
that file is private to the thread that is writing it, and no others
will attempt to read or modify the file.

Consequently the use of the region level locks around the file are
unnecessary, and may actually reduce performance when using NFS, SMB,
or some other sort of remote filesystem that supports locking.

Change-Id: Ice312b6fb4fdf9d36c734c3624c6d0537903913b
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
stable-0.10
Shawn O. Pearce 14 years ago
parent
commit
ed5fe8af9a
  1. 31
      org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java

31
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java

@ -54,8 +54,6 @@ import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.text.MessageFormat;
import org.eclipse.jgit.JGitText;
@ -87,8 +85,6 @@ public class LockFile {
private final File lck;
private FileLock fLck;
private boolean haveLck;
private FileOutputStream os;
@ -131,23 +127,6 @@ public class LockFile {
haveLck = true;
try {
os = new FileOutputStream(lck);
try {
fLck = os.getChannel().tryLock();
if (fLck == null)
throw new OverlappingFileLockException();
} catch (OverlappingFileLockException ofle) {
// We cannot use unlock() here as this file is not
// held by us, but we thought we created it. We must
// not delete it, as it belongs to some other process.
//
haveLck = false;
try {
os.close();
} catch (IOException ioe) {
// Fail by returning haveLck = false.
}
os = null;
}
} catch (IOException ioe) {
unlock();
throw ioe;
@ -276,7 +255,6 @@ public class LockFile {
} else {
os.write(content);
}
fLck.release();
os.close();
os = null;
} catch (IOException ioe) {
@ -337,7 +315,6 @@ public class LockFile {
out.flush();
if (fsync)
os.getChannel().force(true);
fLck.release();
out.close();
os = null;
} catch (IOException ioe) {
@ -472,14 +449,6 @@ public class LockFile {
*/
public void unlock() {
if (os != null) {
if (fLck != null) {
try {
fLck.release();
} catch (IOException ioe) {
// Huh?
}
fLck = null;
}
try {
os.close();
} catch (IOException ioe) {

Loading…
Cancel
Save