changeset 9139:34888f07a385

8167110: Windows peering issue 7155957: closed/java/awt/MenuBar/MenuBarStress1/MenuBarStress1.java hangs on win 64 bit with jdk8 8079595: Resizing dialog which is JWindow parent makes JVM crash 8147842: IME Composition Window is displayed at incorrect location Reviewed-by: serb
author mcherkas
date Thu, 20 Apr 2017 04:46:07 +0100
parents ec5259fc4b04
children e13b17f95b16
files src/share/classes/java/awt/Menu.java src/share/classes/java/awt/MenuBar.java src/share/classes/java/awt/MenuComponent.java src/windows/classes/sun/awt/windows/WMenuItemPeer.java src/windows/classes/sun/awt/windows/WObjectPeer.java src/windows/native/sun/windows/awt_Component.cpp src/windows/native/sun/windows/awt_Menu.cpp src/windows/native/sun/windows/awt_Menu.h src/windows/native/sun/windows/awt_MenuBar.cpp src/windows/native/sun/windows/awt_MenuBar.h src/windows/native/sun/windows/awt_MenuItem.cpp src/windows/native/sun/windows/awt_MenuItem.h src/windows/native/sun/windows/awt_PopupMenu.cpp src/windows/native/sun/windows/awt_PopupMenu.h src/windows/native/sun/windows/awt_new.cpp
diffstat 15 files changed, 232 insertions(+), 176 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/awt/Menu.java	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/share/classes/java/awt/Menu.java	Thu Apr 20 04:46:07 2017 +0100
@@ -411,9 +411,9 @@
             items.removeElementAt(index);
             MenuPeer peer = (MenuPeer)this.peer;
             if (peer != null) {
+                peer.delItem(index);
                 mi.removeNotify();
                 mi.parent = null;
-                peer.delItem(index);
             }
         }
     }
--- a/src/share/classes/java/awt/MenuBar.java	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/share/classes/java/awt/MenuBar.java	Thu Apr 20 04:46:07 2017 +0100
@@ -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);
             }
         }
     }
--- a/src/share/classes/java/awt/MenuComponent.java	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/share/classes/java/awt/MenuComponent.java	Thu Apr 20 04:46:07 2017 +0100
@@ -75,7 +75,7 @@
      * @see #setFont(Font)
      * @see #getFont()
      */
