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 boolean isTimed = false;
private long startTime = 0, lockTime = 0; private long startTime = 0, lockTime = 0;
private boolean relocking = false;
/** /**
* Creates a Lock without default state * Creates a Lock without default state
@ -56,18 +57,21 @@ public class Lock extends SimpleLock {
*/ */
public synchronized void waitHere() { public synchronized void waitHere() {
if(!isTimed) { if(!isTimed) {
super.waitHere(); while(relocking)
super.waitHere();
return; return;
} }
if(!super.isLocked()) if(!super.isLocked())
return; return;
long wt = timeLeft0(); while(relocking) {
if(wt == 0) { long wt = timeLeft0();
unlock(); if(wt == 0) {
return; unlock();
return;
}
super.waitHere(wt);
} }
super.waitHere(wt);
} }
/** /**
@ -77,25 +81,39 @@ public class Lock extends SimpleLock {
public synchronized void waitHere(int timeout) { public synchronized void waitHere(int timeout) {
if(timeout == 0) if(timeout == 0)
return; return;
long start = System.currentTimeMillis();
if(!isTimed) { if(!isTimed) {
super.waitHere(timeout); while(relocking) {
long sectionOffset = System.currentTimeMillis() - start;
long wt = timeout - sectionOffset;
if(wt == 0)
break;
super.waitHere(wt);
}
return; return;
} }
if(!super.isLocked()) if(!super.isLocked())
return; return;
long wt = timeLeft0(); while(relocking) {
if(wt == 0) { long wt = timeLeft0();
unlock(); if(wt == 0) {
return; 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 * Unlock manually
*/ */
public synchronized void unlock() { public synchronized void unlock() {
relocking = false;
super.unlock(); super.unlock();
startTime = 0; startTime = 0;
lockTime = 0; lockTime = 0;
@ -105,9 +123,12 @@ public class Lock extends SimpleLock {
* Lock until manually unlocked * Lock until manually unlocked
*/ */
public synchronized void lock() { public synchronized void lock() {
relocking = super.isLocked();
startTime = System.currentTimeMillis(); startTime = System.currentTimeMillis();
isTimed = false; isTimed = false;
super.lock(); super.lock();
if(relocking)
notifyAll();
} }
/** /**
@ -118,10 +139,13 @@ public class Lock extends SimpleLock {
if(time == 0) if(time == 0)
return; return;
relocking = super.isLocked();
startTime = System.currentTimeMillis(); startTime = System.currentTimeMillis();
lockTime = time; lockTime = time;
isTimed = true; isTimed = true;
super.lock(); super.lock();
if(relocking)
notifyAll();
} }
/** /**