1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 * All rights reserved. This program and the accompanying materials are
4 * made available under the terms of the Eclipse Public License v1.0 which
5 * accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Alexandre Montplaisir - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.internal
.tmf
.core
.statesystem
.backends
.partial
;
14 import java
.util
.List
;
15 import java
.util
.concurrent
.CountDownLatch
;
16 import java
.util
.concurrent
.locks
.Lock
;
17 import java
.util
.concurrent
.locks
.ReentrantLock
;
19 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.AttributeTree
;
20 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.StateSystem
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.NullBackend
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
27 * State system interface-like extension to use with partial state histories.
29 * It mainly exposes the {@link #replaceOngoingState} method, which allows
30 * seeking the state system to a different point by updating its "ongoing" state
33 * @author Alexandre Montplaisir
35 @SuppressWarnings("restriction") /* We're using AttributeTree directly */
36 public class PartialStateSystem
extends StateSystem
{
38 private static final String ERR_MSG
= "Partial state system should not modify the attribute tree!"; //$NON-NLS-1$
40 private final CountDownLatch ssAssignedLatch
= new CountDownLatch(1);
41 private final Lock queryLock
= new ReentrantLock();
44 * Reference to the real upstream state system. This is used so we can read
47 private StateSystem realStateSystem
= null;
52 public PartialStateSystem() {
54 * We use a Null back end here : we only use this state system for its
55 * "ongoing" values, so no need to save the changes that are inserted.
57 super(new NullBackend("partial")); //$NON-NLS-1$
61 * Assign the upstream state system to this one.
64 * The real state system
66 public void assignUpstream(StateSystem ss
) {
68 ssAssignedLatch
.countDown();
71 ITmfStateSystem
getUpstreamSS() {
72 return realStateSystem
;
75 // ------------------------------------------------------------------------
76 // Publicized non-API methods
77 // ------------------------------------------------------------------------
80 public void replaceOngoingState(List
<ITmfStateInterval
> ongoingIntervals
) {
81 super.replaceOngoingState(ongoingIntervals
);
85 public synchronized void dispose() {
89 // ------------------------------------------------------------------------
90 // Methods regarding the query lock
91 // ------------------------------------------------------------------------
94 * Take this inner state system's lock before doing a query.
96 * When doing queries, you should take the lock, then run
97 * {@link #replaceOngoingState}, then send events to its state provider
98 * input to cause state changes, and then call {@link #queryOngoingState} to
99 * get the states at the new "current time".
101 * Only after all that it would be safe to release the lock.
103 public void takeQueryLock() {
105 queryLock
.lockInterruptibly();
106 } catch (InterruptedException e
) {
112 * Release the query lock, when you are done with your query.
114 public void releaseQueryLock() {
119 public AttributeTree
getAttributeTree() {
121 return realStateSystem
.getAttributeTree();
125 * Override these methods to make sure we don't try to overwrite the
126 * "real" upstream attribute tree.
130 public void addEmptyAttribute() {
131 throw new RuntimeException(ERR_MSG
);
135 public int getQuarkAbsoluteAndAdd(String
... attribute
) {
138 return realStateSystem
.getQuarkAbsolute(attribute
);
139 } catch (AttributeNotFoundException e
) {
140 throw new RuntimeException(ERR_MSG
);
145 public int getQuarkRelativeAndAdd(int startingNodeQuark
, String
... subPath
) {
148 return realStateSystem
.getQuarkRelative(startingNodeQuark
, subPath
);
149 } catch (AttributeNotFoundException e
) {
150 throw new RuntimeException(ERR_MSG
);
154 private void waitUntilReady() {
156 ssAssignedLatch
.await();
157 } catch (InterruptedException e
) {