-    Font font;
+    volatile Font font;
 
     /**
      * The menu component's name, which defaults to <code>null</code>.
@@ -292,11 +292,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/src/windows/classes/sun/awt/windows/WMenuItemPeer.java	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/classes/sun/awt/windows/WMenuItemPeer.java	Thu Apr 20 04:46:07 2017 +0100
@@ -71,7 +71,7 @@
         enable(false);
     }
 
-    public void readShortcutLabel() {
+    private void readShortcutLabel() {
         //Fix for 6288578: PIT. Windows: Shortcuts displayed for the menuitems in a popup menu
         WMenuPeer ancestor = parent;
         while (ancestor != null && !(ancestor instanceof WMenuBarPeer)) {
@@ -115,7 +115,7 @@
         readShortcutLabel();
     }
 
-    protected void checkMenuCreation()
+    void checkMenuCreation()
     {
         // fix for 5088782: check if menu peer is created successfully
         if (pData == 0)
--- a/src/windows/classes/sun/awt/windows/WObjectPeer.java	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/classes/sun/awt/windows/WObjectPeer.java	Thu Apr 20 04:46:07 2017 +0100
@@ -34,16 +34,16 @@
     }
 
     // The Windows handle for the native widget.
-    long pData;
+    volatile long pData;
     // if the native peer has been destroyed
-    boolean destroyed = false;
+    private volatile boolean destroyed;
     // The associated AWT object.
-    Object target;
+    volatile Object target;
 
     private volatile boolean disposed;
 
     // set from JNI if any errors in creating the peer occur
-    protected Error createError = null;
+    volatile Error createError = null;
 
     // used to synchronize the state of this peer
     private final Object stateLock = new Object();
--- a/src/windows/native/sun/windows/awt_Component.cpp	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_Component.cpp	Thu Apr 20 04:46:07 2017 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -3747,7 +3747,10 @@
     HWND hWnd = GetHWnd();
     HWND hTop = GetTopLevelParentForWindow(hWnd);
     ::ClientToScreen(hTop, &p);
-
+    if (!m_bitsCandType) {
+        SetCandidateWindow(m_bitsCandType, x - p.x, y - p.y);
+        return;
+    }
     for (int iCandType=0; iCandType<32; iCandType++, bits<<=1) {
         if ( m_bitsCandType & bits )
             SetCandidateWindow(iCandType, x - p.x, y - p.y);
@@ -3765,7 +3768,7 @@
     HIMC hIMC = ImmGetContext(hwnd);
     CANDIDATEFORM cf;
     cf.dwIndex = iCandType;
-    cf.dwStyle = CFS_CANDIDATEPOS;
+    cf.dwStyle = CFS_POINT;
     cf.ptCurrentPos.x = x;
     cf.ptCurrentPos.y = y;
 
@@ -3797,8 +3800,12 @@
 
 MsgRouting AwtComponent::WmImeNotify(WPARAM subMsg, LPARAM bitsCandType)
 {
-    if (!m_useNativeCompWindow && subMsg == IMN_OPENCANDIDATE) {
-        m_bitsCandType = bitsCandType;
+    if (!m_useNativeCompWindow) {
+        if (subMsg == IMN_OPENCANDIDATE) {
+            m_bitsCandType = subMsg;
+        } else if (subMsg != IMN_SETCANDIDATEPOS) {
+            m_bitsCandType = 0;
+        }
         InquireCandidatePosition();
         return mrConsume;
     }
@@ -4053,14 +4060,14 @@
     return (HWND)NULL;
 }
 
-/* Call DefWindowProc for the focus proxy, if any */
+/* Redirects message to the focus proxy, if any */
 void AwtComponent::CallProxyDefWindowProc(UINT message, WPARAM wParam,
     LPARAM lParam, LRESULT &retVal, MsgRouting &mr)
 {
     if (mr != mrConsume)  {
         HWND proxy = GetProxyFocusOwner();
         if (proxy != NULL && ::IsWindowEnabled(proxy)) {
-            retVal = ComCtl32Util::GetInstance().DefWindowProc(NULL, proxy, message, wParam, lParam);
+            retVal = ::DefWindowProc(proxy, message, wParam, lParam);
             mr = mrConsume;
         }
     }
@@ -4139,7 +4146,7 @@
     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 
     if (drawInfo.CtlType == ODT_MENU) {
-        if (drawInfo.itemData != 0) {
+        if (IsMenu((HMENU)drawInfo.hwndItem) && drawInfo.itemData != 0) {
             AwtMenu* menu = (AwtMenu*)(drawInfo.itemData);
             menu->DrawItem(drawInfo);
         }
--- a/src/windows/native/sun/windows/awt_Menu.cpp	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_Menu.cpp	Thu Apr 20 04:46:07 2017 +0100
@@ -34,6 +34,13 @@
 /* IMPORTANT! Read the README.JNI file for notes on JNI converted AWT code.
  */
 
+/***********************************************************************/
+// struct for _DelItem() method
+struct DelItemStruct {
+    jobject menuitem;
+    jint index;
+};
+
 /************************************************************************
  * AwtMenuItem fields
  */
@@ -64,7 +71,6 @@
         ::DestroyMenu(m_hMenu);
         m_hMenu = NULL;
     }
-
     AwtMenuItem::Dispose();
 }
 
@@ -73,7 +79,7 @@
 }
 
 /* Create a new AwtMenu object and menu.   */
-AwtMenu* AwtMenu::Create(jobject self, AwtMenu* parentMenu)
+AwtMenu* AwtMenu::Create(jobject self, jobject parent)
 {
     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 
@@ -85,6 +91,9 @@
             return NULL;
         }
 
+        JNI_CHECK_NULL_GOTO(parent, "peer", done);
+        AwtMenu* parentMenu = (AwtMenu*) JNI_GET_PDATA(parent);
+
         target = env->GetObjectField(self, AwtObject::targetID);
         JNI_CHECK_NULL_GOTO(target, "null target", done);
 
@@ -119,6 +128,46 @@
     return menu;
 }
 
