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