SRADA-737: added wait/notify mechanism for DAState
authorVladislav Eliseev <v.eliseev@samsung.com>
Tue, 7 Jun 2016 08:50:33 +0000 (11:50 +0300)
committerMaria Guseva <m.guseva@samsung.com>
Fri, 1 Jul 2016 08:24:01 +0000 (17:24 +0900)
Now DAState notifies all waiters that are waiting
for DAState.DONE state change.

Change-Id: I43d23468de62472650bf5fbeb77e80cf77d109eb

org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DAState.java

index e462bae..476ee85 100644 (file)
  */
 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;
@@ -39,11 +55,13 @@ public enum DAState {
        // 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:
@@ -73,10 +91,28 @@ public enum DAState {
                        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);
        }