diff options
Diffstat (limited to 'src/nativewindow/classes/com')
-rw-r--r-- | src/nativewindow/classes/com/sun/nativewindow/impl/LockingNativeWindowFactory.java (renamed from src/nativewindow/classes/com/sun/nativewindow/impl/x11/X11NativeWindowFactory.java) | 8 | ||||
-rw-r--r-- | src/nativewindow/classes/com/sun/nativewindow/impl/jvm/JVMUtil.java | 78 | ||||
-rw-r--r-- | src/nativewindow/classes/com/sun/nativewindow/impl/x11/X11Util.java | 126 | ||||
-rw-r--r-- | src/nativewindow/classes/com/sun/nativewindow/impl/x11/awt/X11AWTNativeWindowFactory.java | 2 |
4 files changed, 184 insertions, 30 deletions
diff --git a/src/nativewindow/classes/com/sun/nativewindow/impl/x11/X11NativeWindowFactory.java b/src/nativewindow/classes/com/sun/nativewindow/impl/LockingNativeWindowFactory.java index 1c67d6184..880fd8c7d 100644 --- a/src/nativewindow/classes/com/sun/nativewindow/impl/x11/X11NativeWindowFactory.java +++ b/src/nativewindow/classes/com/sun/nativewindow/impl/LockingNativeWindowFactory.java @@ -30,14 +30,12 @@ * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */ -package com.sun.nativewindow.impl.x11; +package com.sun.nativewindow.impl; import javax.media.nativewindow.*; -import com.sun.nativewindow.impl.*; -public class X11NativeWindowFactory extends NativeWindowFactoryImpl { - // On X11 platforms we need to do some locking; this basic - // implementation should suffice for some simple window toolkits +public class LockingNativeWindowFactory extends NativeWindowFactoryImpl { + // Provides a generic basic and recursive locking mechanism for your discretion. private ToolkitLock toolkitLock = new ToolkitLock() { private Thread owner; private int recursionCount; diff --git a/src/nativewindow/classes/com/sun/nativewindow/impl/jvm/JVMUtil.java b/src/nativewindow/classes/com/sun/nativewindow/impl/jvm/JVMUtil.java new file mode 100644 index 000000000..a4bffb100 --- /dev/null +++ b/src/nativewindow/classes/com/sun/nativewindow/impl/jvm/JVMUtil.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.sun.nativewindow.impl.jvm; + +import java.nio.ByteBuffer; +import com.sun.nativewindow.impl.*; + +/** + * Currently this tool works around the Hotspot race condition bugs: + <PRE> + 4395095 JNI access to java.nio DirectBuffer constructor/accessor + 6852404 Race condition in JNI Direct Buffer access and creation routines + </PRE> + * + * Make sure to initialize this class as soon as possible, + * before doing any multithreading work. + * + */ +public class JVMUtil { + private static final boolean DEBUG = Debug.debug("JVMUtil"); + + static { + initSingleton(); + } + + private static volatile boolean isInit = false; + + public static synchronized void initSingleton() { + if(isInit) return; + isInit=true; + + NativeLibLoaderBase.loadNativeWindow("jvm"); + + ByteBuffer buffer = InternalBufferUtil.newByteBuffer(64); + if( ! initialize(buffer) ) { + throw new RuntimeException("Failed to initialize the JVMUtil "+Thread.currentThread().getName()); + } + if(DEBUG) { + Exception e = new Exception("JVMUtil.initSingleton() .. initialized "+Thread.currentThread().getName()); + e.printStackTrace(); + } + } + + private JVMUtil() {} + + private static native boolean initialize(java.nio.ByteBuffer buffer); +} + diff --git a/src/nativewindow/classes/com/sun/nativewindow/impl/x11/X11Util.java b/src/nativewindow/classes/com/sun/nativewindow/impl/x11/X11Util.java index acd55a823..11343f3e1 100644 --- a/src/nativewindow/classes/com/sun/nativewindow/impl/x11/X11Util.java +++ b/src/nativewindow/classes/com/sun/nativewindow/impl/x11/X11Util.java @@ -33,6 +33,7 @@ package com.sun.nativewindow.impl.x11; import java.util.HashMap; +import java.util.Map; import javax.media.nativewindow.*; @@ -54,35 +55,112 @@ public class X11Util { private X11Util() {} - private static ThreadLocal currentDisplayAssociation = new ThreadLocal(); + private static ThreadLocal currentDisplayMap = new ThreadLocal(); - /** Returns the global static default display connection, read the toolkit lock/unlock - * requirements {@link X11Util above} for synchronization. */ - public static boolean isXineramaEnabledOnThreadLocalDefaultDisplay() { - long dpy = getThreadLocalDefaultDisplay(); - return X11Lib.XineramaEnabled(dpy); + public static class NamedDisplay implements Cloneable { + private String name; + private long handle; + + protected NamedDisplay(String name, long handle) { + this.name=name; + this.handle=handle; + } + + public String getName() { return name; } + public long getHandle() { return handle; } + + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + } + + /** Returns a clone of the thread local display map, you may {@link Object#wait()} on it */ + public static Map getCurrentDisplayMap() { + return (Map) ((HashMap)getCurrentDisplayMapImpl()).clone(); } - /** Returns this thread current default display. */ + /** Returns this thread current default display. If it doesn not exist, it is being created */ public static long getThreadLocalDefaultDisplay() { - Long dpyL = (Long) currentDisplayAssociation.get(); - if(null==dpyL) { - NativeWindowFactory.getDefaultFactory().getToolkitLock().lock(); - try { - long dpy = X11Lib.XOpenDisplay(null); - if(0==dpy) { - throw new NativeWindowException("Unable to create a default display connection on Thread "+Thread.currentThread().getName()); - } - dpyL = new Long(dpy); - currentDisplayAssociation.set( dpyL ); - if(DEBUG) { - Exception e = new Exception("Created new TLS display connection 0x"+Long.toHexString(dpy)+" for thread "+Thread.currentThread().getName()); - e.printStackTrace(); - } - } finally { - NativeWindowFactory.getDefaultFactory().getToolkitLock().unlock(); + return getThreadLocalDisplay(null); + } + + /** Returns this thread named display. If it doesn not exist, it is being created */ + public static long getThreadLocalDisplay(String name) { + NamedDisplay namedDpy = getCurrentDisplay(name); + if(null==namedDpy) { + long dpy = X11Lib.XOpenDisplay(name); + if(0==dpy) { + throw new NativeWindowException("X11Util.Display: Unable to create a display("+name+") connection in Thread "+Thread.currentThread().getName()); + } + namedDpy = new NamedDisplay(name, dpy); + setCurrentDisplay( namedDpy ); + if(DEBUG) { + Exception e = new Exception("X11Util.Display: Created new TLS display("+name+") connection 0x"+Long.toHexString(dpy)+" in thread "+Thread.currentThread().getName()); + e.printStackTrace(); } } - return dpyL.longValue(); + return namedDpy.getHandle(); } + + /** Closes this thread named display. It returns the handle of the closed display or 0, if it does not exist. */ + public static long closeThreadLocalDisplay(String name) { + NamedDisplay namedDpy = removeCurrentDisplay(name); + if(null==namedDpy) { + if(DEBUG) { + Exception e = new Exception("X11Util.Display: Display("+name+") with given handle is not mapped to TLS in thread "+Thread.currentThread().getName()); + e.printStackTrace(); + } + return 0; + } + long dpy = namedDpy.getHandle(); + X11Lib.XCloseDisplay(dpy); + if(DEBUG) { + Exception e = new Exception("X11Util.Display: Closed TLS Display("+name+") with handle 0x"+Long.toHexString(dpy)+" in thread "+Thread.currentThread().getName()); + e.printStackTrace(); + } + return dpy; + } + + private static Map getCurrentDisplayMapImpl() { + Map displayMap = (Map) currentDisplayMap.get(); + if(null==displayMap) { + displayMap = new HashMap(); + currentDisplayMap.set( displayMap ); + } + return displayMap; + } + + /** maps the given display to the thread local display map + * and notifies all threads synchronized to this display map. */ + private static NamedDisplay setCurrentDisplay(NamedDisplay newDisplay) { + Map displayMap = getCurrentDisplayMapImpl(); + NamedDisplay oldDisplay = null; + synchronized(displayMap) { + String name = (null==newDisplay.getName())?"nil":newDisplay.getName(); + oldDisplay = (NamedDisplay) displayMap.put(name, newDisplay); + displayMap.notifyAll(); + } + return oldDisplay; + } + + /** removes the mapping of the given name from the thread local display map + * and notifies all threads synchronized to this display map. */ + private static NamedDisplay removeCurrentDisplay(String name) { + Map displayMap = getCurrentDisplayMapImpl(); + NamedDisplay oldDisplay = null; + synchronized(displayMap) { + if(null==name) name="nil"; + oldDisplay = (NamedDisplay) displayMap.remove(name); + displayMap.notifyAll(); + } + return oldDisplay; + } + + /** Returns the thread local display mapped to the given name */ + private static NamedDisplay getCurrentDisplay(String name) { + if(null==name) name="nil"; + Map displayMap = getCurrentDisplayMapImpl(); + return (NamedDisplay) displayMap.get(name); + } + } diff --git a/src/nativewindow/classes/com/sun/nativewindow/impl/x11/awt/X11AWTNativeWindowFactory.java b/src/nativewindow/classes/com/sun/nativewindow/impl/x11/awt/X11AWTNativeWindowFactory.java index 53b82ce08..dff733735 100644 --- a/src/nativewindow/classes/com/sun/nativewindow/impl/x11/awt/X11AWTNativeWindowFactory.java +++ b/src/nativewindow/classes/com/sun/nativewindow/impl/x11/awt/X11AWTNativeWindowFactory.java @@ -42,7 +42,7 @@ import com.sun.nativewindow.impl.jawt.*; import com.sun.nativewindow.impl.jawt.x11.*; import com.sun.nativewindow.impl.x11.*; -public class X11AWTNativeWindowFactory extends X11NativeWindowFactory { +public class X11AWTNativeWindowFactory extends NativeWindowFactoryImpl { // When running the AWT on X11 platforms, we use the AWT native // interface (JAWT) to lock and unlock the toolkit |