+void AwtMenu::_AddSeparator(void *param)
+{
+    if (AwtToolkit::IsMainThread()) {
+        JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
+
+        jobject self = (jobject)param;
+        AwtMenu *m = NULL;
+        PDATA pData;
+        JNI_CHECK_PEER_GOTO(self, ret);
+        m = (AwtMenu *)pData;
+        m->AddSeparator();
+ret:
+        env->DeleteGlobalRef(self);
+    } else {
+        AwtToolkit::GetInstance().InvokeFunction(AwtMenu::_AddSeparator, param);
+    }
+}
+
+void AwtMenu::_DelItem(void *param)
+{
+    if (AwtToolkit::IsMainThread()) {
+        JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
+
+        DelItemStruct *dis = (DelItemStruct*) param;
+        jobject self = dis->menuitem;
+        jint index = dis->index;
+
+        AwtMenu *m = NULL;
+        PDATA pData;
+        JNI_CHECK_PEER_GOTO(self, ret);
+        m = (AwtMenu *)pData;
+        m->DeleteItem(static_cast<UINT>(index));
+ret:
+        env->DeleteGlobalRef(self);
+        delete dis;
+    } else {
+        AwtToolkit::GetInstance().InvokeFunction(AwtMenu::_DelItem, param);
+    }
+}
+
 void AwtMenu::UpdateLayout()
 {
     UpdateLayout(GetHMenu());
@@ -239,6 +288,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 +314,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 +344,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);
@@ -321,24 +372,6 @@
     return (GetMenuBar() == GetMenuContainer());
 }
 
-LRESULT AwtMenu::WinThreadExecProc(ExecuteArgs * args)
-{
-    switch( args->cmdId ) {
-        case MENU_ADDSEPARATOR:
-            this->AddSeparator();
-            break;
-
-        case MENU_DELITEM:
-            this->DeleteItem(static_cast<UINT>(args->param1));
-            break;
-
-        default:
-            AwtMenuItem::WinThreadExecProc(args);
-            break;
-    }
-    return 0L;
-}
-
 /************************************************************************
  * WMenuPeer native methods
  */
@@ -379,15 +412,14 @@
 {
     TRY;
 
-    PDATA pData;
-    JNI_CHECK_PEER_RETURN(self);
+    jobject selfGlobalRef = env->NewGlobalRef(self);
 
-    AwtObject::WinThreadExec(self, AwtMenu::MENU_ADDSEPARATOR);
+    AwtToolkit::GetInstance().SyncCall(AwtMenu::_AddSeparator, selfGlobalRef);
+    // selfGlobalRef is deleted in _AddSeparator
 
     CATCH_BAD_ALLOC;
 }
 
-
 /*
  * Class:     sun_awt_windows_WMenuPeer
  * Method:    delItem
@@ -399,10 +431,12 @@
 {
     TRY;
 
-    PDATA pData;
-    JNI_CHECK_PEER_RETURN(self);
+    DelItemStruct *dis = new DelItemStruct;
+    dis->menuitem = env->NewGlobalRef(self);
+    dis->index = index;
 
-    AwtObject::WinThreadExec(self, AwtMenu::MENU_DELITEM, index);
+    AwtToolkit::GetInstance().SyncCall(AwtMenu::_DelItem, dis);
+    // global refs and dis are deleted in _DelItem
 
     CATCH_BAD_ALLOC;
 }
@@ -418,13 +452,8 @@
 {
     TRY;
 
-    PDATA pData;
-    JNI_CHECK_PEER_RETURN(menuBar);
-
-    AwtMenuBar* awtMenuBar = (AwtMenuBar *)pData;
-    AwtToolkit::CreateComponent(self, awtMenuBar,
-                                (AwtToolkit::ComponentFactory)AwtMenu::Create,FALSE);
-    JNI_CHECK_PEER_CREATION_RETURN(self);
+    AwtToolkit::CreateComponent(self, menuBar,
+                                (AwtToolkit::ComponentFactory)AwtMenu::Create);
 
     CATCH_BAD_ALLOC;
 }
@@ -440,13 +469,8 @@
 {
     TRY;
 
-    PDATA pData;
-    JNI_CHECK_PEER_RETURN(menu);
-
-    AwtMenu* awtMenu = (AwtMenu *)pData;
-    AwtToolkit::CreateComponent(self, awtMenu,
-                                (AwtToolkit::ComponentFactory)AwtMenu::Create,FALSE);
-    JNI_CHECK_PEER_CREATION_RETURN(self);
+    AwtToolkit::CreateComponent(self, menu,
+                                (AwtToolkit::ComponentFactory)AwtMenu::Create);
 
     CATCH_BAD_ALLOC;
 }
--- a/src/windows/native/sun/windows/awt_Menu.h	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_Menu.h	Thu Apr 20 04:46:07 2017 +0100
@@ -42,13 +42,6 @@
 
 class AwtMenu : public AwtMenuItem {
 public:
-    // id's for methods executed on toolkit thread
-    enum {
-        MENU_ADDSEPARATOR = MENUITEM_LAST+1,
-        MENU_DELITEM,
-        MENU_LAST
-    };
-
     /* method ids for java.awt.Menu */
     static jmethodID countItemsMID;
     static jmethodID getItemMID;
