From 0b43b43f889ad7fc220942b0076e2001ca3cf13f Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 28 Dec 2012 14:03:36 +0100 Subject: Refine AWTEDTExecutor.invoke(..): Allow control whether a non AWT-EDT may execute the runnable. For some 'rare' AWT/GL lifecycle actions, it is required to only run the command on the AWT-EDT, hence adding an argument determining the restriction. --- src/java/jogamp/common/awt/AWTEDTExecutor.java | 51 ++++++++++++++++++-------- 1 file changed, 36 insertions(+), 15 deletions(-) (limited to 'src/java/jogamp/common') diff --git a/src/java/jogamp/common/awt/AWTEDTExecutor.java b/src/java/jogamp/common/awt/AWTEDTExecutor.java index 67fd7e1..8d77f5c 100644 --- a/src/java/jogamp/common/awt/AWTEDTExecutor.java +++ b/src/java/jogamp/common/awt/AWTEDTExecutor.java @@ -63,27 +63,48 @@ public class AWTEDTExecutor implements RunnableExecutor { } /** - * Executes the given runnable on the AWT EDT if current thread is not the EDT and the given tree - * lock is not hold, otherwise execute the runnable in current thread. + * Executes the given runnable on the AWT-EDT and return true, if + * + *

+ * Otherwise execute the given runnable on the current-thread and return true, if + * allowOnNonEDT is true.
+ * This implies that the given tree-lock is being hold by the current-thread. + *

+ *

+ * Otherwise the runnable is not executed and false is returned. + *

+ * * @param treeLock representing the AWT-tree-lock, i.e. {@link java.awt.Component#getTreeLock()} + * @param allowOnNonEDT allow execution on non AWT-EDT in case current thread is not AWT-EDT and the tree-lock is being hold * @param wait if true method waits until {@link Runnable#run()} is completed, otherwise don't wait. * @param r the {@link Runnable} to be executed. + * @return true if the {@link Runnable} has been issued for execution, otherwise false */ - public void invoke(Object treeLock, boolean wait, Runnable r) { - if(EventQueue.isDispatchThread() || Thread.holdsLock(treeLock)) { + public boolean invoke(Object treeLock, boolean allowOnNonEDT, boolean wait, Runnable r) { + if( EventQueue.isDispatchThread() ) { r.run(); - } else { - try { - if(wait) { - EventQueue.invokeAndWait(r); - } else { - EventQueue.invokeLater(r); + return true; + } else if ( !Thread.holdsLock(treeLock) ) { + try { + if(wait) { + EventQueue.invokeAndWait(r); + } else { + EventQueue.invokeLater(r); + } + } catch (InvocationTargetException e) { + throw new RuntimeException(e.getTargetException()); + } catch (InterruptedException e) { + throw new RuntimeException(e); } - } catch (InvocationTargetException e) { - throw new RuntimeException(e.getTargetException()); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + return true; + } else if ( allowOnNonEDT ) { + r.run(); + return true; + } else { + return false; } } } -- cgit v1.2.3