diff --git a/src/main/java/de/tudbut/tools/Lock.java b/src/main/java/de/tudbut/tools/Lock.java index d6eeb43..5622171 100644 --- a/src/main/java/de/tudbut/tools/Lock.java +++ b/src/main/java/de/tudbut/tools/Lock.java @@ -7,6 +7,7 @@ public class Lock extends SimpleLock { private boolean isTimed = false; private long startTime = 0, lockTime = 0; + private boolean relocking = false; /** * Creates a Lock without default state @@ -56,18 +57,21 @@ public class Lock extends SimpleLock { */ public synchronized void waitHere() { if(!isTimed) { - super.waitHere(); + while(relocking) + super.waitHere(); return; } if(!super.isLocked()) return; - long wt = timeLeft0(); - if(wt == 0) { - unlock(); - return; + while(relocking) { + long wt = timeLeft0(); + if(wt == 0) { + unlock(); + return; + } + super.waitHere(wt); } - super.waitHere(wt); } /** @@ -77,25 +81,39 @@ public class Lock extends SimpleLock { public synchronized void waitHere(int timeout) { if(timeout == 0) return; + long start = System.currentTimeMillis(); if(!isTimed) { - super.waitHere(timeout); + while(relocking) { + long sectionOffset = System.currentTimeMillis() - start; + long wt = timeout - sectionOffset; + if(wt == 0) + break; + super.waitHere(wt); + } return; } if(!super.isLocked()) return; - long wt = timeLeft0(); - if(wt == 0) { - unlock(); - return; + while(relocking) { + long wt = timeLeft0(); + if(wt == 0) { + unlock(); + return; + } + long sectionOffset = System.currentTimeMillis() - start; + wt = Math.min(wt, timeout - sectionOffset); + if(wt == 0) + break; + super.waitHere(wt); } - super.waitHere(Math.min(wt, timeout)); } /** * Unlock manually */ public synchronized void unlock() { + relocking = false; super.unlock(); startTime = 0; lockTime = 0; @@ -105,9 +123,12 @@ public class Lock extends SimpleLock { * Lock until manually unlocked */ public synchronized void lock() { + relocking = super.isLocked(); startTime = System.currentTimeMillis(); isTimed = false; super.lock(); + if(relocking) + notifyAll(); } /** @@ -118,10 +139,13 @@ public class Lock extends SimpleLock { if(time == 0) return; + relocking = super.isLocked(); startTime = System.currentTimeMillis(); lockTime = time; isTimed = true; super.lock(); + if(relocking) + notifyAll(); } /**