7155957: closed/java/awt/MenuBar/MenuBarStress1/MenuBarStress1.java hangs on win 64 bit with jdk8
authorssadetsky
Fri, 08 May 2015 17:35:15 +0300
changeset 30918 ee2374d4aae3
parent 30917 511aae7fee1a
child 30919 cfa6e12d8951
7155957: closed/java/awt/MenuBar/MenuBarStress1/MenuBarStress1.java hangs on win 64 bit with jdk8 Reviewed-by: serb, ant
jdk/src/java.desktop/share/classes/java/awt/Menu.java
jdk/src/java.desktop/share/classes/java/awt/MenuBar.java
jdk/src/java.desktop/share/classes/java/awt/MenuComponent.java
jdk/src/java.desktop/windows/classes/sun/awt/windows/WObjectPeer.java
jdk/src/java.desktop/windows/native/libawt/windows/awt_Menu.cpp
jdk/src/java.desktop/windows/native/libawt/windows/awt_MenuBar.cpp
jdk/src/java.desktop/windows/native/libawt/windows/awt_MenuItem.cpp
jdk/src/java.desktop/windows/native/libawt/windows/awt_new.cpp
--- a/jdk/src/java.desktop/share/classes/java/awt/Menu.java	Fri May 08 16:46:24 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/Menu.java	Fri May 08 17:35:15 2015 +0300
@@ -413,9 +413,9 @@
             items.removeElementAt(index);
             MenuPeer peer = (MenuPeer)this.peer;
             if (peer != null) {
+                peer.delItem(index);
                 mi.removeNotify();
                 mi.parent = null;
-                peer.delItem(index);
             }
         }
     }
--- a/jdk/src/java.desktop/share/classes/java/awt/MenuBar.java	Fri May 08 16:46:24 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/MenuBar.java	Fri May 08 17:35:15 2015 +0300
@@ -222,7 +222,6 @@
             if (m.parent != null) {
                 m.parent.remove(m);
             }
-            menus.addElement(m);
             m.parent = this;
 
             MenuBarPeer peer = (MenuBarPeer)this.peer;
@@ -232,6 +231,7 @@
                 }
                 peer.addMenu(m);
             }
+            menus.addElement(m);
             return m;
         }
     }
@@ -248,9 +248,9 @@
             menus.removeElementAt(index);
             MenuBarPeer peer = (MenuBarPeer)this.peer;
             if (peer != null) {
+                peer.delMenu(index);
                 m.removeNotify();
                 m.parent = null;
-                peer.delMenu(index);
             }
             if (helpMenu == m) {
                 helpMenu = null;
--- a/jdk/src/java.desktop/share/classes/java/awt/MenuComponent.java	Fri May 08 16:46:24 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/MenuComponent.java	Fri May 08 17:35:15 2015 +0300
@@ -77,7 +77,7 @@
      * @see #setFont(Font)
      * @see #getFont()
      */
-    Font font;
+    volatile Font font;
 
     /**
      * The menu component's name, which defaults to <code>null</code>.
@@ -302,11 +302,13 @@
      * @see       java.awt.font.TextAttribute
      */
     public void setFont(Font f) {
-        font = f;
-        //Fixed 6312943: NullPointerException in method MenuComponent.setFont(Font)
-        MenuComponentPeer peer = this.peer;
-        if (peer != null) {
-            peer.setFont(f);
+        synchronized (getTreeLock()) {
+            font = f;
+            //Fixed 6312943: NullPointerException in method MenuComponent.setFont(Font)
+            MenuComponentPeer peer = this.peer;
+            if (peer != null) {
+                peer.setFont(f);
+            }
         }
     }
 
--- a/jdk/src/java.desktop/windows/classes/sun/awt/windows/WObjectPeer.java	Fri May 08 16:46:24 2015 +0300
+++ b/jdk/src/java.desktop/windows/classes/sun/awt/windows/WObjectPeer.java	Fri May 08 17:35:15 2015 +0300
@@ -31,9 +31,9 @@
     }
 
     // The Windows handle for the native widget.
-    long pData;
+    volatile long pData;
     // if the native peer has been destroyed
-    boolean destroyed = false;
+    volatile boolean destroyed = false;
     // The associated AWT object.
     Object target;
 
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_Menu.cpp	Fri May 08 16:46:24 2015 +0300
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_Menu.cpp	Fri May 08 17:35:15 2015 +0300
@@ -239,6 +239,7 @@
     }
     jobject menuItem = env->CallObjectMethod(target, AwtMenu::getItemMID,
                                              index);
+    if (!menuItem) return NULL; // menu item was removed concurrently
     DASSERT(!safe_ExceptionOccurred(env));
 
     jobject wMenuItemPeer = GetPeerForTarget(env, menuItem);
@@ -264,9 +265,9 @@
     }
     /* target is a java.awt.Menu */
     jobject target = GetTarget(env);
-
+    if(!target || env->ExceptionCheck()) return;
     int nCount = CountItem(target);
-    for (int i = 0; i < nCount; i++) {
+    for (int i = 0; i < nCount && !env->ExceptionCheck(); i++) {
         AwtMenuItem* awtMenuItem = GetItem(target, i);
         if (awtMenuItem != NULL) {
             SendDrawItem(awtMenuItem, drawInfo);
@@ -294,8 +295,9 @@
     }
    /* target is a java.awt.Menu */
     jobject target = GetTarget(env);
+    if(!target || env->ExceptionCheck()) return;
     int nCount = CountItem(target);
-    for (int i = 0; i < nCount; i++) {
+    for (int i = 0; i < nCount && !env->ExceptionCheck(); i++) {
         AwtMenuItem* awtMenuItem = GetItem(target, i);
         if (awtMenuItem != NULL) {
             SendMeasureItem(awtMenuItem, hDC, measureInfo);
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_MenuBar.cpp	Fri May 08 16:46:24 2015 +0300
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_MenuBar.cpp	Fri May 08 17:35:15 2015 +0300
@@ -156,13 +156,16 @@
     }
 
     jobject menu = env->CallObjectMethod(target, AwtMenuBar::getMenuMID,index);
+    if (!menu) return NULL; // menu item was removed concurrently
     DASSERT(!safe_ExceptionOccurred(env));
 
     jobject menuItemPeer = GetPeerForTarget(env, menu);
     PDATA pData;
-    JNI_CHECK_PEER_RETURN_NULL(menuItemPeer);
+    JNI_CHECK_PEER_GOTO(menuItemPeer, done);
+
     AwtMenuItem* awtMenuItem = (AwtMenuItem*)pData;
 
+done:
     env->DeleteLocalRef(menu);
     env->DeleteLocalRef(menuItemPeer);
 
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_MenuItem.cpp	Fri May 08 16:46:24 2015 +0300
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_MenuItem.cpp	Fri May 08 17:35:15 2015 +0300
@@ -112,6 +112,7 @@
 
     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
     if (m_peerObject != NULL) {
+        JNI_SET_DESTROYED(m_peerObject);
         JNI_SET_PDATA(m_peerObject, NULL);
         env->DeleteGlobalRef(m_peerObject);
         m_peerObject = NULL;
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_new.cpp	Fri May 08 16:46:24 2015 +0300
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_new.cpp	Fri May 08 17:35:15 2015 +0300
@@ -172,6 +172,9 @@
             env->ExceptionClear();
             // rethrow exception
             env->Throw(xcp);
+            // temp solution to reveal all concurrency issues in jtreg and JCK
+            // we will switch it back to silent mode before the release
+            env->ExceptionDescribe();
             return xcp;
         }
     }