@@ -61,7 +54,7 @@
     virtual LPCTSTR GetClassName();
 
     /* Create a new AwtMenu.  This must be run on the main thread. */
-    static AwtMenu* Create(jobject self, AwtMenu* parentMenu);
+    static AwtMenu* Create(jobject self, jobject parent);
 
     INLINE HMENU GetHMenu() { return m_hMenu; }
     INLINE void SetHMenu(HMENU hMenu) {
@@ -94,9 +87,9 @@
     void MeasureItem(HDC hDC, MEASUREITEMSTRUCT& measureInfo);
     void MeasureItems(HDC hDC, MEASUREITEMSTRUCT& measureInfo);
 
-    virtual LRESULT WinThreadExecProc(ExecuteArgs * args);
-
     // invoked on Toolkit thread
+    static void _AddSeparator(void *param);
+    static void _DelItem(void *param);
     static void _CreateMenu(void *param);
     static void _CreateSubMenu(void *param);
     virtual BOOL IsSeparator() { return FALSE; }
--- a/src/windows/native/sun/windows/awt_MenuBar.cpp	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_MenuBar.cpp	Thu Apr 20 04:46:07 2017 +0100
@@ -30,6 +30,12 @@
  */
 
 /***********************************************************************/
+// struct for _DelItem() method
+struct DelItemStruct {
+    jobject menuitem;
+    jint index;
+};
+/***********************************************************************/
 // struct for _AddMenu() method
 struct AddMenuStruct {
     jobject menubar;
@@ -130,18 +136,6 @@
         return myFrame->GetHWnd();
 }
 
-void AwtMenuBar::SendDrawItem(AwtMenuItem* awtMenuItem,
-                              DRAWITEMSTRUCT& drawInfo)
-{
-    awtMenuItem->DrawItem(drawInfo);
-}
-
-void AwtMenuBar::SendMeasureItem(AwtMenuItem* awtMenuItem,
-                                 HDC hDC, MEASUREITEMSTRUCT& measureInfo)
-{
-    awtMenuItem->MeasureItem(hDC, measureInfo);
-}
-
 int AwtMenuBar::CountItem(jobject menuBar)
 {
     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
@@ -159,13 +153,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);
-    AwtMenuItem* awtMenuItem = (AwtMenuItem*)pData;
+    AwtMenuItem* awtMenuItem = NULL;
+    JNI_CHECK_PEER_GOTO(menuItemPeer, done);
+    awtMenuItem = (AwtMenuItem*)pData;
 
+done:
     env->DeleteLocalRef(menu);
     env->DeleteLocalRef(menuItemPeer);
 
@@ -212,20 +209,6 @@
     VERIFY(::DrawMenuBar(GetOwnerHWnd()));
 }
 
-LRESULT AwtMenuBar::WinThreadExecProc(ExecuteArgs * args)
-{
-    switch( args->cmdId ) {
-        case MENUBAR_DELITEM:
-            this->DeleteItem(static_cast<UINT>(args->param1));
-            break;
-
-        default:
-            AwtMenu::WinThreadExecProc(args);
-            break;
-    }
-    return 0L;
-}
-
 void AwtMenuBar::_AddMenu(void *param)
 {
     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
@@ -254,6 +237,28 @@
     delete ams;
 }
 
