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
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.AttributeTree
;
21 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.StateSystem
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
28 * State system interface-like extension to use with partial state histories.
30 * It mainly exposes the {@link #replaceOngoingState} method, which allows
31 * seeking the state system to a different point by updating its "ongoing" state
34 * @author Alexandre Montplaisir
36 @SuppressWarnings("restriction") /* We're using AttributeTree directly */
37 public class PartialStateSystem
extends StateSystem
{
39 private static final String ERR_MSG
= "Partial state system should not modify the attribute tree!"; //$NON-NLS-1$
41 private final CountDownLatch ssAssignedLatch
= new CountDownLatch(1);
42 private final Lock queryLock
= new ReentrantLock();
45 * Reference to the real upstream state system. This is used so we can read
48 private StateSystem realStateSystem
= null;
53 public PartialStateSystem() {
55 * We use a Null back end here : we only use this state system for its
56 * "ongoing" values, so no need to save the changes that are inserted.
58 super(StateHistoryBackendFactory
.createNullBackend("partial")); //$NON-NLS-1$
62 * Assign the upstream state system to this one.
65 * The real state system
67 public void assignUpstream(StateSystem ss
) {
69 ssAssignedLatch
.countDown();
72 ITmfStateSystem
getUpstreamSS() {
73 return realStateSystem
;
76 // ------------------------------------------------------------------------
77 // Publicized non-API methods
78 // ------------------------------------------------------------------------
81 public void replaceOngoingState(List
<@NonNull ITmfStateInterval
> ongoingIntervals
) {
82 super.replaceOngoingState(ongoingIntervals
);
86 public synchronized void dispose() {
90 // ------------------------------------------------------------------------
91 // Methods regarding the query lock
92 // ------------------------------------------------------------------------
95 * Take this inner state system's lock before doing a query.
97 * When doing queries, you should take the lock, then run
98 * {@link #replaceOngoingState}, then send events to its state provider
99 * input to cause state changes, and then call {@link #queryOngoingState} to
100 * get the states at the new "current time".
102 * Only after all that it would be safe to release the lock.
104 public void takeQueryLock() {
106 queryLock
.lockInterruptibly();
107 } catch (InterruptedException e
) {
113 * Release the query lock, when you are done with your query.
115 public void releaseQueryLock() {
120 public AttributeTree
getAttributeTree() {
122 return realStateSystem
.getAttributeTree();
126 * Override these methods to make sure we don't try to overwrite the
127 * "real" upstream attribute tree.
131 public void addEmptyAttribute() {
132 throw new RuntimeException(ERR_MSG
);
136 public int getQuarkAbsoluteAndAdd(String
... attribute
) {
139 return realStateSystem
.getQuarkAbsolute(attribute
);
140 } catch (AttributeNotFoundException e
) {
141 throw new RuntimeException(ERR_MSG
);
146 public int getQuarkRelativeAndAdd(int startingNodeQuark
, String
... subPath
) {
149 return realStateSystem
.getQuarkRelative(startingNodeQuark
, subPath
);
150 } catch (AttributeNotFoundException e
) {
151 throw new RuntimeException(ERR_MSG
);
155 private void waitUntilReady() {
157 ssAssignedLatch
.await();
158 } catch (InterruptedException e
) {