make consecutive lock calls not break timing
All checks were successful
/ Build & Publish (push) Successful in 45s

This commit is contained in:
Daniella / Tove 2024-06-25 05:05:55 +02:00
parent bee64f9ee1
commit 70326b2947
Signed by: TudbuT
GPG key ID: B3CF345217F202D3

View file

@ -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();
}
/**