diff options
Diffstat (limited to 'dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/concurrent/Query.java')
-rw-r--r-- | dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/concurrent/Query.java | 294 |
1 files changed, 146 insertions, 148 deletions
diff --git a/dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/concurrent/Query.java b/dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/concurrent/Query.java index c3ee9c6953a..871c5a2daf8 100644 --- a/dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/concurrent/Query.java +++ b/dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/concurrent/Query.java @@ -7,7 +7,7 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 - * + * * Contributors: * Wind River Systems - initial API and implementation *******************************************************************************/ @@ -24,12 +24,11 @@ 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 + * 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 + * the result in several dispatches, rather than requiring it to return the * data at end of Callable#call method. * <p> * Usage:<br/> @@ -40,159 +39,158 @@ import org.eclipse.core.runtime.Status; * rm.done(); * } * } - * + * * DsfExecutor executor = getExecutor(); * DataQuery query = new DataQuery(); * executor.submit(query); - * + * * try { * Data data = query.get(); * } - * + * * </pre> - * <p> + * <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(); - } - - -} +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(); + } + +} |