18#ifndef _DECAF_UTIL_CONCURRENT_FUTURETASK_H_
19#define _DECAF_UTIL_CONCURRENT_FUTURETASK_H_
39 using decaf::lang::Pointer;
75 FutureTaskAdapter(
const FutureTaskAdapter&);
76 FutureTaskAdapter operator= (
const FutureTaskAdapter&);
88 virtual ~FutureTaskAdapter() {
92 delete this->callable;
99 if (this->task !=
NULL) {
103 return this->callable->
call();
147 FutureTaskSync(
const FutureTaskSync&);
148 FutureTaskSync operator= (
const FutureTaskSync&);
156 virtual ~FutureTaskSync() {
159 bool innerIsCancelled()
const {
163 bool innerIsDone()
const {
164 return ranOrCancelled(
getState()) && this->runner ==
NULL;
172 if (exception !=
NULL) {
178 T innerGet(
long long nanosTimeout) {
185 if (exception !=
NULL) {
191 void innerSet(
const T& result) {
197 if (s == CANCELLED) {
205 this->result = result;
207 this->parent->
done();
219 if (s == CANCELLED) {
229 this->parent->
done();
235 bool innerCancel(
bool mayInterruptIfRunning) {
238 if (ranOrCancelled(s)) {
246 if (mayInterruptIfRunning) {
254 this->parent->
done();
267 result = this->callable->call();
271 }
catch(std::exception& stdex) {
276 __FILE__, __LINE__,
"FutureTask Caught Unknown exception during task execution."));
279 this->parent->
set(result);
285 bool innerRunAndReset() {
293 this->callable->call();
300 }
catch(std::exception& stdex) {
305 __FILE__, __LINE__,
"FutureTask Caught Unknown exception during task execution."));
315 virtual int tryAcquireShared(
int ignore DECAF_UNUSED) {
316 return innerIsDone() ? 1 : -1;
323 virtual bool tryReleaseShared(
int ignore DECAF_UNUSED) {
330 bool ranOrCancelled(
int state)
const {
331 return (state & (RAN | CANCELLED)) != 0;
353 if (callable ==
NULL ) {
355 "The Callable pointer passed to the constructor was NULL");
358 this->sync.
reset(
new FutureTaskSync(
this,
new FutureTaskAdapter(callable, takeOwnership)));
376 if (runnable ==
NULL ) {
378 "The Runnable pointer passed to the constructor was NULL");
381 this->sync.
reset(
new FutureTaskSync(
this,
new FutureTaskAdapter(runnable, result, takeOwnership)));
388 return this->sync->innerIsCancelled();
392 return this->sync->innerIsDone();
395 virtual bool cancel(
bool mayInterruptIfRunning) {
396 return this->sync->innerCancel(mayInterruptIfRunning);
400 return this->sync->innerGet();
404 return this->sync->innerGet(unit.
toNanos(timeout));
432 virtual void set(
const T& result) {
433 this->sync->innerSet(result);
446 this->sync->innerSetException(error);
450 this->sync->innerRun();
462 return this->sync->innerRunAndReset();
471 this->sync = source.sync;
Definition Exception.h:38
virtual Exception * clone() const
Clones this exception.
Decaf's implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition Pointer.h:53
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition Pointer.h:161
Interface for a runnable object - defines a task that can be run by a thread.
Definition Runnable.h:29
virtual void run()=0
Run method - called by the Thread class in the context of the thread.
A Thread is a concurrent unit of execution.
Definition Thread.h:64
void interrupt()
Interrupts the Thread if it is blocked and in an interruptible state.
static Thread * currentThread()
Returns a pointer to the currently executing thread object.
Definition NullPointerException.h:32
A task that returns a result and may throw an exception.
Definition Callable.h:47
virtual V call()=0
Computes a result, or throws an exception if unable to do so.
Definition CancellationException.h:30
Definition ExecutionException.h:31
virtual void run()
Run method - called by the Thread class in the context of the thread.
Definition FutureTask.h:449
virtual bool isDone() const
Returns true if this task completed.
Definition FutureTask.h:391
virtual void setException(const decaf::lang::Exception &error)
Causes this future to report an ExecutionException with the given Exception as its cause,...
Definition FutureTask.h:445
virtual bool cancel(bool mayInterruptIfRunning)
Attempts to cancel execution of this task.
Definition FutureTask.h:395
FutureTask< T > & operator=(const FutureTask< T > &source)
Definition FutureTask.h:470
FutureTask(const FutureTask< T > &source)
Definition FutureTask.h:467
FutureTask(Callable< T > *callable, bool takeOwnership=true)
Creates a FutureTask instance that will, upon running, execute the given Callable.
Definition FutureTask.h:352
virtual T get(long long timeout, const TimeUnit &unit)
Waits if necessary for at most the given time for the computation to complete, and then retrieves its...
Definition FutureTask.h:403
FutureTask< T > * clone()
Definition FutureTask.h:407
virtual bool runAndReset()
Executes the computation without setting its result, and then resets this Future to initial state,...
Definition FutureTask.h:461
virtual ~FutureTask()
Definition FutureTask.h:384
virtual void done()
Protected method invoked when this task transitions to state isDone (whether normally or via cancella...
Definition FutureTask.h:421
virtual T get()
Waits if necessary for the computation to complete, and then retrieves its result.
Definition FutureTask.h:399
virtual bool isCancelled() const
Returns true if this task was canceled before it completed normally.
Definition FutureTask.h:387
virtual void set(const T &result)
Sets the result of this Future to the given value unless this future has already been set or has been...
Definition FutureTask.h:432
FutureTask(decaf::lang::Runnable *runnable, const T &result, bool takeOwnership=true)
Creates a FutureTask that will, upon running, execute the given Runnable, and arrange that the get me...
Definition FutureTask.h:375
A Runnable version of the Future type.
Definition RunnableFuture.h:38
A TimeUnit represents time durations at a given unit of granularity and provides utility methods to c...
Definition TimeUnit.h:62
long long toNanos(long long duration) const
Equivalent to NANOSECONDS.convert(duration, this).
Definition TimeUnit.h:126
Definition TimeoutException.h:32
Definition AbstractQueuedSynchronizer.h:35
virtual int getState() const
Gets and returns the currently set value of this object Synchronization sate.
AbstractQueuedSynchronizer()
virtual bool compareAndSetState(int expect, int update)
Sets the synchronization state to the specified value if the current value is equal to the expected v...
void acquireSharedInterruptibly(int arg)
Acquire the lock in shared mode, allowing interruption.
bool releaseShared(int arg)
When held in shared mode this method releases the Synchronizer.
bool tryAcquireSharedNanos(int arg, long long nanos)
Acquires in shared mode if possible, first checking if the calling thread has already been interrupte...
#define DECAF_CATCHALL_NOTHROW()
A catch-all that does not throw an exception, one use would be to catch any exception in a destructor...
Definition ExceptionDefines.h:62
#define NULL
Definition Config.h:33
Definition AbstractExecutorService.h:28
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25