[Swift-commit] cog r3476
swift at ci.uchicago.edu
swift at ci.uchicago.edu
Sat Sep 22 19:10:14 CDT 2012
------------------------------------------------------------------------
r3476 | hategan | 2012-09-22 19:07:51 -0500 (Sat, 22 Sep 2012) | 1 line
the previous fix only worked if the same FileLock object was used for a given directory; updated to allow locking to work properly even with different FileLock objects pointing to the same directory
------------------------------------------------------------------------
Index: modules/util/src/org/globus/cog/util/concurrent/FileLock.java
===================================================================
--- modules/util/src/org/globus/cog/util/concurrent/FileLock.java (revision 3475)
+++ modules/util/src/org/globus/cog/util/concurrent/FileLock.java (working copy)
@@ -17,9 +17,9 @@
import java.io.FileWriter;
import java.io.IOException;
import java.security.SecureRandom;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Random;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;
@@ -38,9 +38,14 @@
private int myId, myN;
private File dir;
- private Lock jvmLocalLock;
private int lockCount;
+ private static Map<File, Boolean> jvmLocalLocks;
+
+ static {
+ jvmLocalLocks = new HashMap<File, Boolean>();
+ }
+
public FileLock(String dir) {
this(new File(dir));
}
@@ -49,7 +54,6 @@
this.dir = dir;
dir.mkdirs();
this.myId = getId();
- jvmLocalLock = new ReentrantLock();
}
private int getId() {
@@ -70,7 +74,12 @@
}
public void lock() throws IOException, InterruptedException {
- jvmLocalLock.lock();
+ synchronized(jvmLocalLocks) {
+ while (jvmLocalLocks.containsKey(dir)) {
+ jvmLocalLocks.wait();
+ }
+ jvmLocalLocks.put(dir, Boolean.TRUE);
+ }
write(ENTERING, myId, 1);
write(NUMBER, myId, myN = 1 + maxNumber());
write(ENTERING, myId, 0);
@@ -195,12 +204,15 @@
write("locking.number", myId, 0);
}
finally {
- jvmLocalLock.unlock();
+ synchronized(jvmLocalLocks) {
+ jvmLocalLocks.remove(dir);
+ jvmLocalLocks.notifyAll();
+ }
}
}
public static void main(String[] args) {
- String dir = args[0];
+ final String dir = args[0];
int delay = Integer.parseInt(args[1]);
final FileLock l = new FileLock(dir);
try {
@@ -208,15 +220,17 @@
l.lock();
System.out.println("+ " + l.getId());
try {
- if (Math.random() < 0.1) {
+ while (Math.random() < 0.8) {
new Thread() {
@Override
public void run() {
try {
+ FileLock l = new FileLock(dir);
System.out.println(". " + l.getId() + "x");
l.lock();
System.out.println("+ " + l.getId() + "x");
+ Thread.sleep((int) (Math.random() * 5));
l.unlock();
System.out.println("- " + l.getId() + "x");
}
More information about the Swift-commit
mailing list