summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-08-23 09:54:38 -0400
committerJim Hahn <jrh3@att.com>2021-08-23 09:56:14 -0400
commit4564d7b3e8bad18432c817eb780400b220451f1a (patch)
tree4d6305ebe303b7323a0006fd3c889ffb03523b32
parent5d79140dbdaa85c390c3e8f6914739c2c8a6b130 (diff)
Prevent deadlock in ServiceManager
Modified the ServiceManager code so that isAlive() can be invoked without requiring synchronization, thus eliminating one potential area of deadlock. Issue-ID: POLICY-3531 Change-Id: I27d060c3a7cfad8dab20a197d1e42c4ee607a1e2 Signed-off-by: Jim Hahn <jrh3@att.com>
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java19
1 files changed, 10 insertions, 9 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java b/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java
index 1387462f..67a588fe 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java
@@ -23,6 +23,7 @@ package org.onap.policy.common.utils.services;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
+import java.util.concurrent.atomic.AtomicBoolean;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.onap.policy.common.capabilities.Startable;
@@ -50,7 +51,7 @@ public class ServiceManager implements Startable {
/**
* {@code True} if the services are currently running, {@code false} otherwise.
*/
- private boolean running;
+ private final AtomicBoolean running = new AtomicBoolean(false);
/**
* Constructs the object, with a default name.
@@ -77,7 +78,7 @@ public class ServiceManager implements Startable {
* @return this manager
*/
public synchronized ServiceManager addAction(String stepName, RunnableWithEx starter, RunnableWithEx stopper) {
- if (running) {
+ if (isAlive()) {
throw new IllegalStateException(name + " is already running; cannot add " + stepName);
}
@@ -94,7 +95,7 @@ public class ServiceManager implements Startable {
* @return this manager
*/
public synchronized ServiceManager addService(String stepName, Startable service) {
- if (running) {
+ if (isAlive()) {
throw new IllegalStateException(name + " is already running; cannot add " + stepName);
}
@@ -103,13 +104,13 @@ public class ServiceManager implements Startable {
}
@Override
- public synchronized boolean isAlive() {
- return running;
+ public boolean isAlive() {
+ return running.get();
}
@Override
public synchronized boolean start() {
- if (running) {
+ if (isAlive()) {
throw new IllegalStateException(name + " is already running");
}
@@ -134,7 +135,7 @@ public class ServiceManager implements Startable {
if (ex == null) {
logger.info("{} started", name);
- running = true;
+ running.set(true);
return true;
}
@@ -151,11 +152,11 @@ public class ServiceManager implements Startable {
@Override
public synchronized boolean stop() {
- if (!running) {
+ if (!isAlive()) {
throw new IllegalStateException(name + " is not running");
}
- running = false;
+ running.set(false);
rewind(items);
return true;