8025775: JNI warnings in TryXShmAttach
authoralitvinov
Wed, 04 Dec 2013 12:29:18 +0400
changeset 21939 5169fd314636
parent 21938 3a235a461503
child 21940 1d3c0bbc4b7e
8025775: JNI warnings in TryXShmAttach Reviewed-by: art, anthony
jdk/src/solaris/classes/sun/awt/X11/XConstants.java
jdk/src/solaris/classes/sun/awt/X11/XErrorHandler.java
jdk/src/solaris/classes/sun/awt/X11/XErrorHandlerUtil.java
jdk/src/solaris/native/sun/awt/awt_GraphicsEnv.c
jdk/src/solaris/native/sun/awt/awt_GraphicsEnv.h
jdk/src/solaris/native/sun/awt/awt_util.c
jdk/src/solaris/native/sun/awt/awt_util.h
jdk/src/solaris/native/sun/java2d/opengl/GLXSurfaceData.c
jdk/src/solaris/native/sun/java2d/x11/X11SurfaceData.c
jdk/src/solaris/native/sun/xawt/XlibWrapper.c
--- a/jdk/src/solaris/classes/sun/awt/X11/XConstants.java	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/classes/sun/awt/X11/XConstants.java	Wed Dec 04 12:29:18 2013 +0400
@@ -674,9 +674,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/jdk/src/solaris/classes/sun/awt/X11/XErrorHandler.java	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/classes/sun/awt/X11/XErrorHandler.java	Wed Dec 04 12:29:18 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</code> value of the parameter <code>errorOccurred</code> before this
-         * error handler is set as current.
-         * @param errorOccurred <code>true</code> to indicate that an error was handled,
-         *     <code>false</code> 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,51 +76,4 @@
             return theInstance;
         }
     }
-
-    /**
-     * This is a synthetic error handler for errors generated by the native function
-     * <code>XShmAttach</code>. If an error is handled, an internal boolean flag of the
-     * handler is set to <code>true</code>.
-     */
-    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</code> errors generated by the
-     * native <code>glX*</code> functions. Its internal boolean flag is set to <code>true</code>,
-     * 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;
-        }
-    }
 }
--- a/jdk/src/solaris/classes/sun/awt/X11/XErrorHandlerUtil.java	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/classes/sun/awt/X11/XErrorHandlerUtil.java	Wed Dec 04 12:29:18 2013 +0400
@@ -42,7 +42,7 @@
     private static long display;
 
     /**
-     * Error handler at the moment of <code>XErrorHandlerUtil</code> initialization.
+     * Error handler at the moment of {@code XErrorHandlerUtil} initialization.
      */
     private static long saved_error_handler;
 
@@ -63,7 +63,7 @@
         new GetBooleanAction("sun.awt.noisyerrorhandler"));
 
     /**
-     * The flag indicating that <code>init</code> was called already.
+     * The flag indicating that {@code init} was called already.
      */
     private static boolean initPassed;
 
@@ -73,9 +73,9 @@
     private XErrorHandlerUtil() {}
 
     /**
-     * Sets the toolkit global error handler, stores the connection to X11 server, which
-     * will be used during an error handling process. This method is called once from
-     * <code>awt_init_Display</code> function defined in <code>awt_GraphicsEnv.c</code>
+     * Sets the toolkit global error handler, stores the connection to X11 server,
+     * which will be used during an error handling process. This method is called
+     * once from {@code awt_init_Display} function defined in {@code awt_GraphicsEnv.c}
      * file immediately after the connection to X11 window server is opened.
      * @param display the connection to X11 server which should be stored
      */
