private boolean isExclusive = false;
private int lockCounter = 0;
private final List<LockRequest> lockRequestQueue = new LinkedList<>();
+ private static AsynchCallbackExecutor callbackProcessor = new AsynchCallbackExecutor();
private static class AsynchCallbackExecutor implements Runnable {
private List<LockRequest> lockRequestQueue = new LinkedList<>();
@Override
public void run() {
- while (true) {
- for (LockRequest request : consume()) {
- request.callback.success(request.lock);
+ try {
+ while (true) {
+ for (LockRequest request : consume()) {
+ request.callback.success(request.lock);
+ }
+ waitForNewEntries();
}
- waitForNewEntries();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ logger.error("Interrupted {}", e.getMessage());
}
}
return q;
}
- private synchronized void waitForNewEntries() {
- try {
- if (this.lockRequestQueue.isEmpty()) {
- this.wait();
- }
- } catch (InterruptedException e) {
- logger.warn("waitForUnlock interrupted", e);
+ @SuppressWarnings("java:S2274")
+ private synchronized void waitForNewEntries() throws InterruptedException {
+ if (this.lockRequestQueue.isEmpty()) {
+ this.wait();
}
}
}
- private static AsynchCallbackExecutor callbackProcessor = new AsynchCallbackExecutor();
-
- public static enum LockType {
+ public enum LockType {
EXCLUSIVE, SHARED
}
synchronized (this) {
if (lockCounter <= 0) {
lockCounter = -1; // Might as well stop, to make it easier to find the problem
- throw new RuntimeException("Number of unlocks must match the number of locks");
+ logger.error("Number of unlocks must match the number of locks");
}
this.lockCounter--;
if (lockCounter == 0) {
@Override
public String toString() {
- return "Lock cnt: " + this.lockCounter + " exclusive: " + this.isExclusive;
+ return "Lock cnt: " + this.lockCounter + " exclusive: " + this.isExclusive + " queued: "
+ + this.lockRequestQueue.size();
}
/** returns the current number of granted locks */
}
}
}
-
- /*
- * for (LockRequest request : granted) { request.callback.success(this); }
- */
callbackProcessor.addAll(granted);
}
lockRequestQueue.add(new LockRequest(callback, lockType, this));
}
- private void waitForUnlock() {
+ @SuppressWarnings("java:S2274") // Always invoke wait() and await() methods inside a loop
+ private synchronized void waitForUnlock() {
try {
this.wait();
} catch (InterruptedException e) {
logger.warn("waitForUnlock interrupted", e);
+ Thread.currentThread().interrupt();
}
}