[Swift-commit] cog r3475

swift at ci.uchicago.edu swift at ci.uchicago.edu
Fri Sep 21 02:45:03 CDT 2012


------------------------------------------------------------------------
r3475 | hategan | 2012-09-21 02:44:51 -0500 (Fri, 21 Sep 2012) | 1 line

fixed file lock to work with concurrent threads in the same jvm
------------------------------------------------------------------------
Index: modules/util/src/org/globus/cog/util/concurrent/FileLock.java
===================================================================
--- modules/util/src/org/globus/cog/util/concurrent/FileLock.java	(revision 3474)
+++ modules/util/src/org/globus/cog/util/concurrent/FileLock.java	(working copy)
@@ -18,6 +18,8 @@
 import java.io.IOException;
 import java.security.SecureRandom;
 import java.util.Random;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.log4j.Logger;
 
@@ -26,7 +28,7 @@
  * It tries to use the PID as thread identifier with fallback to random numbers.
  *
  * The Entering and Number arrays are implemented as sparse arrays of
- * files in some directory.
+ * files in some directory. The lock is not re-entrant.
  */
 public class FileLock {    
     public static final Logger logger = Logger.getLogger(FileLock.class);
@@ -36,6 +38,8 @@
     
     private int myId, myN;
     private File dir;
+    private Lock jvmLocalLock;
+    private int lockCount;
     
     public FileLock(String dir) {
         this(new File(dir));
@@ -45,6 +49,7 @@
         this.dir = dir;
         dir.mkdirs();
         this.myId = getId();
+        jvmLocalLock = new ReentrantLock();
     }
 
     private int getId() {
@@ -65,6 +70,7 @@
     }
     
     public void lock() throws IOException, InterruptedException {
+        jvmLocalLock.lock();
         write(ENTERING, myId, 1);
         write(NUMBER, myId, myN = 1 + maxNumber());
         write(ENTERING, myId, 0);
@@ -185,6 +191,51 @@
     }
 
     public void unlock() throws IOException {
-        write("locking.number", myId, 0);
+        try {
+            write("locking.number", myId, 0);
+        }
+        finally {
+            jvmLocalLock.unlock();
+        }
     }
+    
+    public static void main(String[] args) {
+        String dir = args[0];
+        int delay = Integer.parseInt(args[1]);
+        final FileLock l = new FileLock(dir);
+        try {
+            System.out.println(". " + l.getId());
+            l.lock();
+            System.out.println("+ " + l.getId());
+            try {
+                if (Math.random() < 0.1) {
+                    new Thread() {
+
+                        @Override
+                        public void run() {
+                            try {
+                                System.out.println(". " + l.getId() + "x");
+                                l.lock();
+                                System.out.println("+ " + l.getId() + "x");
+                                l.unlock();
+                                System.out.println("- " + l.getId() + "x");
+                            }
+                            catch (Exception e) {
+                                e.printStackTrace();
+                            }
+                        }
+                    }.start();
+                }
+                Thread.sleep(delay);
+            }
+            finally {
+                l.unlock();
+                System.out.println("- " + l.getId());
+            }
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            System.exit(1);
+        }
+    }
 }



More information about the Swift-commit mailing list