Commit | Line | Data |
---|---|---|
96b353c5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.internal.tmf.core.component; | |
14 | ||
15 | import org.eclipse.linuxtools.internal.tmf.core.TmfCoreTracer; | |
5419a136 | 16 | import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider; |
96b353c5 FC |
17 | import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider; |
18 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
5419a136 AM |
19 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
20 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType; | |
96b353c5 FC |
21 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
22 | ||
23 | /** | |
24 | * Provides the core event request processor. It also has support for suspending | |
25 | * and resuming a request in a thread-safe manner. | |
26 | * | |
27 | * @author Francois Chouinard | |
28 | * @version 1.0 | |
29 | */ | |
30 | public class TmfEventThread implements Runnable { | |
31 | ||
32 | // ------------------------------------------------------------------------ | |
33 | // Attributes | |
34 | // ------------------------------------------------------------------------ | |
35 | ||
36 | /** | |
37 | * The event provider | |
38 | */ | |
39 | private final TmfDataProvider fProvider; | |
40 | ||
41 | /** | |
42 | * The wrapped event request | |
43 | */ | |
5419a136 | 44 | private final ITmfDataRequest fRequest; |
96b353c5 FC |
45 | |
46 | /** | |
47 | * The request execution priority | |
48 | */ | |
5419a136 | 49 | private final ExecutionType fExecType; |
96b353c5 FC |
50 | |
51 | /** | |
52 | * The wrapped thread (if applicable) | |
53 | */ | |
54 | private final TmfEventThread fThread; | |
55 | ||
56 | /** | |
57 | * The thread execution state | |
58 | */ | |
59 | private volatile boolean isPaused = false; | |
60 | private volatile boolean isCompleted = false; | |
61 | ||
62 | /** | |
63 | * The synchronization object | |
64 | */ | |
65 | private final Object object = new Object(); | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // Constructor | |
69 | // ------------------------------------------------------------------------ | |
70 | ||
71 | /** | |
72 | * Basic constructor | |
73 | * | |
74 | * @param provider the event provider | |
75 | * @param request the request to process | |
76 | */ | |
5419a136 | 77 | public TmfEventThread(TmfDataProvider provider, ITmfDataRequest request) { |
96b353c5 FC |
78 | assert provider != null; |
79 | assert request != null; | |
80 | fProvider = provider; | |
81 | fRequest = request; | |
5419a136 | 82 | fExecType = request.getExecType(); |
96b353c5 FC |
83 | fThread = null; |
84 | } | |
85 | ||
86 | /** | |
87 | * Wrapper constructor | |
88 | * | |
89 | * @param thread the thread to wrap | |
90 | */ | |
91 | public TmfEventThread(TmfEventThread thread) { | |
92 | fProvider = thread.fProvider; | |
93 | fRequest = thread.fRequest; | |
94 | fExecType = thread.fExecType; | |
95 | fThread = thread; | |
96 | } | |
97 | ||
98 | // ------------------------------------------------------------------------ | |
99 | // Getters | |
100 | // ------------------------------------------------------------------------ | |
101 | ||
102 | /** | |
103 | * @return The wrapped thread | |
104 | */ | |
105 | public TmfEventThread getThread() { | |
106 | return fThread; | |
107 | } | |
108 | ||
109 | /** | |
110 | * @return The event provider | |
111 | */ | |
5419a136 | 112 | public ITmfDataProvider getProvider() { |
96b353c5 FC |
113 | return fProvider; |
114 | } | |
115 | ||
116 | /** | |
117 | * @return The event request | |
118 | */ | |
5419a136 | 119 | public ITmfDataRequest getRequest() { |
96b353c5 FC |
120 | return fRequest; |
121 | } | |
122 | ||
123 | /** | |
124 | * @return The request execution priority | |
125 | */ | |
5419a136 | 126 | public ExecutionType getExecType() { |
96b353c5 FC |
127 | return fExecType; |
128 | } | |
129 | ||
130 | /** | |
131 | * @return The request execution state | |
132 | */ | |
133 | public boolean isRunning() { | |
134 | return fRequest.isRunning() && !isPaused; | |
135 | } | |
136 | ||
137 | /** | |
138 | * @return The request execution state | |
139 | */ | |
140 | public boolean isCompleted() { | |
141 | return isCompleted; | |
142 | } | |
143 | ||
144 | // ------------------------------------------------------------------------ | |
145 | // Runnable | |
146 | // ------------------------------------------------------------------------ | |
147 | ||
148 | /* (non-Javadoc) | |
149 | * @see java.lang.Runnable#run() | |
150 | */ | |
151 | @Override | |
152 | public void run() { | |
153 | ||
154 | TmfCoreTracer.traceRequest(fRequest, "is being serviced by " + fProvider.getName()); //$NON-NLS-1$ | |
155 | ||
156 | // Extract the generic information | |
157 | fRequest.start(); | |
5419a136 | 158 | int nbRequested = fRequest.getNbRequested(); |
96b353c5 FC |
159 | int nbRead = 0; |
160 | isCompleted = false; | |
161 | ||
162 | // Initialize the execution | |
163 | ITmfContext context = fProvider.armRequest(fRequest); | |
164 | if (context == null) { | |
165 | fRequest.cancel(); | |
166 | return; | |
167 | } | |
168 | ||
169 | try { | |
170 | // Get the ordered events | |
171 | ITmfEvent event = fProvider.getNext(context); | |
172 | TmfCoreTracer.traceRequest(fRequest, "read first event"); //$NON-NLS-1$ | |
173 | ||
174 | while (event != null && !fProvider.isCompleted(fRequest, event, nbRead)) { | |
5419a136 | 175 | if (isPaused) { |
96b353c5 | 176 | try { |
5419a136 AM |
177 | while (isPaused) { |
178 | synchronized (object) { | |
96b353c5 FC |
179 | object.wait(); |
180 | } | |
181 | } | |
182 | } catch (InterruptedException e) { | |
183 | } | |
184 | } | |
185 | ||
186 | TmfCoreTracer.traceEvent(fProvider, fRequest, event); | |
5419a136 AM |
187 | if (fRequest.getDataType().isInstance(event)) { |
188 | fRequest.handleData(event); | |
189 | } | |
96b353c5 FC |
190 | |
191 | // To avoid an unnecessary read passed the last event requested | |
192 | if (++nbRead < nbRequested) { | |
193 | event = fProvider.getNext(context); | |
194 | } | |
195 | } | |
196 | ||
197 | isCompleted = true; | |
198 | ||
199 | if (fRequest.isCancelled()) { | |
200 | fRequest.cancel(); | |
201 | } else { | |
202 | fRequest.done(); | |
203 | } | |
204 | ||
205 | } catch (Exception e) { | |
206 | fRequest.fail(); | |
207 | } | |
208 | ||
209 | // Cleanup | |
210 | context.dispose(); | |
211 | } | |
212 | ||
213 | // ------------------------------------------------------------------------ | |
214 | // Operations | |
215 | // ------------------------------------------------------------------------ | |
216 | ||
217 | /** | |
218 | * Suspend the thread | |
219 | */ | |
220 | public synchronized void suspend() { | |
221 | isPaused = true; | |
222 | TmfCoreTracer.traceRequest(fRequest, "SUSPENDED"); //$NON-NLS-1$ | |
223 | } | |
224 | ||
225 | /** | |
226 | * Resume the thread | |
227 | */ | |
228 | public synchronized void resume() { | |
229 | isPaused = false; | |
230 | synchronized (object) { | |
231 | object.notifyAll(); | |
232 | } | |
233 | TmfCoreTracer.traceRequest(fRequest, "RESUMED"); //$NON-NLS-1$ | |
234 | } | |
235 | ||
236 | /** | |
237 | * Cancel the request | |
238 | */ | |
239 | public void cancel() { | |
240 | if (!fRequest.isCompleted()) { | |
241 | fRequest.cancel(); | |
242 | } | |
243 | } | |
244 | ||
245 | } |