]> source.dussan.org Git - jgit.git/commitdiff
Remove unnecessary region locking from LockFile 85/1885/2
authorShawn O. Pearce <spearce@spearce.org>
Thu, 11 Nov 2010 01:28:14 +0000 (17:28 -0800)
committerShawn O. Pearce <spearce@spearce.org>
Fri, 12 Nov 2010 21:38:06 +0000 (13:38 -0800)
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>
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java

index a794ec330241acb45d2156b5633756613555cb09..fc464cefe947cfd625262eb4686760bf10ec631c 100644 (file)
@@ -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) {