X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Frepository%2FLock.java;h=a19870be75fa16be3060eeae5d9745cf8b4c3526;hb=bd30ef4a1c87d27491bcbf8b60bd6627bec85a48;hp=ff3f331049239fdd5cacc5b2023c18904c49b939;hpb=3e8bccd59c63f424052fcef5930e94a6629a1a95;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/Lock.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/Lock.java index ff3f3310..a19870be 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/repository/Lock.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Lock.java @@ -59,11 +59,16 @@ public class Lock { @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()); } } @@ -74,14 +79,9 @@ public class Lock { } @SuppressWarnings("java:S2274") - private synchronized void waitForNewEntries() { - try { - if (this.lockRequestQueue.isEmpty()) { - this.wait(); - } - } catch (InterruptedException e) { - logger.warn("waitForUnlock interrupted", e); - Thread.currentThread().interrupt(); + private synchronized void waitForNewEntries() throws InterruptedException { + if (this.lockRequestQueue.isEmpty()) { + this.wait(); } } } @@ -117,7 +117,7 @@ public class Lock { synchronized (this) { if (lockCounter <= 0) { lockCounter = -1; // Might as well stop, to make it easier to find the problem - throw new NullPointerException("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) { @@ -130,7 +130,8 @@ public class Lock { @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 */ @@ -166,9 +167,10 @@ public class Lock { private synchronized void addToQueue(MonoSink callback, LockType lockType) { lockRequestQueue.add(new LockRequest(callback, lockType, this)); + processQueuedEntries(); } - @SuppressWarnings("java:S2274") + @SuppressWarnings("java:S2274") // Always invoke wait() and await() methods inside a loop private synchronized void waitForUnlock() { try { this.wait();