tmf: Allow waiting for a state system's completion
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Tue, 30 Oct 2012 14:38:49 +0000 (10:38 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Tue, 30 Oct 2012 21:05:41 +0000 (17:05 -0400)
ITmfStateSystem.waitUntilBuilt() will now block the calling
thread until the state system underneath is finished building.
This will be immediately useful for statistics (intermediate
queries are useless, we need to wait until it's ready anyway).

It's implemented using a CountDownLatch, similar to the
.waitForCompletion() in event requests.

Change-Id: I98420acc3ca2903d76a315259f25dca7d88b8d42
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/8427
Tested-by: Hudson CI
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java

index c4b7029f415a10c6acc1265489df12804a10b841..b302e49d568efc239ab281388c409864a1052aa7 100644 (file)
@@ -18,6 +18,7 @@ import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
@@ -51,6 +52,9 @@ public class StateSystem implements ITmfStateSystemBuilder {
     private final TransientState transState;
     private final IStateHistoryBackend backend;
 
+    /* Latch tracking if the state history is done building or not */
+    private final CountDownLatch finishedLatch = new CountDownLatch(1);
+
     /**
      * General constructor
      *
@@ -73,6 +77,16 @@ public class StateSystem implements ITmfStateSystemBuilder {
             /* We're opening an existing file */
             this.attributeTree = new AttributeTree(this, backend.supplyAttributeTreeReader());
             transState.setInactive();
+            finishedLatch.countDown(); /* The history is already built */
+        }
+    }
+
+    @Override
+    public void waitUntilBuilt() {
+        try {
+            finishedLatch.await();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
         }
     }
 
@@ -147,6 +161,7 @@ public class StateSystem implements ITmfStateSystemBuilder {
              */
             attributeTree.writeSelf(attributeTreeFile, attributeTreeFilePos);
         }
+        finishedLatch.countDown(); /* Mark the history as finished building */
     }
 
     //--------------------------------------------------------------------------
index 7cb9598be7ba90f8f71af3ba2178c3d374c10576..91601d48c4c9b572823f69dbbb33b79c6e515d61 100644 (file)
@@ -46,6 +46,19 @@ public interface ITmfStateSystem {
      */
     public long getCurrentEndTime();
 
+    /**
+     * While it's possible to query a state history that is being built,
+     * sometimes we might want to wait until the construction is finished before
+     * we start doing queries.
+     *
+     * This method blocks the calling thread until the history back-end is done
+     * building. If it's already built (ie, opening a pre-existing file) this
+     * should return immediately. It's an alternative to listening to the
+     * {@link org.eclipse.linuxtools.tmf.core.signal.TmfStateSystemBuildCompleted}
+     * signal.
+     */
+    public void waitUntilBuilt();
+
     /**
      * Return the current total amount of attributes in the system. This is also
      * equal to the quark that will be assigned to the next attribute that's
This page took 0.026278 seconds and 5 git commands to generate.