Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2be614393454e30adddc921bb08a4dd220a34672 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
 * Copyright (c) 2009, 2010, 2012 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Francois Chouinard - Initial API and implementation
 *   Francois Chouinard - Added support for pre-emption
 *******************************************************************************/

package org.eclipse.linuxtools.internal.tmf.core.request;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.eclipse.linuxtools.internal.tmf.core.TmfCoreTracer;
import org.eclipse.linuxtools.internal.tmf.core.component.TmfEventThread;
import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;

/**
 * A simple, straightforward request executor.
 *
 * @author Francois Chouinard
 * @version 1.1
 */
public class TmfRequestExecutor implements Executor {

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------

    // The request executor
	private final ExecutorService fExecutor = Executors.newFixedThreadPool(2);
	private final String fExecutorName;

	// The request queues
	private final Queue<TmfEventThread> fHighPriorityTasks = new ArrayBlockingQueue<TmfEventThread>(100);
    private final Queue<TmfEventThread> fLowPriorityTasks  = new ArrayBlockingQueue<TmfEventThread>(100);

	// The tasks
	private TmfEventThread fActiveTask;
    private TmfEventThread fSuspendedTask;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    /**
     * Default constructor
     */
    public TmfRequestExecutor() {
        String canonicalName = fExecutor.getClass().getCanonicalName();
        fExecutorName = canonicalName.substring(canonicalName.lastIndexOf('.') + 1);
        if (TmfCoreTracer.isComponentTraced()) {
            TmfCoreTracer.trace(fExecutor + " created"); //$NON-NLS-1$
        }
    }

    /**
     * Standard constructor
     *
     * @param executor The executor service to use
     */
    @Deprecated
    public TmfRequestExecutor(ExecutorService executor) {
        this();
    }

    // ------------------------------------------------------------------------
    // Getters
    // ------------------------------------------------------------------------

	/**
	 * @return the number of pending requests
	 */
    @Deprecated
	public synchronized int getNbPendingRequests() {
		return fHighPriorityTasks.size() + fLowPriorityTasks.size();
	}

	/**
	 * @return the shutdown state (i.e. if it is accepting new requests)
	 */
	public synchronized boolean isShutdown() {
		return fExecutor.isShutdown();
	}

	/**
	 * @return the termination state
	 */
	public synchronized boolean isTerminated() {
		return fExecutor.isTerminated();
	}

    // ------------------------------------------------------------------------
    // Operations
    // ------------------------------------------------------------------------

	/* (non-Javadoc)
	 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
	 */
	@Override
	public synchronized void execute(final Runnable command) {

        // We are expecting MyEventThread:s
        if (!(command instanceof TmfEventThread)) {
            // TODO: Log an error
            return;
        }

        // Wrap the thread in a MyThread
        TmfEventThread thread = (TmfEventThread) command;
        TmfEventThread wrapper = new TmfEventThread(thread) {
            @Override
            public void run() {
                try {
                    command.run();
                } finally {
                    scheduleNext();
                }
            }
        };

        // Add the thread to the appropriate queue
        ExecutionType priority = thread.getExecType();
        (priority == ExecutionType.FOREGROUND ? fHighPriorityTasks : fLowPriorityTasks).offer(wrapper);

        // Schedule or preempt as appropriate
        if (fActiveTask == null) {
            scheduleNext();
        } else if (priority == ExecutionType.FOREGROUND && priority != fActiveTask.getExecType()) {
            fActiveTask.getThread().suspend();
            fSuspendedTask = fActiveTask;
            scheduleNext();
        }
	}

	/**
	 * Executes the next pending request, if applicable.
	 */
	protected synchronized void scheduleNext() {
	       if (!isShutdown()) {
	            if ((fActiveTask = fHighPriorityTasks.poll()) != null) {
	                fExecutor.execute(fActiveTask);
	            } else if (fSuspendedTask != null) {
	                fActiveTask = fSuspendedTask;
	                fSuspendedTask = null;
	                fActiveTask.getThread().resume();
	            } else if ((fActiveTask = fLowPriorityTasks.poll()) != null) {
	                fExecutor.execute(fActiveTask);
	            }
	        }
	}

    /**
     * Stops the executor
     */
    public synchronized void stop() {
        if (fActiveTask != null) {
            fActiveTask.cancel();
        }

        while ((fActiveTask = fHighPriorityTasks.poll()) != null) {
            fActiveTask.cancel();
        }

        fExecutor.shutdown();
        if (TmfCoreTracer.isComponentTraced()) {
            TmfCoreTracer.trace(fExecutor + " terminated"); //$NON-NLS-1$
        }
    }

	// ------------------------------------------------------------------------
	// Object
	// ------------------------------------------------------------------------

	@Override
    @SuppressWarnings("nls")
	public String toString() {
		return "[TmfRequestExecutor(" + fExecutorName + ")]";
	}

}

Back to the top