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