From 1113d44644469eaae69fd11fe7108ab432e4cf38 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 1 Dec 2011 16:51:00 +0100 Subject: GLProfile: use dbl checked locking w/ volatile ; Proper shutdown sequence. --- src/jogl/classes/javax/media/opengl/GLProfile.java | 62 +++++++++++++--------- 1 file changed, 36 insertions(+), 26 deletions(-) (limited to 'src/jogl/classes/javax/media/opengl') diff --git a/src/jogl/classes/javax/media/opengl/GLProfile.java b/src/jogl/classes/javax/media/opengl/GLProfile.java index f9e47f2db..6591f4ae1 100644 --- a/src/jogl/classes/javax/media/opengl/GLProfile.java +++ b/src/jogl/classes/javax/media/opengl/GLProfile.java @@ -109,24 +109,30 @@ public class GLProfile { * @param firstUIActionOnProcess Should be true if called before the first UI action of the running program, * otherwise false. */ - public static synchronized void initSingleton(final boolean firstUIActionOnProcess) { - if(!initialized) { - initialized = true; - // run the whole static initialization privileged to speed up, - // since this skips checking further access - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - if(TempJarCache.isInitialized()) { - String[] atomicNativeJarBaseNames = new String[] { "nativewindow", "jogl", null }; - if( ReflectionUtil.isClassAvailable("com.jogamp.newt.NewtFactory", GLProfile.class.getClassLoader()) ) { - atomicNativeJarBaseNames[2] = "newt"; - } - JNILibLoaderBase.addNativeJarLibs(GLProfile.class, "jogl.all", "jogl-all", atomicNativeJarBaseNames); - } - initProfilesForDefaultDevices(firstUIActionOnProcess); - return null; + public static void initSingleton(final boolean firstUIActionOnProcess) { + if(!initialized) { // volatile: ok + synchronized(GLProfile.class) { + if(!initialized) { + initialized = true; + Platform.initSingleton(); + + // run the whole static initialization privileged to speed up, + // since this skips checking further access + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + if(TempJarCache.isInitialized()) { + String[] atomicNativeJarBaseNames = new String[] { "nativewindow", "jogl", null }; + if( ReflectionUtil.isClassAvailable("com.jogamp.newt.NewtFactory", GLProfile.class.getClassLoader()) ) { + atomicNativeJarBaseNames[2] = "newt"; + } + JNILibLoaderBase.addNativeJarLibs(GLProfile.class, "jogl-all", atomicNativeJarBaseNames); + } + initProfilesForDefaultDevices(firstUIActionOnProcess); + return null; + } + }); } - }); + } } } @@ -147,12 +153,16 @@ public class GLProfile { * 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) { - initialized = false; - NativeWindowFactory.shutdown(); - GLDrawableFactory.shutdown(); - GLContext.shutdown(); + public static void shutdown() { + if(initialized) { // volatile: ok + synchronized(GLProfile.class) { + if(initialized) { + initialized = false; + GLDrawableFactory.shutdown(); // may utilize static GLContext mappings + GLContext.shutdown(); // does not utilize shared resources of GLDrawableFactory + NativeWindowFactory.shutdown(); + } + } } } @@ -1157,7 +1167,7 @@ public class GLProfile { private static /*final*/ AbstractGraphicsDevice defaultDesktopDevice; private static /*final*/ AbstractGraphicsDevice defaultEGLDevice; - static boolean initialized = false; + private static volatile boolean initialized = false; /** * Tries the profiles implementation and native libraries. @@ -1193,7 +1203,7 @@ public class GLProfile { // - Instantiate GLDrawableFactory incl its shared dummy drawable/context, // which will register at GLContext .. // - GLDrawableFactory.initialize(); + GLDrawableFactory.initSingleton(); Throwable t=null; // if successfull it has a shared dummy drawable and context created @@ -1451,7 +1461,7 @@ public class GLProfile { } private static void validateInitialization() { - if(!initialized) { + if(!initialized) { // volatile: ok synchronized(GLProfile.class) { if(!initialized) { initSingleton(false); -- cgit v1.2.3