Commit | Line | Data |
---|---|---|
1b9d3765 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
1b9d3765 AM |
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 | |
7 | * | |
8 | * Contributors: | |
9 | * Alexandre Montplaisir - Initial API and implementation | |
10 | *******************************************************************************/ | |
11 | ||
2bdf0193 | 12 | package org.eclipse.tracecompass.internal.tmf.core.statesystem.backends.partial; |
1b9d3765 AM |
13 | |
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; | |
18 | ||
df2597e0 | 19 | import org.eclipse.jdt.annotation.NonNull; |
e894a508 AM |
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; | |
0306a843 | 23 | import org.eclipse.tracecompass.statesystem.core.backend.StateHistoryBackendFactory; |
e894a508 AM |
24 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
25 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; | |
1b9d3765 AM |
26 | |
27 | /** | |
28 | * State system interface-like extension to use with partial state histories. | |
29 | * | |
30 | * It mainly exposes the {@link #replaceOngoingState} method, which allows | |
31 | * seeking the state system to a different point by updating its "ongoing" state | |
32 | * values. | |
33 | * | |
34 | * @author Alexandre Montplaisir | |
35 | */ | |
bcec0116 | 36 | @SuppressWarnings("restriction") /* We're using AttributeTree directly */ |
1b9d3765 AM |
37 | public class PartialStateSystem extends StateSystem { |
38 | ||
cb42195c | 39 | private static final String ERR_MSG = "Partial state system should not modify the attribute tree!"; //$NON-NLS-1$ |
1b9d3765 AM |
40 | |
41 | private final CountDownLatch ssAssignedLatch = new CountDownLatch(1); | |
42 | private final Lock queryLock = new ReentrantLock(); | |
43 | ||
44 | /** | |
45 | * Reference to the real upstream state system. This is used so we can read | |
46 | * its attribute tree. | |
47 | */ | |
48 | private StateSystem realStateSystem = null; | |
49 | ||
50 | /** | |
51 | * Constructor | |
52 | */ | |
53 | public PartialStateSystem() { | |
54 | /* | |
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. | |
57 | */ | |
0306a843 | 58 | super(StateHistoryBackendFactory.createNullBackend("partial")); //$NON-NLS-1$ |
1b9d3765 AM |
59 | } |
60 | ||
61 | /** | |
62 | * Assign the upstream state system to this one. | |
63 | * | |
64 | * @param ss | |
65 | * The real state system | |
66 | */ | |
67 | public void assignUpstream(StateSystem ss) { | |
68 | realStateSystem = ss; | |
69 | ssAssignedLatch.countDown(); | |
70 | } | |
71 | ||
72 | ITmfStateSystem getUpstreamSS() { | |
73 | return realStateSystem; | |
74 | } | |
75 | ||
3a755f44 AM |
76 | // ------------------------------------------------------------------------ |
77 | // Publicized non-API methods | |
78 | // ------------------------------------------------------------------------ | |
79 | ||
1b9d3765 | 80 | @Override |
df2597e0 | 81 | public void replaceOngoingState(List<@NonNull ITmfStateInterval> ongoingIntervals) { |
1b9d3765 AM |
82 | super.replaceOngoingState(ongoingIntervals); |
83 | } | |
84 | ||
3a755f44 AM |
85 | @Override |
86 | public synchronized void dispose() { | |
87 | super.dispose(); | |
88 | } | |
89 | ||
1b9d3765 AM |
90 | // ------------------------------------------------------------------------ |
91 | // Methods regarding the query lock | |
92 | // ------------------------------------------------------------------------ | |
93 | ||
94 | /** | |
95 | * Take this inner state system's lock before doing a query. | |
96 | * | |
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". | |
101 | * | |
102 | * Only after all that it would be safe to release the lock. | |
103 | */ | |
104 | public void takeQueryLock() { | |
105 | try { | |
106 | queryLock.lockInterruptibly(); | |
107 | } catch (InterruptedException e) { | |
108 | e.printStackTrace(); | |
109 | } | |
110 | } | |
111 | ||
112 | /** | |
113 | * Release the query lock, when you are done with your query. | |
114 | */ | |
115 | public void releaseQueryLock() { | |
116 | queryLock.unlock(); | |
117 | } | |
118 | ||
119 | @Override | |
120 | public AttributeTree getAttributeTree() { | |
121 | waitUntilReady(); | |
122 | return realStateSystem.getAttributeTree(); | |
123 | } | |
124 | ||
125 | /* | |
126 | * Override these methods to make sure we don't try to overwrite the | |
127 | * "real" upstream attribute tree. | |
128 | */ | |
129 | ||
130 | @Override | |
bcec0116 | 131 | public void addEmptyAttribute() { |
cb42195c | 132 | throw new RuntimeException(ERR_MSG); |
1b9d3765 AM |
133 | } |
134 | ||
135 | @Override | |
136 | public int getQuarkAbsoluteAndAdd(String... attribute) { | |
137 | waitUntilReady(); | |
138 | try { | |
139 | return realStateSystem.getQuarkAbsolute(attribute); | |
140 | } catch (AttributeNotFoundException e) { | |
cb42195c | 141 | throw new RuntimeException(ERR_MSG); |
1b9d3765 AM |
142 | } |
143 | } | |
144 | ||
145 | @Override | |
146 | public int getQuarkRelativeAndAdd(int startingNodeQuark, String... subPath) { | |
147 | waitUntilReady(); | |
148 | try { | |
149 | return realStateSystem.getQuarkRelative(startingNodeQuark, subPath); | |
150 | } catch (AttributeNotFoundException e) { | |
cb42195c | 151 | throw new RuntimeException(ERR_MSG); |
1b9d3765 AM |
152 | } |
153 | } | |
154 | ||
155 | private void waitUntilReady() { | |
156 | try { | |
157 | ssAssignedLatch.await(); | |
158 | } catch (InterruptedException e) { | |
159 | e.printStackTrace(); | |
160 | } | |
161 | } | |
162 | ||
163 | } |