Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / request / TmfRequestExecutor.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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.tmf.core.request;
14
15 import java.util.Comparator;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.PriorityBlockingQueue;
20
21 import org.eclipse.linuxtools.tmf.core.Tracer;
22 import org.eclipse.linuxtools.tmf.core.component.TmfThread;
23 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
24
25 /**
26 * <b><u>TmfRequestExecutor</u></b>
27 *
28 * A simple, straightforward request executor.
29 */
30 public class TmfRequestExecutor implements Executor {
31
32 private final ExecutorService fExecutor;
33 private final String fExecutorName;
34 private final PriorityBlockingQueue<TmfThread> fRequestQueue = new PriorityBlockingQueue<TmfThread>(100, new Comparator<TmfThread>() {
35 @Override
36 public int compare(TmfThread o1, TmfThread o2) {
37 if (o1.getExecType() == o2.getExecType())
38 return 0;
39 if (o1.getExecType() == ExecutionType.BACKGROUND)
40 return 1;
41 return -1;
42 }
43 });
44 private TmfThread fCurrentRequest;
45
46 // ------------------------------------------------------------------------
47 // Constructors
48 // ------------------------------------------------------------------------
49
50 public TmfRequestExecutor() {
51 this(Executors.newSingleThreadExecutor());
52 }
53
54 public TmfRequestExecutor(ExecutorService executor) {
55 fExecutor = executor;
56 String canonicalName = fExecutor.getClass().getCanonicalName();
57 fExecutorName = canonicalName.substring(canonicalName.lastIndexOf('.') + 1);
58 if (Tracer.isComponentTraced()) Tracer.trace(fExecutor + " created"); //$NON-NLS-1$
59 }
60
61 /**
62 * @return the number of pending requests
63 */
64 public synchronized int getNbPendingRequests() {
65 return fRequestQueue.size();
66 }
67
68 /**
69 * @return the shutdown state (i.e. if it is accepting new requests)
70 */
71 public synchronized boolean isShutdown() {
72 return fExecutor.isShutdown();
73 }
74
75 /**
76 * @return the termination state
77 */
78 public synchronized boolean isTerminated() {
79 return fExecutor.isTerminated();
80 }
81
82 /**
83 * Stops the executor
84 */
85 public synchronized void stop() {
86 if (fCurrentRequest != null) {
87 fCurrentRequest.cancel();
88 }
89
90 while ((fCurrentRequest = fRequestQueue.poll()) != null) {
91 fCurrentRequest.cancel();
92 }
93
94 fExecutor.shutdown();
95 if (Tracer.isComponentTraced()) Tracer.trace(fExecutor + " terminated"); //$NON-NLS-1$
96 }
97
98 // ------------------------------------------------------------------------
99 // Operations
100 // ------------------------------------------------------------------------
101
102 /* (non-Javadoc)
103 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
104 */
105 @Override
106 public synchronized void execute(final Runnable requestThread) {
107 fRequestQueue.offer(new TmfThread(((TmfThread) requestThread).getExecType()) {
108 @Override
109 public void run() {
110 try {
111 requestThread.run();
112 } finally {
113 scheduleNext();
114 }
115 }
116 @Override
117 public void cancel() {
118 ((TmfThread) requestThread).cancel();
119 }
120 });
121 if (fCurrentRequest == null) {
122 scheduleNext();
123 }
124 }
125
126 /**
127 * Executes the next pending request, if applicable.
128 */
129 protected synchronized void scheduleNext() {
130 if ((fCurrentRequest = fRequestQueue.poll()) != null) {
131 if (!isShutdown())
132 fExecutor.execute(fCurrentRequest);
133 }
134 }
135
136 // ------------------------------------------------------------------------
137 // Object
138 // ------------------------------------------------------------------------
139
140 @Override
141 @SuppressWarnings("nls")
142 public String toString() {
143 return "[TmfRequestExecutor(" + fExecutorName + ")]";
144 }
145
146 }
This page took 0.049315 seconds and 5 git commands to generate.