changeset 6816:cd2d8f23e93c

8025775: JNI warnings in TryXShmAttach Reviewed-by: art, anthony
author alitvinov
date Thu, 19 Dec 2013 15:21:08 +0400
parents ad8490675e4c
children 3ff488283a24
files src/solaris/classes/sun/awt/X11/XConstants.java src/solaris/classes/sun/awt/X11/XErrorHandler.java src/solaris/native/sun/awt/awt_GraphicsEnv.c src/solaris/native/sun/awt/awt_GraphicsEnv.h src/solaris/native/sun/awt/awt_util.c src/solaris/native/sun/awt/awt_util.h src/solaris/native/sun/awt/awt_wm.c src/solaris/native/sun/awt/awt_xembed_server.c src/solaris/native/sun/java2d/opengl/GLXSurfaceData.c src/solaris/native/sun/java2d/x11/X11SurfaceData.c src/solaris/native/sun/xawt/XlibWrapper.c
diffstat 11 files changed, 177 insertions(+), 258 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/classes/sun/awt/X11/XConstants.java	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/classes/sun/awt/X11/XConstants.java	Thu Dec 19 15:21:08 2013 +0400
@@ -699,9 +699,4 @@
     public static final long XkbModifierMapMask = (1L<<2);
     public static final long XkbVirtualModsMask = (1L<<6); //server map
 
-    /*****************************************************************
-     * X SHARED MEMORY EXTENSION FUNCTIONS
-     *****************************************************************/
-
-    public static final int X_ShmAttach = 1;
 }
--- a/src/solaris/classes/sun/awt/X11/XErrorHandler.java	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/classes/sun/awt/X11/XErrorHandler.java	Thu Dec 19 15:21:08 2013 +0400
@@ -42,29 +42,6 @@
         }
     }
 
-    /**
-     * This is a base synthetic error handler containing a boolean flag which allows
-     * to show that an error is handled or not.
-     */
-    public static class XErrorHandlerWithFlag extends XBaseErrorHandler {
-        private volatile boolean errorOccurred = false;
-
-        public boolean getErrorOccurredFlag() {
-            return errorOccurred;
-        }
-
-        /**
-         * Sets an internal boolean flag to a particular value. Should be always called
-         * with {@code false} value of the parameter {@code errorOccurred} before this
-         * error handler is set as current.
-         * @param errorOccurred {@code true} to indicate that an error was handled,
-         *     {@code false} to reset the internal boolean flag
-         */
-        public void setErrorOccurredFlag(boolean errorOccurred) {
-            this.errorOccurred = errorOccurred;
-        }
-    }
-
     /*
      * Instead of validating window id, we simply call XGetWindowProperty,
      * but temporary install this function as the error handler to ignore
@@ -99,75 +76,4 @@
             return theInstance;
         }
     }
-
-    /**
-     * This is a synthetic error handler for errors generated by the native function
-     * {@code XShmAttach}. If an error is handled, an internal boolean flag of the
-     * handler is set to {@code true}.
-     */
-    public static final class XShmAttachHandler extends XErrorHandlerWithFlag {
-        private XShmAttachHandler() {}
-
-        @Override
-        public int handleError(long display, XErrorEvent err) {
-            if (err.get_minor_code() == XConstants.X_ShmAttach) {
-                setErrorOccurredFlag(true);
-                return 0;
-            }
-            return super.handleError(display, err);
-        }
-
-        // Shared instance
-        private static XShmAttachHandler theInstance = new XShmAttachHandler();
-        public static XShmAttachHandler getInstance() {
-            return theInstance;
-        }
-    }
-
-    /**
-     * This is a synthetic error handler for {@code BadAlloc} errors generated by the
-     * native {@code glX*} functions. Its internal boolean flag is set to {@code true},
-     * if an error is handled.
-     */
-    public static final class GLXBadAllocHandler extends XErrorHandlerWithFlag {
-        private GLXBadAllocHandler() {}
-
-        @Override
-        public int handleError(long display, XErrorEvent err) {
-            if (err.get_error_code() == XConstants.BadAlloc) {
-                setErrorOccurredFlag(true);
-                return 0;
-            }
-            return super.handleError(display, err);
-        }
-
-        private static GLXBadAllocHandler theInstance = new GLXBadAllocHandler();
-        public static GLXBadAllocHandler getInstance() {
-            return theInstance;
-        }
-    }
-
-    /**
-     * This is a synthetic error handler for errors generated by the native function
-     * {@code XChangeWindowAttributes}. If an error is handled, an internal boolean
-     * flag of the handler is set to {@code true}.
-     */
-    public static final class XChangeWindowAttributesHandler extends XErrorHandlerWithFlag {
-        private XChangeWindowAttributesHandler() {}
-
-        @Override
-        public int handleError(long display, XErrorEvent err) {
-            if ((err.get_request_code() == XProtocolConstants.X_ChangeWindowAttributes) &&
-                (err.get_error_code() == XConstants.BadAccess)) {
-                setErrorOccurredFlag(true);
-                return 0;
-            }
-            return super.handleError(display, err);
-        }
-
-        private static XChangeWindowAttributesHandler theInstance = new XChangeWindowAttributesHandler();
-        public static XChangeWindowAttributesHandler getInstance() {
-            return theInstance;
-        }
-    }
 }
--- a/src/solaris/native/sun/awt/awt_GraphicsEnv.c	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/awt/awt_GraphicsEnv.c	Thu Dec 19 15:21:08 2013 +0400
@@ -978,6 +978,20 @@
 
 static jint canUseShmExt = UNSET_MITSHM;
 static jint canUseShmExtPixmaps = UNSET_MITSHM;
+static jboolean xshmAttachFailed = JNI_FALSE;
+
+int XShmAttachXErrHandler(Display *display, XErrorEvent *xerr) {
+    if (xerr->minor_code == X_ShmAttach) {
+        xshmAttachFailed = JNI_TRUE;
+    }
+    return 0;
+}
+jboolean isXShmAttachFailed() {
+    return xshmAttachFailed;
+}
+void resetXShmAttachFailed() {
+    xshmAttachFailed = JNI_FALSE;
+}
 
 extern int mitShmPermissionMask;
 
@@ -985,7 +999,6 @@
     XShmSegmentInfo shminfo;
     int XShmMajor, XShmMinor;
     int a, b, c;
-    jboolean xShmAttachResult;
 
     AWT_LOCK();
     if (canUseShmExt != UNSET_MITSHM) {
@@ -1024,14 +1037,21 @@
         }
         shminfo.readOnly = True;
 
-        xShmAttachResult = TryXShmAttach(env, awt_display, &shminfo);
+        resetXShmAttachFailed();
+        /**
+         * The J2DXErrHandler handler will set xshmAttachFailed
+         * to JNI_TRUE if any Shm error has occured.
+         */
+        EXEC_WITH_XERROR_HANDLER(XShmAttachXErrHandler,
+                                 XShmAttach(awt_display, &shminfo));
+
         /**
          * Get rid of the id now to reduce chances of leaking
          * system resources.
          */
         shmctl(shminfo.shmid, IPC_RMID, 0);
 
-        if (xShmAttachResult == JNI_TRUE) {
+        if (isXShmAttachFailed() == JNI_FALSE) {
             canUseShmExt = CAN_USE_MITSHM;
             /* check if we can use shared pixmaps */
             XShmQueryVersion(awt_display, &XShmMajor, &XShmMinor,
@@ -1046,23 +1066,6 @@
     }
     AWT_UNLOCK();
 }
-
-/*
- * Must be called with the acquired AWT lock.
- */
-jboolean TryXShmAttach(JNIEnv *env, Display *display, XShmSegmentInfo *shminfo) {
-    jboolean errorOccurredFlag = JNI_FALSE;
-    jobject errorHandlerRef;
-
-    /*
-     * XShmAttachHandler will set its internal flag to JNI_TRUE, if any Shm error occurs.
-     */
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$XShmAttachHandler",
-        "()Lsun/awt/X11/XErrorHandler$XShmAttachHandler;", JNI_TRUE,
-        errorHandlerRef, errorOccurredFlag,
-        XShmAttach(display, shminfo));
-    return errorOccurredFlag == JNI_FALSE ? JNI_TRUE : JNI_FALSE;
-}
 #endif /* MITSHM */
 
 /*
--- a/src/solaris/native/sun/awt/awt_GraphicsEnv.h	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/awt/awt_GraphicsEnv.h	Thu Dec 19 15:21:08 2013 +0400
@@ -53,7 +53,8 @@
 extern int XShmQueryExtension();
 
 void TryInitMITShm(JNIEnv *env, jint *shmExt, jint *shmPixmaps);
-jboolean TryXShmAttach(JNIEnv *env, Display *display, XShmSegmentInfo *shminfo);
+void resetXShmAttachFailed();
+jboolean isXShmAttachFailed();
 
 #endif /* MITSHM */
 
--- a/src/solaris/native/sun/awt/awt_util.c	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/awt/awt_util.c	Thu Dec 19 15:21:08 2013 +0400
@@ -57,6 +57,16 @@
 
 #include "java_awt_event_MouseWheelEvent.h"
 
+/*
+ * Called by "ToolkitErrorHandler" function in "XlibWrapper.c" file.
+ */
+XErrorHandler current_native_xerror_handler = NULL;
+
+/*
+ * A place for error handler to report the error code.
+ */
+unsigned char xerror_code = Success;
+
 extern jint getModifiers(uint32_t state, jint button, jint keyCode);
 extern jint getButton(uint32_t button);
 
--- a/src/solaris/native/sun/awt/awt_util.h	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/awt/awt_util.h	Thu Dec 19 15:21:08 2013 +0400
@@ -79,62 +79,42 @@
 void awt_util_delEmbeddedFrame(Widget embeddedFrame);
 Boolean awt_util_processEventForEmbeddedFrame(XEvent *ev);
 
-/*
- * Expected types of arguments of the macro.
- * (JNIEnv*, const char*, const char*, jboolean, jobject)
- */
-#define WITH_XERROR_HANDLER(env, handlerClassName, getInstanceSignature,                          \
-                            handlerHasFlag, handlerRef) do {                                      \
-    handlerRef = JNU_CallStaticMethodByName(env, NULL, handlerClassName, "getInstance",           \
-        getInstanceSignature).l;                                                                  \
-    if (handlerHasFlag == JNI_TRUE) {                                                             \
-        JNU_CallMethodByName(env, NULL, handlerRef, "setErrorOccurredFlag", "(Z)V", JNI_FALSE);   \
-    }                                                                                             \
-    JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XErrorHandlerUtil", "WITH_XERROR_HANDLER", \
-        "(Lsun/awt/X11/XErrorHandler;)V", handlerRef);                                            \
+#define WITH_XERROR_HANDLER(f) do {             \
+    XSync(awt_display, False);                  \
+    xerror_code = Success;                      \
+    current_native_xerror_handler = (f);        \
+} while (0)
+
+/* Convenience macro for handlers to use */
+#define XERROR_SAVE(err) do {                   \
+    xerror_code = (err)->error_code;            \
+} while (0)
+
+#define RESTORE_XERROR_HANDLER do {             \
+    XSync(awt_display, False);                  \
+    current_native_xerror_handler = NULL;       \
+} while (0)
+
+#define EXEC_WITH_XERROR_HANDLER(f, code) do {  \
+    WITH_XERROR_HANDLER(f);                     \
+    do {                                        \
+        code;                                   \
+    } while (0);                                \
+    RESTORE_XERROR_HANDLER;                     \
 } while (0)
 
 /*
- * Expected types of arguments of the macro.
- * (JNIEnv*, jboolean)
+ * Called by "ToolkitErrorHandler" function in "XlibWrapper.c" file.
  */
-#define RESTORE_XERROR_HANDLER(env, doXSync) do {                                                 \
-    JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XErrorHandlerUtil",                        \
-        "RESTORE_XERROR_HANDLER", "(Z)V", doXSync);                                               \
-} while (0)
+extern XErrorHandler current_native_xerror_handler;
 
 /*
- * Expected types of arguments of the macro.
- * (JNIEnv*, const char*, const char*, jboolean, jobject, jboolean, No type - C expression)
+ * A place for error handler to report the error code.
  */
-#define EXEC_WITH_XERROR_HANDLER(env, handlerClassName, getInstanceSignature, handlerHasFlag,     \
-                                 handlerRef, errorOccurredFlag, code) do {                        \
-    handlerRef = NULL;                                                                            \
-    WITH_XERROR_HANDLER(env, handlerClassName, getInstanceSignature, handlerHasFlag, handlerRef); \
-    do {                                                                                          \
-        code;                                                                                     \
-    } while (0);                                                                                  \
-    RESTORE_XERROR_HANDLER(env, JNI_TRUE);                                                        \
-    if (handlerHasFlag == JNI_TRUE) {                                                             \
-        errorOccurredFlag = GET_HANDLER_ERROR_OCCURRED_FLAG(env, handlerRef);                     \
-    }                                                                                             \
-} while (0)
+extern unsigned char xerror_code;
 
-/*
- * Expected types of arguments of the macro for jboolean expression.
- * (JNIEnv*, jobject)
- */
-#define GET_HANDLER_ERROR_OCCURRED_FLAG(env, handlerRef) (handlerRef != NULL ?                    \
-    JNU_CallMethodByName(env, NULL, handlerRef, "getErrorOccurredFlag", "()Z").z : JNI_FALSE)
+extern int xerror_ignore_bad_window(Display *dpy, XErrorEvent *err);
 
-/*
- * Expected types of arguments of the macro for jbyte expression.
- * (JNIEnv*, jobject)
- */
-#define GET_XERROR_CODE(env, savedError)                                                          \
-    ((savedError = JNU_GetStaticFieldByName(env, NULL, "sun/awt/X11/XErrorHandlerUtil",           \
-        "saved_error", "Lsun/awt/X11/XErrorEvent;").l) != NULL ?                                  \
-        JNU_CallMethodByName(env, NULL, savedError, "get_error_code", "()B").b : Success)
 #endif /* !HEADLESS */
 
 #ifndef INTERSECTS
--- a/src/solaris/native/sun/awt/awt_wm.c	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/awt/awt_wm.c	Thu Dec 19 15:21:08 2013 +0400
@@ -372,6 +372,21 @@
 \*****************************************************************************/
 
 /*
+ * Instead of validating window id, we simply call XGetWindowProperty,
+ * but temporary install this function as the error handler to ignore
+ * BadWindow error.
+ */
+int /* but ingored */
+xerror_ignore_bad_window(Display *dpy, XErrorEvent *err)
+{
+    XERROR_SAVE(err);
+    if (err->error_code == BadWindow) {
+        DTRACE_PRINTLN("IGNORING BadWindow");
+    }
+    return 0; /* ok to fail */
+}
+
+/*
  * Convenience wrapper for XGetWindowProperty for XA_ATOM properties.
  * E.g. WM_PROTOCOLS, _NET_WM_STATE, _OL_DECOR_DEL.
  * It's up to caller to XFree returned value.
@@ -439,19 +454,16 @@
     unsigned long nitems;
     unsigned long bytes_after;
     unsigned char *string;
-    JNIEnv* env;
-    jboolean errorOccurredFlag;
-    jobject errorHandlerRef;
 
     /* BadWindow is ok and will be blocked by our special handler */
-    env = (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2);
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$IgnoreBadWindowHandler",
-        "()Lsun/awt/X11/XErrorHandler$IgnoreBadWindowHandler;", JNI_FALSE,
-        errorHandlerRef, errorOccurredFlag,
+    WITH_XERROR_HANDLER(xerror_ignore_bad_window);
+    {
         status = XGetWindowProperty(awt_display, w,
                      property, 0, 0xFFFF, False, property_type,
                      &actual_type, &actual_format, &nitems, &bytes_after,
-                     &string));
+                     &string);
+    }
+    RESTORE_XERROR_HANDLER;
 
     if (status != Success || string == NULL) {
         return NULL;
@@ -486,19 +498,16 @@
     unsigned long nitems;
     unsigned long bytes_after;
     long *data;                 /* NB: 64 bit: Format 32 props are 'long' */
-    JNIEnv* env;
-    jboolean errorOccurredFlag;
-    jobject errorHandlerRef;
 
     /* BadWindow is ok and will be blocked by our special handler */
-    env = (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2);
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$IgnoreBadWindowHandler",
-        "()Lsun/awt/X11/XErrorHandler$IgnoreBadWindowHandler;", JNI_FALSE,
-        errorHandlerRef, errorOccurredFlag,
+    WITH_XERROR_HANDLER(xerror_ignore_bad_window);
+    {
         status = XGetWindowProperty(awt_display, w,
                      property, 0, 1, False, property_type,
                      &actual_type, &actual_format, &nitems, &bytes_after,
-                     (unsigned char **)&data));
+                     (unsigned char **)&data);
+    }
+    RESTORE_XERROR_HANDLER;
 
     if (status != Success || data == NULL) {
         return 0;
@@ -699,23 +708,20 @@
     unsigned long nitems;
     unsigned long bytes_after;
     unsigned char *data;
-    JNIEnv* env;
-    jboolean errorOccurredFlag;
-    jobject errorHandlerRef;
 
     if (!awt_wm_atomInterned(&XA_ENLIGHTENMENT_COMMS, "ENLIGHTENMENT_COMMS")) {
         return False;
     }
 
     /* BadWindow is ok and will be blocked by our special handler */
-    env = (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2);
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$IgnoreBadWindowHandler",
-        "()Lsun/awt/X11/XErrorHandler$IgnoreBadWindowHandler;", JNI_FALSE,
-        errorHandlerRef, errorOccurredFlag,
+    WITH_XERROR_HANDLER(xerror_ignore_bad_window);
+    {
         status = XGetWindowProperty(awt_display, w,
                      XA_ENLIGHTENMENT_COMMS, 0, 14, False, XA_STRING,
                      &actual_type, &actual_format, &nitems, &bytes_after,
-                     &data));
+                     &data);
+    }
+    RESTORE_XERROR_HANDLER;
 
     if (status != Success || data == NULL) {
         DTRACE_PRINTLN("no ENLIGHTENMENT_COMMS");
@@ -795,9 +801,6 @@
     unsigned long nitems;
     unsigned long bytes_after;
     long *data;                 /* NB: 64 bit: Format 32 props are 'long' */
-    JNIEnv* env;
-    jboolean errorOccurredFlag;
-    jobject errorHandlerRef;
 
     DTRACE_PRINT("WM: checking for CDE ...  ");
 
@@ -833,14 +836,14 @@
     }
 
     /* BadWindow is ok and will be blocked by our special handler */
-    env = (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2);
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$IgnoreBadWindowHandler",
-        "()Lsun/awt/X11/XErrorHandler$IgnoreBadWindowHandler;", JNI_FALSE,
-        errorHandlerRef, errorOccurredFlag,
+    WITH_XERROR_HANDLER(xerror_ignore_bad_window);
+    {
         status = XGetWindowProperty(awt_display, wmwin,
                      _XA_DT_SM_STATE_INFO, 0, 1, False, _XA_DT_SM_STATE_INFO,
                      &actual_type, &actual_format, &nitems, &bytes_after,
-                     (unsigned char **)&data));
+                     (unsigned char **)&data);
+    }
+    RESTORE_XERROR_HANDLER;
 
     if (status != Success || data == NULL) {
         DTRACE_PRINTLN("no _DT_SM_STATE_INFO");
@@ -1005,6 +1008,18 @@
 }
 
 /*
+ * Temporary error handler that ensures that we know if
+ * XChangeProperty succeeded or not.
+ */
+static int /* but ignored */
+xerror_verify_change_property(Display *dpy, XErrorEvent *err)
+{
+    XERROR_SAVE(err);
+    if (err->request_code == X_ChangeProperty) { }
+    return 0;
+}
+
+/*
  * Prepare IceWM check.
  *
  * The only way to detect IceWM, seems to be by setting
@@ -1031,11 +1046,6 @@
         'a','l','l','W','o','r','k','s','p','a','c','e','s','\0',
         '0','\0'
     };
-    JNIEnv* env;
-    jboolean errorOccurredFlag;
-    jobject errorHandlerRef;
-    jobject savedError;
-    unsigned char xerror_code;
 
     DTRACE_PRINT("WM: scheduling check for IceWM ...  ");
 
@@ -1043,14 +1053,13 @@
         return False;
     }
 
-    env = (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2);
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$VerifyChangePropertyHandler",
-        "()Lsun/awt/X11/XErrorHandler$VerifyChangePropertyHandler;", JNI_FALSE,
-        errorHandlerRef, errorOccurredFlag,
+    WITH_XERROR_HANDLER(xerror_verify_change_property);
+    {
         XChangeProperty(awt_display, DefaultRootWindow(awt_display),
                         _XA_ICEWM_WINOPTHINT, _XA_ICEWM_WINOPTHINT, 8,
-                        PropModeReplace, opt, sizeof(opt)));
-    xerror_code = GET_XERROR_CODE(env, savedError);
+                        PropModeReplace, opt, sizeof(opt));
+    }
+    RESTORE_XERROR_HANDLER;
 
     if (xerror_code != Success) {
         DTRACE_PRINTLN1("can't set _ICEWM_WINOPTHINT, error = %d",
@@ -1140,6 +1149,25 @@
     return True;
 }
 
+static Boolean winmgr_running = False;
+
+/*
+ * Temporary error handler that checks if selecting for
+ * SubstructureRedirect failed.
+ */
+static int /* but ignored */
+xerror_detect_wm(Display *dpy, XErrorEvent *err)
+{
+    XERROR_SAVE(err);
+    if (err->request_code == X_ChangeWindowAttributes
+        && err->error_code == BadAccess)
+    {
+        DTRACE_PRINTLN("some WM is running (hmm, we'll see)");
+        winmgr_running = True;
+    }
+    return 0;
+}
+
 /*
  * Make an educated guess about running window manager.
  * XXX: ideally, we should detect wm restart.
@@ -1156,9 +1184,6 @@
     XSetWindowAttributes substruct;
     const char *vendor_string;
     Boolean doIsIceWM;
-    JNIEnv* env;
-    jboolean errorOccurredFlag = JNI_FALSE;
-    jobject errorHandlerRef;
 
     if (awt_wmgr != UNDETERMINED_WM) {
         return awt_wmgr;
@@ -1186,21 +1211,22 @@
      * can select for, and if the request fails, than some other WM is
      * already running.
      */
+    winmgr_running = 0;
     substruct.event_mask = SubstructureRedirectMask;
 
     DTRACE_PRINT("WM: trying SubstructureRedirect ...  ");
-    env = (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2);
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$XChangeWindowAttributesHandler",
-        "()Lsun/awt/X11/XErrorHandler$XChangeWindowAttributesHandler;", JNI_TRUE,
-        errorHandlerRef, errorOccurredFlag,
+    WITH_XERROR_HANDLER(xerror_detect_wm);
+    {
         XChangeWindowAttributes(awt_display, DefaultRootWindow(awt_display),
-                                CWEventMask, &substruct));
+                                CWEventMask, &substruct);
+    }
+    RESTORE_XERROR_HANDLER;
 
     /*
      * If no WM is running than our selection for SubstructureRedirect
      * succeeded and needs to be undone (hey we are *not* a WM ;-).
      */
-    if (errorOccurredFlag == JNI_FALSE) {
+    if (!winmgr_running) {
         DTRACE_PRINTLN("no WM is running");
         awt_wmgr = NO_WM;
         substruct.event_mask = 0;
@@ -2687,11 +2713,6 @@
     Boolean shaded;
     unsigned long nitems;
     unsigned long i;
-    JNIEnv* env;
-    jboolean errorOccurredFlag;
-    jobject errorHandlerRef;
-    jobject savedError;
-    unsigned char xerror_code;
 
     net_wm_state = awt_getAtomListProperty(shell_win,
                                            _XA_NET_WM_STATE, &nitems);
@@ -2733,14 +2754,13 @@
     awt_wm_dtraceStateNet(net_wm_state, nitems);
 #endif
 
-    env = (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2);
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$VerifyChangePropertyHandler",
-        "()Lsun/awt/X11/XErrorHandler$VerifyChangePropertyHandler;", JNI_FALSE,
-        errorHandlerRef, errorOccurredFlag,
+    WITH_XERROR_HANDLER(xerror_verify_change_property);
+    {
         XChangeProperty(dpy, shell_win,
                         _XA_NET_WM_STATE, XA_ATOM, 32, PropModeReplace,
-                        (unsigned char *)net_wm_state, nitems));
-    xerror_code = GET_XERROR_CODE(env, savedError);
+                        (unsigned char *)net_wm_state, nitems);
+    }
+    RESTORE_XERROR_HANDLER;
 
     if (xerror_code != Success) {
         DTRACE_PRINTLN1("WM:     XChangeProperty failed, error = %d",
--- a/src/solaris/native/sun/awt/awt_xembed_server.c	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/awt/awt_xembed_server.c	Thu Dec 19 15:21:08 2013 +0400
@@ -644,19 +644,12 @@
     return dim;
 }
 
-Boolean isMapped(JNIEnv* env, Window w) {
+Boolean isMapped(Window w) {
     XWindowAttributes attr;
     Status status = 0;
-    jboolean errorOccurredFlag;
-    jobject errorHandlerRef;
-    jobject savedError;
-    unsigned char xerror_code;
-
-    EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$IgnoreBadWindowHandler",
-        "()Lsun/awt/X11/XErrorHandler$IgnoreBadWindowHandler;", JNI_FALSE,
-        errorHandlerRef, errorOccurredFlag,
-        status = XGetWindowAttributes(awt_display, w, &attr));
-    xerror_code = GET_XERROR_CODE(env, savedError);
+    WITH_XERROR_HANDLER(xerror_ignore_bad_window);
+    status = XGetWindowAttributes(awt_display, w, &attr);
+    RESTORE_XERROR_HANDLER;
     if (status == 0 || xerror_code != Success) {
         return False;
     }
@@ -692,7 +685,7 @@
             sdata->version = *data;
             flags = *(data+1);
             new_mapped = (flags & XEMBED_MAPPED) != 0;
-            currently_mapped = isMapped(env, sdata->handle);
+            currently_mapped = isMapped(sdata->handle);
             if (new_mapped != currently_mapped) {
                 if (new_mapped) {
                     XMapWindow(awt_display, sdata->handle);
--- a/src/solaris/native/sun/java2d/opengl/GLXSurfaceData.c	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/java2d/opengl/GLXSurfaceData.c	Thu Dec 19 15:21:08 2013 +0400
@@ -50,6 +50,8 @@
 extern void
     OGLSD_SetNativeDimensions(JNIEnv *env, OGLSDOps *oglsdo, jint w, jint h);
 
+jboolean surfaceCreationFailed = JNI_FALSE;
+
 #endif /* !HEADLESS */
 
 JNIEXPORT void JNICALL
@@ -391,6 +393,15 @@
     return JNI_TRUE;
 }
 
+static int
+GLXSD_BadAllocXErrHandler(Display *display, XErrorEvent *xerr)
+{
+    if (xerr->error_code == BadAlloc) {
+        surfaceCreationFailed = JNI_TRUE;
+    }
+    return 0;
+}
+
 JNIEXPORT jboolean JNICALL
 Java_sun_java2d_opengl_GLXSurfaceData_initPbuffer
     (JNIEnv *env, jobject glxsd,
@@ -406,8 +417,6 @@
     int attrlist[] = {GLX_PBUFFER_WIDTH, 0,
                       GLX_PBUFFER_HEIGHT, 0,
                       GLX_PRESERVED_CONTENTS, GL_FALSE, 0};
-    jboolean errorOccurredFlag;
-    jobject errorHandlerRef;
 
     J2dTraceLn3(J2D_TRACE_INFO,
                 "GLXSurfaceData_initPbuffer: w=%d h=%d opq=%d",
@@ -435,15 +444,12 @@
     attrlist[1] = width;
     attrlist[3] = height;
 
-    WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$GLXBadAllocHandler",
-        "()Lsun/awt/X11/XErrorHandler$GLXBadAllocHandler;", JNI_TRUE, errorHandlerRef);
-    pbuffer = j2d_glXCreatePbuffer(awt_display, glxinfo->fbconfig, attrlist);
-    // Call XSync without the acquired AWT lock to avoid a deadlock (see 8015730).
-    XSync(awt_display, False);
-    RESTORE_XERROR_HANDLER(env, JNI_FALSE);
-    errorOccurredFlag = GET_HANDLER_ERROR_OCCURRED_FLAG(env, errorHandlerRef);
-
-    if ((pbuffer == 0) || errorOccurredFlag) {
+    surfaceCreationFailed = JNI_FALSE;
+    EXEC_WITH_XERROR_HANDLER(
+        GLXSD_BadAllocXErrHandler,
+        pbuffer = j2d_glXCreatePbuffer(awt_display,
+                                       glxinfo->fbconfig, attrlist));
+    if ((pbuffer == 0) || surfaceCreationFailed) {
         J2dRlsTraceLn(J2D_TRACE_ERROR,
             "GLXSurfaceData_initPbuffer: could not create glx pbuffer");
         return JNI_FALSE;
--- a/src/solaris/native/sun/java2d/x11/X11SurfaceData.c	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/java2d/x11/X11SurfaceData.c	Thu Dec 19 15:21:08 2013 +0400
@@ -68,6 +68,7 @@
 #ifndef XAWT
 extern struct MComponentPeerIDs mComponentPeerIDs;
 #endif
+extern int XShmAttachXErrHandler(Display *display, XErrorEvent *xerr);
 extern AwtGraphicsConfigDataPtr
     getGraphicsConfigFromComponentPeer(JNIEnv *env, jobject this);
 extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;
@@ -550,8 +551,6 @@
 {
     XImage *img = NULL;
     XShmSegmentInfo *shminfo;
-    JNIEnv* env;
-    jboolean xShmAttachResult;
 
     shminfo = malloc(sizeof(XShmSegmentInfo));
     if (shminfo == NULL) {
@@ -591,8 +590,9 @@
 
     shminfo->readOnly = False;
 
-    env = (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2);
-    xShmAttachResult = TryXShmAttach(env, awt_display, shminfo);
+    resetXShmAttachFailed();
+    EXEC_WITH_XERROR_HANDLER(XShmAttachXErrHandler,
+                             XShmAttach(awt_display, shminfo));
 
     /*
      * Once the XSync round trip has finished then we
@@ -601,7 +601,7 @@
      */
     shmctl(shminfo->shmid, IPC_RMID, 0);
 
-    if (xShmAttachResult == JNI_FALSE) {
+    if (isXShmAttachFailed() == JNI_TRUE) {
         J2dRlsTraceLn1(J2D_TRACE_ERROR,
                        "X11SD_SetupSharedSegment XShmAttach has failed: %s",
                        strerror(errno));
--- a/src/solaris/native/sun/xawt/XlibWrapper.c	Wed Dec 18 16:45:18 2013 -0800
+++ b/src/solaris/native/sun/xawt/XlibWrapper.c	Thu Dec 19 15:21:08 2013 +0400
@@ -41,6 +41,7 @@
 #include <sizecalc.h>
 
 #include <awt.h>
+#include <awt_util.h>
 #include <jvm.h>
 
 #include <Region.h>
@@ -1262,6 +1263,10 @@
 JavaVM* jvm = NULL;
 static int ToolkitErrorHandler(Display * dpy, XErrorEvent * event) {
     JNIEnv * env;
+    // First call the native synthetic error handler declared in "awt_util.h" file.
+    if (current_native_xerror_handler != NULL) {
+        current_native_xerror_handler(dpy, event);
+    }
     if (jvm != NULL) {
         env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
         if (env) {