Implement TmfTimestampDelta for deltas of timestamps
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / request / TmfRequestExecutor.java
CommitLineData
951d134a
FC
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
9b749023 3 *
951d134a
FC
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
9b749023 8 *
951d134a
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
8fd82db5 13package org.eclipse.linuxtools.internal.tmf.core.request;
8c8bf09f 14
9b635e61 15import java.util.Comparator;
8c8bf09f 16import java.util.concurrent.Executor;
54d55ced 17import java.util.concurrent.ExecutorService;
8c8bf09f 18import java.util.concurrent.Executors;
9b635e61
FC
19import java.util.concurrent.PriorityBlockingQueue;
20
5500a7f0 21import org.eclipse.linuxtools.internal.tmf.core.TmfCoreTracer;
8fd82db5 22import org.eclipse.linuxtools.internal.tmf.core.component.TmfThread;
6c13869b 23import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
8c8bf09f 24
951d134a 25/**
2fb2eb37 26 * A simple, straightforward request executor.
9b749023 27 *
8fd82db5
FC
28 * @version 1.0
29 * @author Francois Chouinard
951d134a 30 */
8c8bf09f
ASL
31public class TmfRequestExecutor implements Executor {
32
54d55ced 33 private final ExecutorService fExecutor;
7a88aecf 34 private final String fExecutorName;
9b749023
AM
35 private final PriorityBlockingQueue<TmfThread> fRequestQueue = new PriorityBlockingQueue<TmfThread>(
36 100, new Comparator<TmfThread>() {
37 @Override
38 public int compare(TmfThread o1, TmfThread o2) {
39 if (o1.getExecType() == o2.getExecType()) {
40 return 0;
41 }
42 if (o1.getExecType() == ExecutionType.BACKGROUND) {
43 return 1;
44 }
45 return -1;
46 }
47 });
48 private TmfThread fCurrentRequest;
fc6ccf6f 49
9b749023
AM
50 // ------------------------------------------------------------------------
51 // Constructors
52 // ------------------------------------------------------------------------
53
54 /**
55 * Default constructor
56 */
57 public TmfRequestExecutor() {
58 this(Executors.newSingleThreadExecutor());
59 }
60
61 /**
62 * Standard constructor
63 *
64 * @param executor The executor service to use
65 */
66 public TmfRequestExecutor(ExecutorService executor) {
67 fExecutor = executor;
68 String canonicalName = fExecutor.getClass().getCanonicalName();
69 fExecutorName = canonicalName.substring(canonicalName.lastIndexOf('.') + 1);
5500a7f0 70 if (TmfCoreTracer.isComponentTraced())
9b749023 71 {
5500a7f0 72 TmfCoreTracer.trace(fExecutor + " created"); //$NON-NLS-1$
9b749023
AM
73 }
74 }
54d55ced 75
5c00c0b7
FC
76 /**
77 * @return the number of pending requests
78 */
1087f2b9 79 public synchronized int getNbPendingRequests() {
5c00c0b7
FC
80 return fRequestQueue.size();
81 }
9b749023 82
5c00c0b7
FC
83 /**
84 * @return the shutdown state (i.e. if it is accepting new requests)
85 */
db1ea19b 86 public synchronized boolean isShutdown() {
5c00c0b7
FC
87 return fExecutor.isShutdown();
88 }
9b749023 89
5c00c0b7
FC
90 /**
91 * @return the termination state
92 */
1087f2b9 93 public synchronized boolean isTerminated() {
5c00c0b7
FC
94 return fExecutor.isTerminated();
95 }
9b749023 96
2fb2eb37
FC
97 /**
98 * Stops the executor
99 */
1087f2b9 100 public synchronized void stop() {
475743b7
FC
101 if (fCurrentRequest != null) {
102 fCurrentRequest.cancel();
103 }
9b749023 104
1087f2b9
BH
105 while ((fCurrentRequest = fRequestQueue.poll()) != null) {
106 fCurrentRequest.cancel();
107 }
108
54d55ced 109 fExecutor.shutdown();
5500a7f0 110 if (TmfCoreTracer.isComponentTraced())
9b749023 111 {
5500a7f0 112 TmfCoreTracer.trace(fExecutor + " terminated"); //$NON-NLS-1$
9b749023 113 }
54d55ced 114 }
9b749023 115
5c00c0b7
FC
116 // ------------------------------------------------------------------------
117 // Operations
118 // ------------------------------------------------------------------------
9b749023 119
2fb2eb37
FC
120 /* (non-Javadoc)
121 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
122 */
d4011df2 123 @Override
9b635e61
FC
124 public synchronized void execute(final Runnable requestThread) {
125 fRequestQueue.offer(new TmfThread(((TmfThread) requestThread).getExecType()) {
126 @Override
8c8bf09f
ASL
127 public void run() {
128 try {
9b635e61 129 requestThread.run();
8c8bf09f
ASL
130 } finally {
131 scheduleNext();
132 }
133 }
475743b7
FC
134 @Override
135 public void cancel() {
136 ((TmfThread) requestThread).cancel();
137 }
8c8bf09f 138 });
5c00c0b7 139 if (fCurrentRequest == null) {
8c8bf09f
ASL
140 scheduleNext();
141 }
142 }
143
2fb2eb37
FC
144 /**
145 * Executes the next pending request, if applicable.
146 */
8c8bf09f 147 protected synchronized void scheduleNext() {
5c00c0b7 148 if ((fCurrentRequest = fRequestQueue.poll()) != null) {
9b749023
AM
149 if (!isShutdown()) {
150 fExecutor.execute(fCurrentRequest);
151 }
8c8bf09f
ASL
152 }
153 }
154
5c00c0b7
FC
155 // ------------------------------------------------------------------------
156 // Object
157 // ------------------------------------------------------------------------
158
159 @Override
3b38ea61 160 @SuppressWarnings("nls")
5c00c0b7 161 public String toString() {
7a88aecf 162 return "[TmfRequestExecutor(" + fExecutorName + ")]";
8c8bf09f
ASL
163 }
164
165}
This page took 0.049003 seconds and 5 git commands to generate.