Skip to content

Commit

Permalink
Fix FunctionTask's run()/eval(..) return value assignment: Ensure it'…
Browse files Browse the repository at this point in the history
…s done before syncObject.notifyAll() ; Make methods final

Fixes commit b387d01.
  • Loading branch information
sgothel committed Feb 14, 2013
1 parent b387d01 commit 1604f23
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
38 changes: 21 additions & 17 deletions src/java/com/jogamp/common/util/FunctionTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,23 @@ public FunctionTask(Function<R,A> runnable, Object syncObject, boolean catchExce
}

/** Return the user action */
public Function<R,A> getRunnable() {
public final Function<R,A> getRunnable() {
return runnable;
}

/**
* Sets the arguments for {@link #run()}.
* They will be cleared afterwards.
* They will be cleared after calling {@link #run()} or {@link #eval(Object...)}.
*/
public void setArgs(A... args) {
public final void setArgs(A... args) {
this.args = args;
}

/**
* Retrieves the cached result of {@link #run()}
* and clears it afterwards.
* and is cleared within this method.
*/
public R getResult() {
public final R getResult() {
final R res = result;
result = null;
return res;
Expand All @@ -119,18 +119,14 @@ public R getResult() {
* </p>
*/
@Override
public void run() {
result = eval(args);
args = null;
}

@Override
public R eval(A... args) {
R res = null;
public final void run() {
final A[] args = this.args;
this.args = null;
this.result = null;
tStarted = System.currentTimeMillis();
if(null == syncObject) {
try {
res = runnable.eval(args);
this.result = runnable.eval(args);
} catch (Throwable t) {
runnableException = t;
if(!catchExceptions) {
Expand All @@ -142,7 +138,7 @@ public R eval(A... args) {
} else {
synchronized (syncObject) {
try {
res = runnable.eval(args);
this.result = runnable.eval(args);
} catch (Throwable t) {
runnableException = t;
if(!catchExceptions) {
Expand All @@ -153,8 +149,16 @@ public R eval(A... args) {
syncObject.notifyAll();
}
}
}
}
}

@Override
public final R eval(A... args) {
this.args = args;
run();
final R res = result;
result = null;
return res;
}
}
}

4 changes: 2 additions & 2 deletions src/java/com/jogamp/common/util/RunnableTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public RunnableTask(Runnable runnable, Object syncObject, boolean catchException
}

/** Return the user action */
public Runnable getRunnable() {
public final Runnable getRunnable() {
return runnable;
}

@Override
public void run() {
public final void run() {
tStarted = System.currentTimeMillis();
if(null == syncObject) {
try {
Expand Down
30 changes: 15 additions & 15 deletions src/java/com/jogamp/common/util/TaskBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ protected TaskBase(Object syncObject, boolean catchExceptions) {
* Return the synchronization object if any.
* @see #RunnableTask(Runnable, Object, boolean)
*/
public Object getSyncObject() {
public final Object getSyncObject() {
return syncObject;
}

/**
* Attach a custom object to this task.
* Useful to piggybag further information, ie tag a task final.
*/
public void setAttachment(Object o) {
public final void setAttachment(Object o) {
attachment = o;
}

/**
* Return the attachment object if any.
* @see #setAttachment(Object)
*/
public Object getAttachment() {
public final Object getAttachment() {
return attachment;
}

Expand All @@ -86,7 +86,7 @@ public Object getAttachment() {
* @see #isFlushed()
* @see #isInQueue()
*/
public void flush() {
public final void flush() {
if(!isExecuted() && hasWaiter()) {
synchronized (syncObject) {
isFlushed = true;
Expand All @@ -98,36 +98,36 @@ public void flush() {
/**
* @return !{@link #isExecuted()} && !{@link #isFlushed()}
*/
public boolean isInQueue() { return 0 != tExecuted && !isFlushed; }
public final boolean isInQueue() { return 0 != tExecuted && !isFlushed; }

/**
* @return True if executed, otherwise false;
*/
public boolean isExecuted() { return 0 != tExecuted ; }
public final boolean isExecuted() { return 0 != tExecuted ; }

/**
* @return True if flushed, otherwise false;
*/
public boolean isFlushed() { return isFlushed; }
public final boolean isFlushed() { return isFlushed; }

/**
* @return True if invoking thread waits until done,
* ie a <code>notifyObject</code> was passed, otherwise false;
*/
public boolean hasWaiter() { return null != syncObject; }
public final boolean hasWaiter() { return null != syncObject; }

/**
* @return A thrown exception while execution of the user action, if any and if catched
* @see #RunnableTask(Runnable, Object, boolean)
*/
public Throwable getThrowable() { return runnableException; }
public final Throwable getThrowable() { return runnableException; }

public long getTimestampCreate() { return tCreated; }
public long getTimestampBeforeExec() { return tStarted; }
public long getTimestampAfterExec() { return tExecuted; }
public long getDurationInQueue() { return tStarted - tCreated; }
public long getDurationInExec() { return tExecuted - tStarted; }
public long getDurationTotal() { return tExecuted - tCreated; }
public final long getTimestampCreate() { return tCreated; }
public final long getTimestampBeforeExec() { return tStarted; }
public final long getTimestampAfterExec() { return tExecuted; }
public final long getDurationInQueue() { return tStarted - tCreated; }
public final long getDurationInExec() { return tExecuted - tStarted; }
public final long getDurationTotal() { return tExecuted - tCreated; }

@Override
public String toString() {
Expand Down

0 comments on commit 1604f23

Please sign in to comment.