Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 871c5a2daf8887f21e5d76cd0c4565e75251a1d7 (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
190
191
192
193
194
195
196
/*******************************************************************************
 * Copyright (c) 2006, 2015 Wind River Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.concurrent;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.cdt.dsf.internal.DsfPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

/**
 * A convenience class that allows a client to retrieve data from services
 * synchronously from a non-dispatch thread.  This class is different from
 * a Callable<V> in that it allows the implementation code to calculate
 * the result in several dispatches, rather than requiring it to return the
 * data at end of Callable#call method.
 * <p>
 * Usage:<br/>
 * <pre>
 *     class DataQuery extends Query<Data> {
 *         protected void execute(DataRequestMonitor<Data> rm) {
 *             rm.setData(fSlowService.getData());
 *             rm.done();
 *         }
 *     }
 *
 *     DsfExecutor executor = getExecutor();
 *     DataQuery query = new DataQuery();
 *     executor.submit(query);
 *
 *     try {
 *         Data data = query.get();
 *     }
 *
 * </pre>
 * <p>
 * @see java.util.concurrent.Callable
 *
 * @since 1.0
 */
@ThreadSafe
abstract public class Query<V> extends DsfRunnable implements Future<V> {
	private class QueryRm extends DataRequestMonitor<V> {

		boolean fExecuted = false;

		boolean fCompleted = false;

		private QueryRm() {
			super(ImmediateExecutor.getInstance(), null);
		}

		@Override
		public synchronized void handleCompleted() {
			fCompleted = true;
			notifyAll();
		}

		public synchronized boolean isCompleted() {
			return fCompleted;
		}

		public synchronized boolean setExecuted() {
			if (fExecuted || isCanceled()) {
				// already executed or canceled
				return false;
			}
			fExecuted = true;
			return true;
		}
	};

	private final QueryRm fRm = new QueryRm();

	/**
	 * The no-argument constructor
	 */
	public Query() {
	}

	@Override
	public V get() throws InterruptedException, ExecutionException {
		IStatus status;
		V data;
		synchronized (fRm) {
			while (!isDone()) {
				fRm.wait();
			}
			status = fRm.getStatus();
			data = fRm.getData();
		}

		if (status.getSeverity() == IStatus.CANCEL) {
			throw new CancellationException();
		} else if (status.getSeverity() != IStatus.OK) {
			throw new ExecutionException(new CoreException(status));
		}
		return data;
	}

	@Override
	public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
		long timeLeft = unit.toMillis(timeout);
		long timeoutTime = System.currentTimeMillis() + unit.toMillis(timeout);

		IStatus status;
		V data;
		synchronized (fRm) {
			while (!isDone()) {
				if (timeLeft <= 0) {
					throw new TimeoutException();
				}
				fRm.wait(timeLeft);
				timeLeft = timeoutTime - System.currentTimeMillis();
			}
			status = fRm.getStatus();
			data = fRm.getData();
		}

		if (status.getSeverity() == IStatus.CANCEL) {
			throw new CancellationException();
		} else if (status.getSeverity() != IStatus.OK) {
			throw new ExecutionException(new CoreException(status));
		}
		return data;
	}

	/**
	 * Don't try to interrupt the DSF executor thread, just ignore the request
	 * if set.
	 */
	@Override
	public boolean cancel(boolean mayInterruptIfRunning) {
		boolean completed = false;
		synchronized (fRm) {
			completed = fRm.isCompleted();
			if (!completed) {
				fRm.cancel();
				fRm.notifyAll();
			}
		}
		return !completed;
	}

	@Override
	public boolean isCancelled() {
		return fRm.isCanceled();
	}

	@Override
	public boolean isDone() {
		synchronized (fRm) {
			// If future is canceled, return right away.
			return fRm.isCompleted() || fRm.isCanceled();
		}
	}

	abstract protected void execute(DataRequestMonitor<V> rm);

	@Override
	public void run() {
		if (fRm.setExecuted()) {
			execute(fRm);
		}
	}

	/**
	 * Completes the query with the given exception.
	 *
	 * @deprecated Query implementations should call the request monitor to
	 * set the exception status directly.
	 */
	@Deprecated
	protected void doneException(Throwable t) {
		fRm.setStatus(
				new Status(IStatus.ERROR, DsfPlugin.PLUGIN_ID, IDsfStatusConstants.INTERNAL_ERROR, "Exception", t)); //$NON-NLS-1$
		fRm.done();
	}

}

Back to the top