diff options
author | Sven Gothel <[email protected]> | 2010-11-17 10:47:13 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2010-11-17 10:47:13 +0100 |
commit | d7e07a5638d1dffe47df572ae071eb4dabe52426 (patch) | |
tree | 6df84ea71fa0f6f71cff7d3d4644e3372a9d32f0 /src/jogl/classes | |
parent | b0b1e3fb9c0f915cdf8d237c0f61a9d08ca83b01 (diff) |
Move shutdown hook registration to GLDrawableFactory.
Unregister the shutdown hook if called manually (recommended!).
Diffstat (limited to 'src/jogl/classes')
-rw-r--r-- | src/jogl/classes/javax/media/opengl/GLDrawableFactory.java | 62 | ||||
-rw-r--r-- | src/jogl/classes/javax/media/opengl/GLProfile.java | 26 |
2 files changed, 59 insertions, 29 deletions
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/*<GLDrawableFactoryImpl>*/ 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<glDrawableFactories.size(); i++) { GLDrawableFactory factory = (GLDrawableFactory) glDrawableFactories.get(i); @@ -169,6 +203,24 @@ public abstract class GLDrawableFactory { } } + protected static void shutdown() { + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + unregisterFactoryShutdownHook(); + return null; + } + }); + shutdownImpl(); + } + + private AbstractGraphicsDevice defaultSharedDevice = null; + + protected GLDrawableFactory() { + synchronized(glDrawableFactories) { + glDrawableFactories.add(this); + } + } + protected abstract void shutdownInstance(); /** diff --git a/src/jogl/classes/javax/media/opengl/GLProfile.java b/src/jogl/classes/javax/media/opengl/GLProfile.java index 878c4024e..02252ac33 100644 --- a/src/jogl/classes/javax/media/opengl/GLProfile.java +++ b/src/jogl/classes/javax/media/opengl/GLProfile.java @@ -103,7 +103,6 @@ public class GLProfile { // since this skips checking further access AccessController.doPrivileged(new PrivilegedAction() { public Object run() { - registerFactoryShutdownHook(); initProfilesForDefaultDevices(firstUIActionOnProcess); return null; } @@ -122,8 +121,9 @@ public class GLProfile { /** * Manual shutdown method, may be called after your last JOGL use * within the running JVM.<br> - * This method is called via the JVM shutdown hook.<br> * It releases all temporary created resources, ie issues {@link javax.media.opengl.GLDrawableFactory#shutdown()}.<br> + * The shutdown implementation is called via the JVM shutdown hook, if not manually invoked here.<br> + * Invoke <code>shutdown()</code> manually is recommended, due to the unreliable JVM state within the shutdown hook.<br> */ 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) { |