18 #ifndef _DECAF_UTIL_CONCURRENT_FUTURETASK_H_ 19 #define _DECAF_UTIL_CONCURRENT_FUTURETASK_H_ 37 namespace concurrent {
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&);
153 AbstractQueuedSynchronizer(), callable(callable), result(), exception(), parent(parent), runner(
NULL) {
156 virtual ~FutureTaskSync() {
159 bool innerIsCancelled()
const {
160 return getState() == CANCELLED;
163 bool innerIsDone()
const {
164 return ranOrCancelled(getState()) && this->runner ==
NULL;
168 this->acquireSharedInterruptibly(0);
169 if (getState() == CANCELLED) {
172 if (exception !=
NULL) {
178 T innerGet(
long long nanosTimeout) {
179 if (!tryAcquireSharedNanos(0, nanosTimeout)) {
182 if (getState() == CANCELLED) {
185 if (exception !=
NULL) {
191 void innerSet(
const T& result) {
197 if (s == CANCELLED) {
204 if (compareAndSetState(s, RAN)) {
205 this->result = result;
207 this->parent->
done();
219 if (s == CANCELLED) {
226 if (compareAndSetState(s, RAN)) {
229 this->parent->
done();
235 bool innerCancel(
bool mayInterruptIfRunning) {
238 if (ranOrCancelled(s)) {
241 if (compareAndSetState(s, CANCELLED)) {
246 if (mayInterruptIfRunning) {
254 this->parent->
done();
259 if (!compareAndSetState(READY, RUNNING)) {
264 if (getState() == RUNNING) {
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() {
286 if (!compareAndSetState(READY, RUNNING)) {
292 if (getState() == RUNNING) {
293 this->callable->
call();
296 return compareAndSetState(RUNNING, READY);
300 }
catch(std::exception& stdex) {
305 __FILE__, __LINE__,
"FutureTask Caught Unknown exception during task execution."));
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();
403 virtual T
get(
long long timeout,
const TimeUnit& unit) {
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;
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition: Pointer.h:161
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
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
static Thread * currentThread()
Returns a pointer to the currently executing thread object.
virtual bool runAndReset()
Executes the computation without setting its result, and then resets this Future to initial state...
Definition: FutureTask.h:461
Definition: AbstractQueuedSynchronizer.h:35
FutureTask(const FutureTask< T > &source)
Definition: FutureTask.h:467
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
A task that returns a result and may throw an exception.
Definition: Callable.h:47
#define NULL
Definition: Config.h:33
virtual bool cancel(bool mayInterruptIfRunning)
Attempts to cancel execution of this task.
Definition: FutureTask.h:395
#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
Definition: CancellationException.h:30
virtual void done()
Protected method invoked when this task transitions to state isDone (whether normally or via cancella...
Definition: FutureTask.h:421
A cancellable asynchronous computation.
Definition: FutureTask.h:58
virtual Exception * clone() const
Clones this exception.
Definition: TimeoutException.h:32
A TimeUnit represents time durations at a given unit of granularity and provides utility methods to c...
Definition: TimeUnit.h:62
virtual ~FutureTask()
Definition: FutureTask.h:384
FutureTask< T > & operator=(const FutureTask< T > &source)
Definition: FutureTask.h:470
FutureTask(Callable< T > *callable, bool takeOwnership=true)
Creates a FutureTask instance that will, upon running, execute the given Callable.
Definition: FutureTask.h:352
virtual void run()
Run method - called by the Thread class in the context of the thread.
Definition: FutureTask.h:449
Definition: ExecutionException.h:31
virtual V call()=0
Computes a result, or throws an exception if unable to do so.
A Runnable version of the Future type.
Definition: RunnableFuture.h:38
A Thread is a concurrent unit of execution.
Definition: Thread.h:64
Definition: NullPointerException.h:32
virtual bool isDone() const
Returns true if this task completed.
Definition: FutureTask.h:391
#define DECAF_UNUSED
Definition: Config.h:160
Interface for a runnable object - defines a task that can be run by a thread.
Definition: Runnable.h:29
Definition: Exception.h:38
FutureTask< T > * clone()
Definition: FutureTask.h:407
void interrupt()
Interrupts the Thread if it is blocked and in an interruptible state.
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
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
virtual bool isCancelled() const
Returns true if this task was canceled before it completed normally.
Definition: FutureTask.h:387