From d7e07a5638d1dffe47df572ae071eb4dabe52426 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 17 Nov 2010 10:47:13 +0100 Subject: Move shutdown hook registration to GLDrawableFactory. Unregister the shutdown hook if called manually (recommended!). --- .../javax/media/opengl/GLDrawableFactory.java | 62 ++++++++++++++++++++-- src/jogl/classes/javax/media/opengl/GLProfile.java | 26 +-------- 2 files changed, 59 insertions(+), 29 deletions(-) (limited to 'src/jogl/classes/javax/media') diff --git a/src/jogl/classes/javax/media/opengl/GLDrawableFactory.java b/src/jogl/classes/javax/media/opengl/GLDrawableFactory.java index f6dce1c9c..ab23e18f8 100644 --- a/src/jogl/classes/javax/media/opengl/GLDrawableFactory.java +++ b/src/jogl/classes/javax/media/opengl/GLDrawableFactory.java @@ -97,10 +97,21 @@ public abstract class GLDrawableFactory { protected static ArrayList/**/ glDrawableFactories = new ArrayList(); + // Shutdown hook mechanism for the factory + private static boolean factoryShutdownHookRegistered = false; + private static Thread factoryShutdownHook = null; + /** * Instantiate singleton factories if available, EGLES1, EGLES2 and the OS native ones. */ static { + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + registerFactoryShutdownHook(); + return null; + } + }); + nativeOSType = NativeWindowFactory.getNativeWindowType(true); GLDrawableFactory tmp = null; @@ -151,15 +162,38 @@ public abstract class GLDrawableFactory { eglFactory = tmp; } - private AbstractGraphicsDevice defaultSharedDevice = null; + private static synchronized void registerFactoryShutdownHook() { + if (factoryShutdownHookRegistered) { + return; + } + factoryShutdownHook = new Thread(new Runnable() { + public void run() { + GLDrawableFactory.shutdownImpl(); + } + }); + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Runtime.getRuntime().addShutdownHook(factoryShutdownHook); + return null; + } + }); + factoryShutdownHookRegistered = true; + } - protected GLDrawableFactory() { - synchronized(glDrawableFactories) { - glDrawableFactories.add(this); + private static synchronized void unregisterFactoryShutdownHook() { + if (!factoryShutdownHookRegistered) { + return; } + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Runtime.getRuntime().removeShutdownHook(factoryShutdownHook); + return null; + } + }); + factoryShutdownHookRegistered = false; } - protected static void shutdown() { + private static void shutdownImpl() { synchronized(glDrawableFactories) { for(int i=0; i - * This method is called via the JVM shutdown hook.
* It releases all temporary created resources, ie issues {@link javax.media.opengl.GLDrawableFactory#shutdown()}.
+ * The shutdown implementation is called via the JVM shutdown hook, if not manually invoked here.
+ * Invoke shutdown() manually is recommended, due to the unreliable JVM state within the shutdown hook.
*/ public static synchronized void shutdown() { if(initialized) { @@ -1065,10 +1065,6 @@ public class GLProfile { static boolean initialized = false; - // Shutdown hook mechanism for the factory - private static boolean factoryShutdownHookRegistered = false; - private static Thread factoryShutdownHook = null; - /** * Tries the profiles implementation and native libraries. * Throws an GLException if no profile could be found at all. @@ -1299,24 +1295,6 @@ public class GLProfile { return defaultEGLDevice; } - private static synchronized void registerFactoryShutdownHook() { - if (factoryShutdownHookRegistered) { - return; - } - factoryShutdownHook = new Thread(new Runnable() { - public void run() { - GLDrawableFactory.shutdown(); - } - }); - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - Runtime.getRuntime().addShutdownHook(factoryShutdownHook); - return null; - } - }); - factoryShutdownHookRegistered = true; - } - private static void validateInitialization() { if(!initialized) { synchronized(GLProfile.class) { -- cgit v1.2.3