diff options
author | Sven Gothel <[email protected]> | 2010-09-16 02:14:06 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2010-09-16 02:14:06 +0200 |
commit | e62a91e26ba01a8970658681891edebcee7461e1 (patch) | |
tree | 7b7ec2834c076e303d5e9cb4d2510f6ecc1e958b /src/newt | |
parent | 1ceebc122739b8a59b1bc099bc33ea880c837da9 (diff) |
NEWT: Changed Lifecycle of Display/Screen (part 3)
- Fix DefaultEDTUtil deadlocks: Minimize locking!
- invoke:
- Check isCurrentThreadEDT() before locking edtLock
- Check isRunning() redundant, since we start it beforehand
- EventDispatchThread.run():
- Relax definition of EDTUtil::waitUntilIdle(), ie method
may return while last task is being executed (see below).
- Execute task outside of edtTasks lock.
TODO: Fix more test cases (new and old)
Diffstat (limited to 'src/newt')
4 files changed, 33 insertions, 19 deletions
diff --git a/src/newt/classes/com/jogamp/newt/Display.java b/src/newt/classes/com/jogamp/newt/Display.java index 3d91a5f0f..5e3e32964 100644 --- a/src/newt/classes/com/jogamp/newt/Display.java +++ b/src/newt/classes/com/jogamp/newt/Display.java @@ -146,7 +146,7 @@ public abstract class Display { } } - protected boolean getShallRunOnEDT() { + protected boolean shallRunOnEDT() { return true; } @@ -154,7 +154,7 @@ public abstract class Display { if(NewtFactory.useEDT()) { if ( ! DEBUG_TEST_EDT_MAINTHREAD ) { Thread current = Thread.currentThread(); - edtUtil = new DefaultEDTUtil(current.getThreadGroup(), "Display_"+getFQName(), dispatchMessagesRunnable); + edtUtil = new DefaultEDTUtil(current.getThreadGroup(), "Display-"+getFQName(), dispatchMessagesRunnable); } else { // Begin JAU EDT Test .. MainThread.addPumpMessage(this, dispatchMessagesRunnable); @@ -172,7 +172,7 @@ public abstract class Display { } public void runOnEDTIfAvail(boolean wait, final Runnable task) { - if( getShallRunOnEDT() && null!=edtUtil ) { + if( shallRunOnEDT() && null!=edtUtil ) { edtUtil.invoke(wait, task); } else { task.run(); @@ -285,7 +285,7 @@ public abstract class Display { sb.append(type); sb.append("_"); sb.append(name); - sb.append("_"); + sb.append("-"); sb.append(id); return sb.toString(); } diff --git a/src/newt/classes/com/jogamp/newt/impl/awt/AWTDisplay.java b/src/newt/classes/com/jogamp/newt/impl/awt/AWTDisplay.java index c9c6c63fc..3290309bb 100644 --- a/src/newt/classes/com/jogamp/newt/impl/awt/AWTDisplay.java +++ b/src/newt/classes/com/jogamp/newt/impl/awt/AWTDisplay.java @@ -54,7 +54,7 @@ public class AWTDisplay extends Display { protected void closeNativeImpl() { } - protected boolean getShallRunOnEDT() { + protected boolean shallRunOnEDT() { return false; } protected void dispatchMessagesNative() { /* nop */ } diff --git a/src/newt/classes/com/jogamp/newt/util/DefaultEDTUtil.java b/src/newt/classes/com/jogamp/newt/util/DefaultEDTUtil.java index 4c0df11fc..47c089f87 100644 --- a/src/newt/classes/com/jogamp/newt/util/DefaultEDTUtil.java +++ b/src/newt/classes/com/jogamp/newt/util/DefaultEDTUtil.java @@ -54,9 +54,9 @@ public class DefaultEDTUtil implements EDTUtil { public DefaultEDTUtil(ThreadGroup tg, String name, Runnable pumpMessages) { this.threadGroup = tg; - this.name=new String(Thread.currentThread().getName()+"-"+"EDT-"+name); + this.name=new String(Thread.currentThread().getName()+"-EDT-"+name); this.pumpMessages=pumpMessages; - this.edt = new EventDispatchThread(threadGroup, name); + this.edt = new EventDispatchThread(threadGroup, name+"-EDT"); } public final void reset() { @@ -109,26 +109,24 @@ public class DefaultEDTUtil implements EDTUtil { RunnableTask rTask = null; Object rTaskLock = new Object(); synchronized(rTaskLock) { // lock the optional task execution - synchronized(edtLock) { // lock the EDT status - start(); // start if not started yet - if(isRunning() && edt != Thread.currentThread() ) { + if( isCurrentThreadEDT() ) { + wait = false; + task.run(); + } else { + synchronized(edtLock) { // lock the EDT status + start(); // start if not started yet rTask = new RunnableTask(task, wait?rTaskLock:null, true); synchronized(edtTasks) { edtTasks.add(rTask); edtTasks.notifyAll(); } - } else { - // if !running or isEDTThread, do it right away - wait = false; - task.run(); } } + // wait until task finished, if requested // and no stop() call slipped through. if( wait && isRunning() ) { try { - // JAU FIXME - System.out.println(Thread.currentThread()+": Wait on Task. EDT: "+edt); rTaskLock.wait(); } catch (InterruptedException ie) { throwable = ie; @@ -202,6 +200,7 @@ public class DefaultEDTUtil implements EDTUtil { pumpMessages.run(); } // wait and work on tasks + Runnable task = null; synchronized(edtTasks) { // wait for tasks while(!shouldStop && edtTasks.size()==0) { @@ -213,11 +212,13 @@ public class DefaultEDTUtil implements EDTUtil { } // execute one task, if available if(edtTasks.size()>0) { - Runnable task = (Runnable) edtTasks.remove(0); - task.run(); + task = (Runnable) edtTasks.remove(0); edtTasks.notifyAll(); } } + if(null!=task) { + task.run(); + } } while(!shouldStop || edtTasks.size()>0) ; } catch (Throwable t) { // handle errors .. diff --git a/src/newt/classes/com/jogamp/newt/util/EDTUtil.java b/src/newt/classes/com/jogamp/newt/util/EDTUtil.java index 91f35459b..969da6c2d 100644 --- a/src/newt/classes/com/jogamp/newt/util/EDTUtil.java +++ b/src/newt/classes/com/jogamp/newt/util/EDTUtil.java @@ -54,11 +54,24 @@ public interface EDTUtil { public boolean isRunning(); - /** Shall start the thread if not running */ + /** + * Add task to the EDT task queue. + * Wait until execution is finished if wait is true. + * Shall start the thread if not running + */ public void invoke(boolean wait, Runnable task); + /** + * Wait until EDT task queue, filled via invoke, is empty. + * It is allowed that the last task is still in execution + * when this method returns. + */ public void waitUntilIdle(); + /** + * Wait until EDT task has stopped. + * stop is not exected here and should be beforehand. + */ public void waitUntilStopped(); } |