@@ -109,9 +109,9 @@
     }
 
     private static void RESTORE_XERROR_HANDLER(boolean doXSync) {
-        // Wait until all requests are processed by the X server
-        // and only then uninstall the error handler.
         if (doXSync) {
+            // Wait until all requests are processed by the X server
+            // and only then uninstall the error handler.
             XSync();
         }
         current_error_handler = null;
--- a/jdk/src/solaris/native/sun/awt/awt_GraphicsEnv.c	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/native/sun/awt/awt_GraphicsEnv.c	Wed Dec 04 12:29:18 2013 +0400
@@ -906,6 +906,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;
 
@@ -913,7 +927,6 @@
     XShmSegmentInfo shminfo;
     int XShmMajor, XShmMinor;
     int a, b, c;
-    jboolean xShmAttachResult;
 
     AWT_LOCK();
     if (canUseShmExt != UNSET_MITSHM) {
@@ -957,14 +970,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,
@@ -979,23 +999,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/jdk/src/solaris/native/sun/awt/awt_GraphicsEnv.h	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/native/sun/awt/awt_GraphicsEnv.h	Wed Dec 04 12:29:18 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/jdk/src/solaris/native/sun/awt/awt_util.c	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/native/sun/awt/awt_util.c	Wed Dec 04 12:29:18 2013 +0400
@@ -41,6 +41,11 @@
 
 #include "java_awt_event_MouseWheelEvent.h"
 
+/*
+ * Called by "ToolkitErrorHandler" function in "XlibWrapper.c" file.
+ */
+XErrorHandler current_native_xerror_handler = NULL;
+
 extern jint getModifiers(uint32_t state, jint button, jint keyCode);
 extern jint getButton(uint32_t button);
 
--- a/jdk/src/solaris/native/sun/awt/awt_util.h	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/native/sun/awt/awt_util.h	Wed Dec 04 12:29:18 2013 +0400
@@ -29,57 +29,29 @@
 #ifndef HEADLESS
 #include "gdefs.h"
 
-/*
- * 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);                  \
+    current_native_xerror_handler = (f);        \
 } while (0)
 
-/*
- * Expected types of arguments of the macro.
- * (JNIEnv*, jboolean)
- */
-#define RESTORE_XERROR_HANDLER(env, doXSync) do {                                                 \
-    JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XErrorHandlerUtil",                        \
-        "RESTORE_XERROR_HANDLER", "(Z)V", doXSync);                                               \
+#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*, const char*, const char*, jboolean, jobject, jboolean, No type - C expression)
+ * Called by "ToolkitErrorHandler" function in "XlibWrapper.c" file.
  */
-#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) {                                                             \
-        GET_HANDLER_ERROR_OCCURRED_FLAG(env, handlerRef, errorOccurredFlag);                      \
-    }                                                                                             \
-} while (0)
+extern XErrorHandler current_native_xerror_handler;
 
-/*
- * Expected types of arguments of the macro.
- * (JNIEnv*, jobject, jboolean)
- */
-#define GET_HANDLER_ERROR_OCCURRED_FLAG(env, handlerRef, errorOccurredFlag) do {                  \
-    if (handlerRef != NULL) {                                                                     \
-        errorOccurredFlag = JNU_CallMethodByName(env, NULL, handlerRef, "getErrorOccurredFlag",   \
-            "()Z").z;                                                                             \
-    }                                                                                             \
-} while (0)
 #endif /* !HEADLESS */
 
 #ifndef INTERSECTS
--- a/jdk/src/solaris/native/sun/java2d/opengl/GLXSurfaceData.c	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/native/sun/java2d/opengl/GLXSurfaceData.c	Wed Dec 04 12:29:18 2013 +0400
@@ -48,6 +48,8 @@
 extern void
     OGLSD_SetNativeDimensions(JNIEnv *env, OGLSDOps *oglsdo, jint w, jint h);
 
+jboolean surfaceCreationFailed = JNI_FALSE;
+
 #endif /* !HEADLESS */
 
 JNIEXPORT void JNICALL
@@ -347,6 +349,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,
@@ -362,8 +373,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",
@@ -391,15 +400,12 @@
     attrlist[1] = width;
     attrlist[3] = height;
 
-    errorOccurredFlag = JNI_FALSE;
-    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);
-    XSync(awt_display, False);
-    RESTORE_XERROR_HANDLER(env, JNI_FALSE);
-    GET_HANDLER_ERROR_OCCURRED_FLAG(env, errorHandlerRef, errorOccurredFlag);
-
-    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/jdk/src/solaris/native/sun/java2d/x11/X11SurfaceData.c	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/native/sun/java2d/x11/X11SurfaceData.c	Wed Dec 04 12:29:18 2013 +0400
@@ -65,6 +65,7 @@
 static DisposeFunc X11SD_Dispose;
 static GetPixmapBgFunc X11SD_GetPixmapWithBg;
 static ReleasePixmapBgFunc X11SD_ReleasePixmapWithBg;
+extern int XShmAttachXErrHandler(Display *display, XErrorEvent *xerr);
 extern AwtGraphicsConfigDataPtr
     getGraphicsConfigFromComponentPeer(JNIEnv *env, jobject this);
 extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;
@@ -532,8 +533,6 @@
 {
     XImage *img = NULL;
     XShmSegmentInfo *shminfo;
-    JNIEnv* env;
-    jboolean xShmAttachResult;
 
     shminfo = malloc(sizeof(XShmSegmentInfo));
     if (shminfo == NULL) {
@@ -573,8 +572,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
@@ -583,7 +583,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/jdk/src/solaris/native/sun/xawt/XlibWrapper.c	Tue Dec 03 15:10:37 2013 -0800
+++ b/jdk/src/solaris/native/sun/xawt/XlibWrapper.c	Wed Dec 04 12:29:18 2013 +0400
@@ -41,6 +41,7 @@
 #include <sizecalc.h>
 
 #include <awt.h>
+#include <awt_util.h>
 #include <jvm.h>
 
 #include <Region.h>
@@ -1266,6 +1267,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) {