8078606: Deadlock in awt clipboard
authoranashaty
Thu, 04 Jun 2015 15:24:30 +0300
changeset 31169 0647b79041f2
parent 31168 3b8f5ca8938f
child 31170 4d414cfb92cc
8078606: Deadlock in awt clipboard Reviewed-by: azvegint, bae
jdk/src/java.desktop/unix/classes/sun/awt/X11/XClipboard.java
jdk/src/java.desktop/unix/classes/sun/awt/X11/XRootWindow.java
--- a/jdk/src/java.desktop/unix/classes/sun/awt/X11/XClipboard.java	Wed Jun 03 18:26:06 2015 -0700
+++ b/jdk/src/java.desktop/unix/classes/sun/awt/X11/XClipboard.java	Thu Jun 04 15:24:30 2015 +0300
@@ -156,19 +156,24 @@
         isSelectionNotifyProcessed = true;
 
         boolean mustSchedule = false;
-        synchronized (XClipboard.classLock) {
-            if (targetsAtom2Clipboard == null) {
-                targetsAtom2Clipboard = new HashMap<Long, XClipboard>(2);
+        XToolkit.awtLock();
+        try {
+            synchronized (XClipboard.classLock) {
+                if (targetsAtom2Clipboard == null) {
+                    targetsAtom2Clipboard = new HashMap<Long, XClipboard>(2);
+                }
+                mustSchedule = targetsAtom2Clipboard.isEmpty();
+                targetsAtom2Clipboard.put(getTargetsPropertyAtom().getAtom(), this);
+                if (mustSchedule) {
+                    XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
+                                                new SelectionNotifyHandler());
+                }
             }
-            mustSchedule = targetsAtom2Clipboard.isEmpty();
-            targetsAtom2Clipboard.put(getTargetsPropertyAtom().getAtom(), this);
             if (mustSchedule) {
-                XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
-                                            new SelectionNotifyHandler());
+                XToolkit.schedule(new CheckChangeTimerTask(), XClipboard.getPollInterval());
             }
-        }
-        if (mustSchedule) {
-            XToolkit.schedule(new CheckChangeTimerTask(), XClipboard.getPollInterval());
+        } finally {
+            XToolkit.awtUnlock();
         }
     }
 
--- a/jdk/src/java.desktop/unix/classes/sun/awt/X11/XRootWindow.java	Wed Jun 03 18:26:06 2015 -0700
+++ b/jdk/src/java.desktop/unix/classes/sun/awt/X11/XRootWindow.java	Thu Jun 04 15:24:30 2015 +0300
@@ -31,18 +31,22 @@
  * common logical ancestor
  */
 class XRootWindow extends XBaseWindow {
-    private static XRootWindow xawtRootWindow = null;
-    static XRootWindow getInstance() {
-        XToolkit.awtLock();
-        try {
-            if (xawtRootWindow == null) {
+    private static class LazyHolder {
+        private static final XRootWindow xawtRootWindow;
+
+        static {
+            XToolkit.awtLock();
+            try {
                 xawtRootWindow = new XRootWindow();
                 xawtRootWindow.init(xawtRootWindow.getDelayedParams().delete(DELAYED));
+            } finally {
+                XToolkit.awtUnlock();
             }
-            return xawtRootWindow;
-        } finally {
-            XToolkit.awtUnlock();
         }
+
+    }
+    static XRootWindow getInstance() {
+        return LazyHolder.xawtRootWindow;
     }
 
     private XRootWindow() {