+void AwtMenuBar::_DelItem(void *param)
+{
+    if (AwtToolkit::IsMainThread()) {
+        JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
+
+        DelItemStruct *dis = (DelItemStruct*) param;
+        jobject self = dis->menuitem;
+        jint index = dis->index;
+
+        AwtMenuBar *m = NULL;
+        PDATA pData;
+        JNI_CHECK_PEER_GOTO(self, ret);
+        m = (AwtMenuBar *)pData;
+        m->DeleteItem(static_cast<UINT>(index));
+ret:
+        env->DeleteGlobalRef(self);
+        delete dis;
+    } else {
+        AwtToolkit::GetInstance().InvokeFunction(AwtMenuBar::_DelItem, param);
+    }
+}
+
 /************************************************************************
  * MenuBar native methods
  */
@@ -320,9 +325,12 @@
 {
     TRY;
 
-    PDATA pData;
-    JNI_CHECK_PEER_RETURN(self);
-    AwtObject::WinThreadExec(self, AwtMenuBar::MENUBAR_DELITEM, (LPARAM)index);
+    DelItemStruct *dis = new DelItemStruct;
+    dis->menuitem = env->NewGlobalRef(self);
+    dis->index = index;
+
+    AwtToolkit::GetInstance().SyncCall(AwtMenuBar::_DelItem, dis);
+    // global refs and dis are deleted in _DelItem
 
     CATCH_BAD_ALLOC;
 }
@@ -341,9 +349,6 @@
     AwtToolkit::CreateComponent(self, frame,
                                 (AwtToolkit::ComponentFactory)
                                 AwtMenuBar::Create);
-    PDATA pData;
-    JNI_CHECK_PEER_CREATION_RETURN(self);
-
     CATCH_BAD_ALLOC;
 }
 
--- a/src/windows/native/sun/windows/awt_MenuBar.h	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_MenuBar.h	Thu Apr 20 04:46:07 2017 +0100
@@ -42,10 +42,6 @@
 
 class AwtMenuBar : public AwtMenu {
 public:
-    // id's for methods executed on toolkit thread
-    enum MenuExecIds {
-        MENUBAR_DELITEM = MENU_LAST+1
-    };
 
     /* java.awt.MenuBar method ids */
     static jmethodID getMenuCountMID;
@@ -73,20 +69,15 @@
     AwtMenuItem* GetItem(jobject target, long index);
     int CountItem(jobject menuBar);
 
-    void SendDrawItem(AwtMenuItem* awtMenuItem,
-                      DRAWITEMSTRUCT& drawInfo);
-    void SendMeasureItem(AwtMenuItem* awtMenuItem,
-                         HDC hDC, MEASUREITEMSTRUCT& measureInfo);
     void DrawItem(DRAWITEMSTRUCT& drawInfo);
     void MeasureItem(HDC hDC, MEASUREITEMSTRUCT& measureInfo);
 
     void AddItem(AwtMenuItem* item);
     void DeleteItem(UINT index);
 
-    virtual LRESULT WinThreadExecProc(ExecuteArgs * args);
-
     // called on Toolkit thread
     static void _AddMenu(void *param);
+    static void _DelItem(void *param);
 protected:
     AwtFrame* m_frame;
 };
--- a/src/windows/native/sun/windows/awt_MenuItem.cpp	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_MenuItem.cpp	Thu Apr 20 04:46:07 2017 +0100
@@ -50,6 +50,16 @@
     jobject menuitem;
     jstring label;
 };
+// struct for _SetEnable() method
+struct SetEnableStruct {
+    jobject menuitem;
+    jboolean isEnabled;
+};
+// struct for _setState() method
+struct SetStateStruct {
+    jobject menuitem;
+    jboolean isChecked;
+};
 /************************************************************************
  * AwtMenuItem fields
  */
@@ -104,6 +114,7 @@
 {
     if (m_freeId) {
         AwtToolkit::GetInstance().RemoveCmdID( GetID() );
+        m_freeId = FALSE;
     }
 }
 void AwtMenuItem::Dispose()
@@ -112,6 +123,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;
@@ -199,13 +211,12 @@
         if (env->EnsureLocalCapacity(1) < 0) {
             return NULL;
         }
-        PDATA pData;
-        JNI_CHECK_PEER_RETURN_NULL(menuPeer);
+        JNI_CHECK_NULL_RETURN_NULL(menuPeer, "peer");
 
         /* target is a java.awt.MenuItem  */
         target = env->GetObjectField(peer, AwtObject::targetID);
 
