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,12 +57,14 @@ public class Lock extends SimpleLock {
*/ */
public synchronized void waitHere() { public synchronized void waitHere() {
if(!isTimed) { if(!isTimed) {
while(relocking)
super.waitHere(); super.waitHere();
return; return;
} }
if(!super.isLocked()) if(!super.isLocked())
return; return;
while(relocking) {
long wt = timeLeft0(); long wt = timeLeft0();
if(wt == 0) { if(wt == 0) {
unlock(); unlock();
@ -69,6 +72,7 @@ public class Lock extends SimpleLock {
} }
super.waitHere(wt); super.waitHere(wt);
} }
}
/** /**
* Wait until unlocked, either by a timer, manually, or when it waited for timeout * Wait until unlocked, either by a timer, manually, or when it waited for timeout
@ -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;
while(relocking) {
long wt = timeLeft0(); long wt = timeLeft0();
if(wt == 0) { if(wt == 0) {
unlock(); unlock();
return; return;
} }
super.waitHere(Math.min(wt, timeout)); long sectionOffset = System.currentTimeMillis() - start;
wt = Math.min(wt, timeout - sectionOffset);
if(wt == 0)
break;
super.waitHere(wt);
}
} }
/** /**
* 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();
} }
/** /**