*/
package org.tizen.dynamicanalyzer.common;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
public enum DAState {
INIT, DONE, RUNNING, PREPARE_START, PREPARE_STOP, PREPARE_CANCEL;
- private static DAState currentState = INIT;
- private static final Object stateLock = new Object();
+ /**
+ * Current logical state of Dynamic Analyzer.
+ */
+ private static volatile DAState currentState = INIT;
+
+ /**
+ * Guard lock used to change state in multithreading environment.
+ */
+ private static final Lock stateLock = new ReentrantLock();
+
+ /**
+ * Condition lock used to notify waiters when state becomes DONE.
+ */
+ private static final Condition doneCondition = stateLock.newCondition();
public static DAState getCurrentState() {
return currentState;
// return false otherwise
public static boolean changeCurrentState(DAState state) {
boolean ret = false;
- synchronized (stateLock) {
+ stateLock.lock();
+ try {
switch (state) {
case INIT:
case DONE:
currentState = state;
+ doneCondition.signalAll();
ret = true;
break;
case RUNNING:
default:
break;
}
+ } finally {
+ stateLock.unlock();
}
return ret;
}
+ /**
+ * Blocks caller thread until DA state doesn't become DONE.
+ *
+ * @throws InterruptedException if waiting was interrupted
+ */
+ public static void waitDone() throws InterruptedException {
+ stateLock.lock();
+ try {
+ while (currentState != DONE) {
+ doneCondition.await();
+ }
+ } finally {
+ stateLock.unlock();
+ }
+ }
+
public static boolean isRunning() {
return (currentState == RUNNING);
}