-        AwtMenu* menu = (AwtMenu *)pData;
+        AwtMenu* menu = (AwtMenu *)JNI_GET_PDATA(menuPeer);
         item = new AwtMenuItem();
         jboolean isCheckbox =
             (jboolean)env->GetBooleanField(peer, AwtMenuItem::isCheckboxID);
@@ -216,7 +227,9 @@
         item->LinkObjects(env, peer);
         item->SetMenuContainer(menu);
         item->SetNewID();
-        menu->AddItem(item);
+        if (menu != NULL) {
+            menu->AddItem(item);
+        }
     } catch (...) {
         env->DeleteLocalRef(target);
         throw;
@@ -692,30 +705,6 @@
     }
 }
 
-LRESULT AwtMenuItem::WinThreadExecProc(ExecuteArgs * args)
-{
-    switch( args->cmdId ) {
-        case MENUITEM_ENABLE:
-        {
-            BOOL        isEnabled = (BOOL)args->param1;
-            this->Enable(isEnabled);
-        }
-        break;
-
-        case MENUITEM_SETSTATE:
-        {
-            BOOL        isChecked = (BOOL)args->param1;
-            this->SetState(isChecked);
-        }
-        break;
-
-        default:
-            AwtObject::WinThreadExecProc(args);
-            break;
-    }
-    return 0L;
-}
-
 void AwtMenuItem::_SetLabel(void *param) {
     if (AwtToolkit::IsMainThread()) {
         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
@@ -811,6 +800,53 @@
     }
 }
 
