make consecutive lock calls not break timing
All checks were successful
/ Build & Publish (push) Successful in 53s
All checks were successful
/ Build & Publish (push) Successful in 53s
This commit is contained in:
parent
bee64f9ee1
commit
1f301d292e
|
@ -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
|
||||
|
@ -62,12 +63,14 @@ public class Lock extends SimpleLock {
|
|||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -84,18 +87,23 @@ public class Lock extends SimpleLock {
|
|||
|
||||
if(!super.isLocked())
|
||||
return;
|
||||
long wt = timeLeft0();
|
||||
if(wt == 0) {
|
||||
unlock();
|
||||
return;
|
||||
long start = System.currentTimeMillis();
|
||||
while(relocking) {
|
||||
long wt = timeLeft0();
|
||||
if(wt == 0) {
|
||||
unlock();
|
||||
return;
|
||||
}
|
||||
long sectionOffset = System.currentTimeMillis() - start;
|
||||
super.waitHere(Math.min(wt, timeout - sectionOffset));
|
||||
}
|
||||
super.waitHere(Math.min(wt, timeout));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock manually
|
||||
*/
|
||||
public synchronized void unlock() {
|
||||
relocking = false;
|
||||
super.unlock();
|
||||
startTime = 0;
|
||||
lockTime = 0;
|
||||
|
@ -105,9 +113,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 +129,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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue