From 16576a7e413898b7f3a60ce3512ea30afac388ef Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Tue, 30 Oct 2012 10:38:49 -0400 Subject: [PATCH] tmf: Allow waiting for a state system's completion 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 Reviewed-on: https://git.eclipse.org/r/8427 Tested-by: Hudson CI --- .../tmf/core/statesystem/StateSystem.java | 15 +++++++++++++++ .../tmf/core/statesystem/ITmfStateSystem.java | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java index c4b7029f41..b302e49d56 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java @@ -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 */ } //-------------------------------------------------------------------------- diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java index 7cb9598be7..91601d48c4 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java @@ -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 -- 2.34.1