+void AwtMenuItem::_SetEnable(void *param)
+{
+    if (AwtToolkit::IsMainThread()) {
+        JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
+
+        SetEnableStruct *ses = (SetEnableStruct*) param;
+        jobject self = ses->menuitem;
+        jboolean isEnabled = ses->isEnabled;
+
+        AwtMenuItem *m = NULL;
+
+        PDATA pData;
+        JNI_CHECK_PEER_GOTO(self, ret);
+
+        m = (AwtMenuItem *)pData;
+
+        m->Enable(isEnabled);
+ret:
+        env->DeleteGlobalRef(self);
+        delete ses;
+    } else {
+        AwtToolkit::GetInstance().InvokeFunction(AwtMenuItem::_SetEnable, param);
+    }
+}
+
+void AwtMenuItem::_SetState(void *param)
+{
+    if (AwtToolkit::IsMainThread()) {
+        JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
+
+        SetStateStruct *sts = (SetStateStruct*) param;
+        jobject self = sts->menuitem;
+        jboolean isChecked = sts->isChecked;
+
+        AwtMenuItem *m = NULL;
+
+        PDATA pData;
+        JNI_CHECK_PEER_GOTO(self, ret);
+        m = (AwtMenuItem *)pData;
+        m->SetState(isChecked);
+ret:
+        env->DeleteGlobalRef(self);
+        delete sts;
+    } else {
+        AwtToolkit::GetInstance().InvokeFunction(AwtMenuItem::_SetState, param);
+    }
+}
 BOOL AwtMenuItem::IsSeparator() {
     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
     if (env->EnsureLocalCapacity(2) < 0) {
@@ -981,13 +1017,9 @@
 {
     TRY;
 
-    JNI_CHECK_NULL_RETURN(menu, "null Menu");
     AwtToolkit::CreateComponent(self, menu,
                                 (AwtToolkit::ComponentFactory)
                                 AwtMenuItem::Create);
-    PDATA pData;
-    JNI_CHECK_PEER_CREATION_RETURN(self);
-
     CATCH_BAD_ALLOC;
 }
 
@@ -1002,9 +1034,12 @@
 {
     TRY;
 
-    PDATA pData;
-    JNI_CHECK_PEER_RETURN(self);
-    AwtObject::WinThreadExec(self, AwtMenuItem::MENUITEM_ENABLE, (LPARAM)on );
+    SetEnableStruct *ses = new SetEnableStruct;
+    ses->menuitem = env->NewGlobalRef(self);
+    ses->isEnabled = on;
+
+    AwtToolkit::GetInstance().SyncCall(AwtMenuItem::_SetEnable, ses);
+    // global refs and ses are deleted in _SetEnable
 
     CATCH_BAD_ALLOC;
 }
@@ -1043,9 +1078,12 @@
 {
     TRY;
 
-    PDATA pData;
-    JNI_CHECK_PEER_RETURN(self);
-    AwtObject::WinThreadExec(self, AwtMenuItem::MENUITEM_SETSTATE, (LPARAM)on);
+    SetStateStruct *sts = new SetStateStruct;
+    sts->menuitem = env->NewGlobalRef(self);
+    sts->isChecked = on;
+
+    AwtToolkit::GetInstance().SyncCall(AwtMenuItem::_SetState, sts);
+    // global refs and sts are deleted in _SetState
 
     CATCH_BAD_ALLOC;
 }
--- a/src/windows/native/sun/windows/awt_MenuItem.h	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_MenuItem.h	Thu Apr 20 04:46:07 2017 +0100
@@ -46,13 +46,6 @@
 
 class AwtMenuItem : public AwtObject {
 public:
-    // id's for methods executed on toolkit thread
-    enum {
-        MENUITEM_ENABLE,
-        MENUITEM_SETSTATE,
-        MENUITEM_LAST
-    };
-
     /* java.awt.MenuComponent fields */
     static jfieldID fontID;
     static jfieldID appContextID;
@@ -155,13 +148,14 @@
      */
     MsgRouting WmNotify(UINT notifyCode);
 
-    virtual LRESULT WinThreadExecProc(ExecuteArgs * args);
     virtual BOOL IsDisabledAndPopup() {
         return FALSE;
     }
     virtual BOOL IsSeparator();
 
     // invoked on Toolkit thread
+    static void _SetState(void *param);
+    static void _SetEnable(void *param);
     static void _SetLabel(void *param);
     static void _UpdateLayout(void *param);
 
--- a/src/windows/native/sun/windows/awt_PopupMenu.cpp	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_PopupMenu.cpp	Thu Apr 20 04:46:07 2017 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -66,7 +66,7 @@
 }
 
 /* Create a new AwtPopupMenu object and menu.   */
-AwtPopupMenu* AwtPopupMenu::Create(jobject self, AwtComponent* parent)
+AwtPopupMenu* AwtPopupMenu::Create(jobject self, jobject parent)
 {
     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 
@@ -78,6 +78,9 @@
             return NULL;
         }
 
+        JNI_CHECK_NULL_GOTO(parent, "peer", done);
+        AwtComponent* awtParent = (AwtComponent*) JNI_GET_PDATA(parent);
+
         target = env->GetObjectField(self, AwtObject::targetID);
         JNI_CHECK_NULL_GOTO(target, "null target", done);
 
@@ -95,7 +98,7 @@
         popupMenu->SetHMenu(hMenu);
 
         popupMenu->LinkObjects(env, self);
-        popupMenu->SetParent(parent);
+        popupMenu->SetParent(awtParent);
     } catch (...) {
         env->DeleteLocalRef(target);
         throw;
@@ -277,12 +280,8 @@
 {
     TRY;
 
-    PDATA pData;
-    JNI_CHECK_PEER_RETURN(parent);
-    AwtComponent* awtParent = (AwtComponent *)pData;
     AwtToolkit::CreateComponent(
-        self, awtParent, (AwtToolkit::ComponentFactory)AwtPopupMenu::Create, FALSE);
-    JNI_CHECK_PEER_CREATION_RETURN(self);
+        self, parent, (AwtToolkit::ComponentFactory)AwtPopupMenu::Create);
 
     CATCH_BAD_ALLOC;
 }
--- a/src/windows/native/sun/windows/awt_PopupMenu.h	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_PopupMenu.h	Thu Apr 20 04:46:07 2017 +0100
@@ -48,7 +48,7 @@
     virtual LPCTSTR GetClassName();
 
     /* Create a new AwtPopupMenu.  This must be run on the main thread. */
-    static AwtPopupMenu* Create(jobject self, AwtComponent* parent);
+    static AwtPopupMenu* Create(jobject self, jobject parent);
 
     /* Display the popup modally. */
     void Show(JNIEnv *env, jobject event, BOOL isTrayIconPopup);
--- a/src/windows/native/sun/windows/awt_new.cpp	Fri Apr 24 19:44:15 2015 +0300
+++ b/src/windows/native/sun/windows/awt_new.cpp	Thu Apr 20 04:46:07 2017 +0100
@@ -177,6 +177,9 @@
         } else {
             // 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;
         }
     }