Commit | Line | Data |
---|---|---|
96b353c5 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2012, 2013 Ericsson |
96b353c5 FC |
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 | ||
96b353c5 FC |
148 | @Override |
149 | public void run() { | |
150 | ||
151 | TmfCoreTracer.traceRequest(fRequest, "is being serviced by " + fProvider.getName()); //$NON-NLS-1$ | |
152 | ||
153 | // Extract the generic information | |
154 | fRequest.start(); | |
5419a136 | 155 | int nbRequested = fRequest.getNbRequested(); |
96b353c5 FC |
156 | int nbRead = 0; |
157 | isCompleted = false; | |
158 | ||
159 | // Initialize the execution | |
160 | ITmfContext context = fProvider.armRequest(fRequest); | |
161 | if (context == null) { | |
162 | fRequest.cancel(); | |
163 | return; | |
164 | } | |
165 | ||
166 | try { | |
167 | // Get the ordered events | |
168 | ITmfEvent event = fProvider.getNext(context); | |
169 | TmfCoreTracer.traceRequest(fRequest, "read first event"); //$NON-NLS-1$ | |
170 | ||
171 | while (event != null && !fProvider.isCompleted(fRequest, event, nbRead)) { | |
5419a136 | 172 | if (isPaused) { |
96b353c5 | 173 | try { |
5419a136 AM |
174 | while (isPaused) { |
175 | synchronized (object) { | |
96b353c5 FC |
176 | object.wait(); |
177 | } | |
178 | } | |
179 | } catch (InterruptedException e) { | |
180 | } | |
181 | } | |
182 | ||
183 | TmfCoreTracer.traceEvent(fProvider, fRequest, event); | |
5419a136 AM |
184 | if (fRequest.getDataType().isInstance(event)) { |
185 | fRequest.handleData(event); | |
186 | } | |
96b353c5 FC |
187 | |
188 | // To avoid an unnecessary read passed the last event requested | |
189 | if (++nbRead < nbRequested) { | |
190 | event = fProvider.getNext(context); | |
191 | } | |
192 | } | |
193 | ||
194 | isCompleted = true; | |
195 | ||
196 | if (fRequest.isCancelled()) { | |
197 | fRequest.cancel(); | |
198 | } else { | |
199 | fRequest.done(); | |
200 | } | |
201 | ||
202 | } catch (Exception e) { | |
203 | fRequest.fail(); | |
204 | } | |
205 | ||
206 | // Cleanup | |
207 | context.dispose(); | |
208 | } | |
209 | ||
210 | // ------------------------------------------------------------------------ | |
211 | // Operations | |
212 | // ------------------------------------------------------------------------ | |
213 | ||
214 | /** | |
215 | * Suspend the thread | |
216 | */ | |
217 | public synchronized void suspend() { | |
218 | isPaused = true; | |
219 | TmfCoreTracer.traceRequest(fRequest, "SUSPENDED"); //$NON-NLS-1$ | |
220 | } | |
221 | ||
222 | /** | |
223 | * Resume the thread | |
224 | */ | |
225 | public synchronized void resume() { | |
226 | isPaused = false; | |
227 | synchronized (object) { | |
228 | object.notifyAll(); | |
229 | } | |
230 | TmfCoreTracer.traceRequest(fRequest, "RESUMED"); //$NON-NLS-1$ | |
231 | } | |
232 | ||
233 | /** | |
234 | * Cancel the request | |
235 | */ | |
236 | public void cancel() { | |
237 | if (!fRequest.isCompleted()) { | |
238 | fRequest.cancel(); | |
239 | } | |
240 | } | |
241 | ||
242 | } |