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=ed944924ca037963eea8127962584e4c3150cac0;hb=refs%2Fchanges%2F07%2F3307%2F5;hp=bc5d77a32ef0db0655fc4591b2af26577a22327a;hpb=14accd2e91460d1651fe2c228fe1ba964cbfb6a6;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 bc5d77a3..ed944924 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 @@ -42,6 +42,7 @@ public class Lock { private boolean isExclusive = false; private int lockCounter = 0; private final List lockRequestQueue = new LinkedList<>(); + private static AsynchCallbackExecutor callbackProcessor = new AsynchCallbackExecutor(); private static class AsynchCallbackExecutor implements Runnable { private List lockRequestQueue = new LinkedList<>(); @@ -58,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()); } } @@ -72,20 +78,15 @@ public class Lock { 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 } @@ -116,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 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) { @@ -129,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 */ @@ -148,10 +150,6 @@ public class Lock { } } } - - /* - * for (LockRequest request : granted) { request.callback.success(this); } - */ callbackProcessor.addAll(granted); } @@ -171,11 +169,13 @@ public class Lock { 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(); } }