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)) {
227 exception.reset(t.
clone());
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."));
315 virtual int tryAcquireShared(
int ignore) {
316 return innerIsDone() ? 1 : -1;
323 virtual bool tryReleaseShared(
int ignore) {
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;