diff options
author | Sven Gothel <[email protected]> | 2011-02-26 21:43:20 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-02-26 21:43:20 +0100 |
commit | 76444dce2b678a7f6769564abac4f8a73f414609 (patch) | |
tree | 09358c0a48715c69e7e8f511cf3d9be729177509 /src/nativewindow | |
parent | 77546f8968779fbdcfe58f89c6924803642889c7 (diff) |
Clean/Fix: Threading Code
- Remove unsafe double checked locking
- Annotate safe double checked locking (volatile)
- use 'static final' if possible
Diffstat (limited to 'src/nativewindow')
4 files changed, 40 insertions, 21 deletions
diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/NativeWindowVersion.java b/src/nativewindow/classes/com/jogamp/nativewindow/NativeWindowVersion.java index ccb2176fc..38bd70a90 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/NativeWindowVersion.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/NativeWindowVersion.java @@ -42,7 +42,7 @@ public class NativeWindowVersion extends JogampVersion { } public static NativeWindowVersion getInstance() { - if(null == jogampCommonVersionInfo) { + if(null == jogampCommonVersionInfo) { // volatile: ok synchronized(NativeWindowVersion.class) { if( null == jogampCommonVersionInfo ) { final String packageName = "javax.media.nativewindow"; diff --git a/src/nativewindow/classes/javax/media/nativewindow/awt/AWTWindowClosingProtocol.java b/src/nativewindow/classes/javax/media/nativewindow/awt/AWTWindowClosingProtocol.java index 7112f944d..e7db942e4 100644 --- a/src/nativewindow/classes/javax/media/nativewindow/awt/AWTWindowClosingProtocol.java +++ b/src/nativewindow/classes/javax/media/nativewindow/awt/AWTWindowClosingProtocol.java @@ -51,6 +51,7 @@ public class AWTWindowClosingProtocol implements WindowClosingProtocol { } class WindowClosingAdapter extends WindowAdapter { + @Override public void windowClosing(WindowEvent e) { int op = AWTWindowClosingProtocol.this.getDefaultCloseOperation(); @@ -80,7 +81,7 @@ public class AWTWindowClosingProtocol implements WindowClosingProtocol { * @return */ public final boolean addClosingListenerOneShot() { - if(!closingListenerSet) { + if(!closingListenerSet) { // volatile: ok synchronized(closingListenerLock) { if(!closingListenerSet) { closingListenerSet=addClosingListenerImpl(); @@ -92,7 +93,7 @@ public class AWTWindowClosingProtocol implements WindowClosingProtocol { } public final boolean removeClosingListener() { - if(closingListenerSet) { + if(closingListenerSet) { // volatile: ok synchronized(closingListenerLock) { if(closingListenerSet) { Window w = AWTMisc.getWindow(comp); diff --git a/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java b/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java index d5811d9c5..80dd5c402 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/jawt/JAWTUtil.java @@ -60,13 +60,23 @@ public class JAWTUtil { private static final Method isQueueFlusherThread; private static final boolean j2dExist; - private static Class sunToolkitClass; - private static Method sunToolkitAWTLockMethod; - private static Method sunToolkitAWTUnlockMethod; - private static boolean hasSunToolkitAWTLock; + private static final Class sunToolkitClass; + private static final Method sunToolkitAWTLockMethod; + private static final Method sunToolkitAWTUnlockMethod; + private static final boolean hasSunToolkitAWTLock; private static final JAWTToolkitLock jawtToolkitLock; + private static class PrivilegedDataBlob1 { + PrivilegedDataBlob1() { + ok = false; + } + Class sunToolkitClass; + Method sunToolkitAWTLockMethod; + Method sunToolkitAWTUnlockMethod; + boolean ok; + } + static { JAWTJNILibLoader.loadAWTImpl(); JAWTJNILibLoader.loadNativeWindow("awt"); @@ -87,22 +97,28 @@ public class JAWTUtil { isQueueFlusherThread = m; j2dExist = ok; - AccessController.doPrivileged(new PrivilegedAction() { + PrivilegedDataBlob1 pdb1 = (PrivilegedDataBlob1) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { - try { - sunToolkitClass = Class.forName("sun.awt.SunToolkit"); - sunToolkitAWTLockMethod = sunToolkitClass.getDeclaredMethod("awtLock", new Class[]{}); - sunToolkitAWTLockMethod.setAccessible(true); - sunToolkitAWTUnlockMethod = sunToolkitClass.getDeclaredMethod("awtUnlock", new Class[]{}); - sunToolkitAWTUnlockMethod.setAccessible(true); + PrivilegedDataBlob1 d = new PrivilegedDataBlob1(); + try { + d.sunToolkitClass = Class.forName("sun.awt.SunToolkit"); + d.sunToolkitAWTLockMethod = sunToolkitClass.getDeclaredMethod("awtLock", new Class[]{}); + d.sunToolkitAWTLockMethod.setAccessible(true); + d.sunToolkitAWTUnlockMethod = sunToolkitClass.getDeclaredMethod("awtUnlock", new Class[]{}); + d.sunToolkitAWTUnlockMethod.setAccessible(true); + d.ok=true; } catch (Exception e) { // Either not a Sun JDK or the interfaces have changed since 1.4.2 / 1.5 } - return null; + return d; } }); + sunToolkitClass = pdb1.sunToolkitClass; + sunToolkitAWTLockMethod = pdb1.sunToolkitAWTLockMethod; + sunToolkitAWTUnlockMethod = pdb1.sunToolkitAWTUnlockMethod; + boolean _hasSunToolkitAWTLock = false; - if (null != sunToolkitAWTLockMethod && null != sunToolkitAWTUnlockMethod) { + if ( pdb1.ok ) { try { sunToolkitAWTLockMethod.invoke(null, (Object[])null); sunToolkitAWTUnlockMethod.invoke(null, (Object[])null); @@ -152,11 +168,11 @@ public class JAWTUtil { } - public static final boolean hasJava2D() { + public static boolean hasJava2D() { return j2dExist; } - public static final boolean isJava2DQueueFlusherThread() { + public static boolean isJava2DQueueFlusherThread() { boolean b = false; if(j2dExist) { try { diff --git a/src/nativewindow/classes/jogamp/nativewindow/x11/X11Util.java b/src/nativewindow/classes/jogamp/nativewindow/x11/X11Util.java index 4098e101b..052a9934e 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/x11/X11Util.java +++ b/src/nativewindow/classes/jogamp/nativewindow/x11/X11Util.java @@ -54,7 +54,7 @@ public class X11Util { private static final boolean DEBUG = Debug.debug("X11Util"); private static final boolean TRACE_DISPLAY_LIFECYCLE = Debug.getBooleanProperty("nativewindow.debug.X11Util.TraceDisplayLifecycle", true, AccessController.getContext()); - private static String nullDisplayName = null; + private static volatile String nullDisplayName = null; private static boolean isFirstX11ActionOnProcess = false; private static boolean isInit = false; @@ -117,7 +117,7 @@ public class X11Util { } public static String getNullDisplayName() { - if(null==nullDisplayName) { + if(null==nullDisplayName) { // volatile: ok synchronized(X11Util.class) { if(null==nullDisplayName) { NativeWindowFactory.getDefaultToolkitLock().lock(); @@ -174,17 +174,19 @@ public class X11Util { public final Throwable getCreationStack() { return creationStack; } + @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } + @Override public String toString() { return "NamedX11Display["+name+", 0x"+Long.toHexString(handle)+", refCount "+refCount+", unCloseable "+unCloseable+"]"; } } /** Returns the number of unclosed X11 Displays. - * @param realXCloseAndPendingDisplays if true, {@link #closePendingDisplayConnections()} is called. + * @param realXCloseOpenAndPendingDisplays if true, {@link #closePendingDisplayConnections()} is called. */ public static int shutdown(boolean realXCloseOpenAndPendingDisplays, boolean verbose) { int num=0; |