From 192224fc3c38521f38eb3bc51bebb16b628e4cdb Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 18 Mar 2013 04:02:46 +0100 Subject: Function- RunnableTask: Clear runnableException @ start for re-entry; Fix tExecuted (@ exception); Add debug property 'jogamp.debug.TaskBase.TraceSource', to dump ctor stack trace @ exception. --- src/java/com/jogamp/common/util/FunctionTask.java | 12 +++++++++--- src/java/com/jogamp/common/util/RunnableTask.java | 14 +++++++++---- src/java/com/jogamp/common/util/TaskBase.java | 24 +++++++++++++++++++---- 3 files changed, 39 insertions(+), 11 deletions(-) (limited to 'src/java/com/jogamp/common/util') diff --git a/src/java/com/jogamp/common/util/FunctionTask.java b/src/java/com/jogamp/common/util/FunctionTask.java index 8238aff..a131838 100644 --- a/src/java/com/jogamp/common/util/FunctionTask.java +++ b/src/java/com/jogamp/common/util/FunctionTask.java @@ -127,35 +127,41 @@ public class FunctionTask extends TaskBase implements Function { final A[] args = this.args; this.args = null; this.result = null; + runnableException = null; tStarted = System.currentTimeMillis(); if(null == syncObject) { try { this.result = runnable.eval(args); + tExecuted = System.currentTimeMillis(); } catch (Throwable t) { + tExecuted = System.currentTimeMillis(); runnableException = t; if(null != exceptionOut) { + exceptionOut.println("FunctionTask.run(): "+getExceptionOutIntro()+" exception occured on thread "+Thread.currentThread().getName()+": "+toString()); + printSourceTrace(); t.printStackTrace(exceptionOut); } if(!catchExceptions) { throw new RuntimeException(runnableException); } - } finally { - tExecuted = System.currentTimeMillis(); } } else { synchronized (syncObject) { try { this.result = runnable.eval(args); + tExecuted = System.currentTimeMillis(); } catch (Throwable t) { + tExecuted = System.currentTimeMillis(); runnableException = t; if(null != exceptionOut) { + exceptionOut.println("FunctionTask.run(): "+getExceptionOutIntro()+" exception occured on thread "+Thread.currentThread().getName()+": "+toString()); + printSourceTrace(); t.printStackTrace(exceptionOut); } if(!catchExceptions) { throw new RuntimeException(runnableException); } } finally { - tExecuted = System.currentTimeMillis(); syncObject.notifyAll(); } } diff --git a/src/java/com/jogamp/common/util/RunnableTask.java b/src/java/com/jogamp/common/util/RunnableTask.java index ee484e1..ce4688d 100644 --- a/src/java/com/jogamp/common/util/RunnableTask.java +++ b/src/java/com/jogamp/common/util/RunnableTask.java @@ -88,35 +88,41 @@ public class RunnableTask extends TaskBase { @Override public final void run() { + runnableException = null; tStarted = System.currentTimeMillis(); if(null == syncObject) { try { runnable.run(); + tExecuted = System.currentTimeMillis(); } catch (Throwable t) { + tExecuted = System.currentTimeMillis(); runnableException = t; if(null != exceptionOut) { - t.printStackTrace(exceptionOut); + exceptionOut.println("RunnableTask.run(): "+getExceptionOutIntro()+" exception occured on thread "+Thread.currentThread().getName()+": "+toString()); + printSourceTrace(); + runnableException.printStackTrace(exceptionOut); } if(!catchExceptions) { throw new RuntimeException(runnableException); } - } finally { - tExecuted = System.currentTimeMillis(); } } else { synchronized (syncObject) { try { runnable.run(); + tExecuted = System.currentTimeMillis(); } catch (Throwable t) { + tExecuted = System.currentTimeMillis(); runnableException = t; if(null != exceptionOut) { + exceptionOut.println("RunnableTask.run(): "+getExceptionOutIntro()+" exception occured on thread "+Thread.currentThread().getName()+": "+toString()); + printSourceTrace(); t.printStackTrace(exceptionOut); } if(!catchExceptions) { throw new RuntimeException(runnableException); } } finally { - tExecuted = System.currentTimeMillis(); syncObject.notifyAll(); } } diff --git a/src/java/com/jogamp/common/util/TaskBase.java b/src/java/com/jogamp/common/util/TaskBase.java index 266a5b7..9fd7c0d 100644 --- a/src/java/com/jogamp/common/util/TaskBase.java +++ b/src/java/com/jogamp/common/util/TaskBase.java @@ -30,14 +30,20 @@ package com.jogamp.common.util; import java.io.PrintStream; +import jogamp.common.Debug; + /** * Helper class to provide a Runnable queue implementation with a Runnable wrapper * which notifies after execution for the invokeAndWait() semantics. */ public abstract class TaskBase implements Runnable { + /** Enable via the property jogamp.debug.TaskBase.TraceSource */ + private static final boolean TRACE_SOURCE = Debug.isPropertyDefined("jogamp.debug.TaskBase.TraceSource", true); + protected final Object syncObject; protected final boolean catchExceptions; - protected final PrintStream exceptionOut; + protected final PrintStream exceptionOut; + protected final Throwable sourceStack; protected Object attachment; protected Throwable runnableException; @@ -49,11 +55,21 @@ public abstract class TaskBase implements Runnable { this.syncObject = syncObject; this.catchExceptions = catchExceptions; this.exceptionOut = exceptionOut; + this.sourceStack = TRACE_SOURCE ? new Throwable("Creation @") : null; tCreated = System.currentTimeMillis(); tStarted = 0; tExecuted = 0; isFlushed = false; } + + protected final String getExceptionOutIntro() { + return catchExceptions ? "A catched" : "An uncatched"; + } + protected final void printSourceTrace() { + if( null != sourceStack && null != exceptionOut ) { + sourceStack.printStackTrace(exceptionOut); + } + } /** * Return the synchronization object if any. @@ -130,12 +146,12 @@ public abstract class TaskBase implements Runnable { 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; } + public final long getDurationInExec() { return 0 < tExecuted ? tExecuted - tStarted : 0; } + public final long getDurationTotal() { return 0 < tExecuted ? tExecuted - tCreated : tStarted - tCreated; } @Override public String toString() { - return "RunnableTask[executed "+isExecuted()+", t2-t0 "+getDurationTotal()+", t2-t1 "+getDurationInExec()+", t1-t0 "+getDurationInQueue()+", throwable "+getThrowable()+", Attachment "+attachment+"]"; + return "RunnableTask[executed "+isExecuted()+", tTotal "+getDurationTotal()+" ms, tExec "+getDurationInExec()+" ms, tQueue "+getDurationInQueue()+" ms, attachment "+attachment+", throwable "+getThrowable()+"]"; } } -- cgit v1.2.3