From b823e8a0e18ec0b357d62af468a14162a9fb4948 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 19 Mar 2012 17:14:15 +0100 Subject: Fix / Cleanup AWT related Threading code: Using enums, properly disable singlethreading if requested, fix doc: -Dopengl.1thread -> -Djogl.1thread --- .../classes/com/jogamp/opengl/swt/GLCanvas.java | 4 +- src/jogl/classes/javax/media/opengl/Threading.java | 16 +++--- src/jogl/classes/jogamp/opengl/ThreadingImpl.java | 63 +++++++++++----------- .../jogamp/opengl/awt/AWTThreadingPlugin.java | 10 ++-- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/jogl/classes/com/jogamp/opengl/swt/GLCanvas.java b/src/jogl/classes/com/jogamp/opengl/swt/GLCanvas.java index 42097bd6d..b4c7cddf0 100644 --- a/src/jogl/classes/com/jogamp/opengl/swt/GLCanvas.java +++ b/src/jogl/classes/com/jogamp/opengl/swt/GLCanvas.java @@ -480,7 +480,7 @@ public class GLCanvas extends Canvas implements GLAutoDrawable { */ protected boolean isRenderThread() { if (Threading.isSingleThreaded()) { - if (ThreadingImpl.getMode() != ThreadingImpl.WORKER) { + if (ThreadingImpl.getMode() != ThreadingImpl.Mode.ST_WORKER) { final Display display = getDisplay(); return display != null && display.getThread() == Thread.currentThread(); } @@ -521,7 +521,7 @@ public class GLCanvas extends Canvas implements GLAutoDrawable { * The non-null action to dispatch. */ private void runInDesignatedGLThread(final Runnable makeCurrentAndRunAction) { - if (ThreadingImpl.getMode() != ThreadingImpl.WORKER) { + if (ThreadingImpl.getMode() != ThreadingImpl.Mode.ST_WORKER) { final Display display = getDisplay(); assert display.getThread() != Thread.currentThread() : "Incorrect use of thread dispatching."; display.syncExec(makeCurrentAndRunAction); diff --git a/src/jogl/classes/javax/media/opengl/Threading.java b/src/jogl/classes/javax/media/opengl/Threading.java index d0f3ebb93..4871c1dd7 100644 --- a/src/jogl/classes/javax/media/opengl/Threading.java +++ b/src/jogl/classes/javax/media/opengl/Threading.java @@ -101,17 +101,17 @@ import jogamp.opengl.*; In addition to specifying programmatically whether the single thread for OpenGL work is enabled, users may switch it on and off - using the system property opengl.1thread. Valid values + using the system property jogl.1thread. Valid values for this system property are:
-    -Dopengl.1thread=false     Disable single-threading of OpenGL work
-    -Dopengl.1thread=true      Enable single-threading of OpenGL work (default -- on a newly-created worker thread)
-    -Dopengl.1thread=auto      Select default single-threading behavior (currently on)
-    -Dopengl.1thread=awt       Enable single-threading of OpenGL work on AWT event dispatch thread (current default on all
-                                 platforms, and also the default behavior older releases)
-    -Dopengl.1thread=worker    Enable single-threading of OpenGL work on newly-created worker thread (not suitable for Mac
-                                 OS X or X11 platforms, and risky on Windows in applet environments)
+    -Djogl.1thread=false     Disable single-threading of OpenGL work, hence use multithreading.
+    -Djogl.1thread=true      Enable single-threading of OpenGL work (default -- on a newly-created worker thread)
+    -Djogl.1thread=auto      Select default single-threading behavior (currently on)
+    -Djogl.1thread=awt       Enable single-threading of OpenGL work on AWT event dispatch thread (current default on all
+                             platforms, and also the default behavior older releases)
+    -Djogl.1thread=worker    Enable single-threading of OpenGL work on newly-created worker thread (not suitable for Mac
+                             OS X or X11 platforms, and risky on Windows in applet environments)
     
*/ diff --git a/src/jogl/classes/jogamp/opengl/ThreadingImpl.java b/src/jogl/classes/jogamp/opengl/ThreadingImpl.java index 07d5c3402..0ba86870d 100644 --- a/src/jogl/classes/jogamp/opengl/ThreadingImpl.java +++ b/src/jogl/classes/jogamp/opengl/ThreadingImpl.java @@ -47,13 +47,20 @@ import javax.media.opengl.GLProfile; /** Implementation of the {@link javax.media.opengl.Threading} class. */ public class ThreadingImpl { - public static final int AWT = 1; - public static final int WORKER = 2; + public enum Mode { + MT(0), ST_AWT(1), ST_WORKER(2); + + public final int id; + + Mode(int id){ + this.id = id; + } + } protected static final boolean DEBUG = Debug.debug("Threading"); private static boolean singleThreaded = true; - private static int mode; + private static Mode mode = Mode.MT; private static boolean hasAWT; // We need to know whether we're running on X11 platforms to change // our behavior when the Java2D/JOGL bridge is active @@ -65,7 +72,11 @@ public class ThreadingImpl { threadingPlugin = AccessController.doPrivileged(new PrivilegedAction() { public ThreadingPlugin run() { - String workaround = Debug.getProperty("jogl.1thread", true); + final String workaround; + { + final String w = Debug.getProperty("jogl.1thread", true); + workaround = null != w ? w.toLowerCase() : null; + } ClassLoader cl = ThreadingImpl.class.getClassLoader(); // Default to using the AWT thread on all platforms except // Windows. On OS X there is instability apparently due to @@ -80,50 +91,48 @@ public class ThreadingImpl { String osType = NativeWindowFactory.getNativeWindowType(false); _isX11 = NativeWindowFactory.TYPE_X11.equals(osType); - int defaultMode = ( hasAWT ? AWT : WORKER ); - - mode = defaultMode; + mode = ( hasAWT ? Mode.ST_AWT : Mode.ST_WORKER ); // default + if (workaround != null) { - workaround = workaround.toLowerCase(); if (workaround.equals("true") || workaround.equals("auto")) { // Nothing to do; singleThreaded and mode already set up } else if (workaround.equals("worker")) { singleThreaded = true; - mode = WORKER; + mode = Mode.ST_WORKER; } else if (hasAWT && workaround.equals("awt")) { singleThreaded = true; - mode = AWT; + mode = Mode.ST_AWT; } else { singleThreaded = false; + mode = Mode.MT; } } - printWorkaroundNotice(); - + ThreadingPlugin threadingPlugin=null; - if(hasAWT) { + if(Mode.ST_AWT == mode) { // try to fetch the AWTThreadingPlugin Exception error=null; try { threadingPlugin = (ThreadingPlugin) ReflectionUtil.createInstance("jogamp.opengl.awt.AWTThreadingPlugin", cl); } catch (JogampRuntimeException jre) { error = jre; } - if(AWT == mode && null==threadingPlugin) { + if(null==threadingPlugin) { throw new GLException("Mode is AWT, but class 'jogamp.opengl.awt.AWTThreadingPlugin' is not available", error); } } + if(DEBUG) { + System.err.println("Threading: jogl.1thread "+workaround+", singleThreaded "+singleThreaded+", hasAWT "+hasAWT+", mode "+mode+", plugin "+threadingPlugin); + } return threadingPlugin; } }); - if(DEBUG) { - System.err.println("Threading: hasAWT "+hasAWT+", mode "+((mode==AWT)?"AWT":"WORKER")+", plugin "+threadingPlugin); - } } /** No reason to ever instantiate this class */ private ThreadingImpl() {} public static boolean isX11() { return _isX11; } - public static int getMode() { return mode; } + public static Mode getMode() { return mode; } /** If an implementation of the javax.media.opengl APIs offers a multithreading option but the default behavior is single-threading, @@ -165,9 +174,9 @@ public class ThreadingImpl { } switch (mode) { - case AWT: + case ST_AWT: throw new InternalError(); - case WORKER: + case ST_WORKER: return GLWorkerThread.isWorkerThread(); default: throw new InternalError("Illegal single-threading mode " + mode); @@ -198,10 +207,10 @@ public class ThreadingImpl { } switch (mode) { - case AWT: + case ST_AWT: throw new InternalError(); - case WORKER: + case ST_WORKER: GLWorkerThread.start(); // singleton start via volatile-dbl-checked-locking try { GLWorkerThread.invokeAndWait(r); @@ -220,14 +229,6 @@ public class ThreadingImpl { /** This is a workaround for AWT-related deadlocks which only seem to show up in the context of applets */ public static boolean isAWTMode() { - return (mode == AWT); - } - - private static void printWorkaroundNotice() { - if (singleThreaded && Debug.verbose()) { - System.err.println("Using " + - (mode == AWT ? "AWT" : "OpenGL worker") + - " thread for performing OpenGL work in javax.media.opengl implementation"); - } + return (mode == Mode.ST_AWT); } } diff --git a/src/jogl/classes/jogamp/opengl/awt/AWTThreadingPlugin.java b/src/jogl/classes/jogamp/opengl/awt/AWTThreadingPlugin.java index dd493f5ee..901146fc4 100644 --- a/src/jogl/classes/jogamp/opengl/awt/AWTThreadingPlugin.java +++ b/src/jogl/classes/jogamp/opengl/awt/AWTThreadingPlugin.java @@ -42,8 +42,6 @@ package jogamp.opengl.awt; import javax.media.opengl.*; -import java.awt.event.*; - import java.awt.EventQueue; import java.lang.reflect.InvocationTargetException; @@ -55,14 +53,14 @@ public class AWTThreadingPlugin implements ThreadingPlugin { public boolean isOpenGLThread() throws GLException { switch (ThreadingImpl.getMode()) { - case ThreadingImpl.AWT: + case ST_AWT: // FIXME: See the FIXME below in 'invokeOnOpenGLThread' if (Java2D.isOGLPipelineActive() && !ThreadingImpl.isX11()) { return Java2D.isQueueFlusherThread(); } else { return EventQueue.isDispatchThread(); } - case ThreadingImpl.WORKER: + case ST_WORKER: if (Java2D.isOGLPipelineActive()) { // FIXME: ideally only the QFT would be considered to be the // "OpenGL thread", but we can not currently run all of @@ -80,7 +78,7 @@ public class AWTThreadingPlugin implements ThreadingPlugin { public void invokeOnOpenGLThread(Runnable r) throws GLException { switch (ThreadingImpl.getMode()) { - case ThreadingImpl.AWT: + case ST_AWT: // FIXME: ideally should run all OpenGL work on the Java2D QFT // thread when it's enabled, but unfortunately there are // deadlock issues on X11 platforms when making our @@ -102,7 +100,7 @@ public class AWTThreadingPlugin implements ThreadingPlugin { } break; - case ThreadingImpl.WORKER: + case ST_WORKER: GLWorkerThread.start(); // singleton start via volatile-dbl-checked-locking try { GLWorkerThread.invokeAndWait(r); -- cgit v1.2.3