< prev index next >

src/java.desktop/unix/native/libawt_xawt/awt/awt_util.c

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -63,39 +63,35 @@
 
 jboolean
 awtJNI_ThreadYield(JNIEnv *env) {
 
     static jclass threadClass = NULL;
-    static jmethodID yieldMethodID = NULL;
+    static volatile jmethodID yieldMethodID = NULL;
 
-    /* Initialize our java identifiers once. Checking before locking
-     * is a huge performance win.
+    /* Double Check Locking is used with volatile yieldMethodID identifier
+     * to initialize java identifiers only once & multi-thread safe.
      */
-    if (threadClass == NULL) {
-        // should enter a monitor here...
-        Boolean err = FALSE;
-        if (threadClass == NULL) {
+
+    if (yieldMethodID == NULL) {
+        AWT_LOCK();
+        if (yieldMethodID == NULL) {
             jclass tc = (*env)->FindClass(env, "java/lang/Thread");
             CHECK_NULL_RETURN(tc, JNI_FALSE);
             threadClass = (*env)->NewGlobalRef(env, tc);
+
             (*env)->DeleteLocalRef(env, tc);
             if (threadClass != NULL) {
-                yieldMethodID = (*env)->GetStaticMethodID(env,
-                                              threadClass,
-                                              "yield",
-                                              "()V"
-                                                );
+                yieldMethodID = (*env)->GetStaticMethodID(env, threadClass,
+                                              "yield", "()V");
             }
         }
+        AWT_UNLOCK();
         if (yieldMethodID == NULL) {
             threadClass = NULL;
-            err = TRUE;
-        }
-        if (err) {
             return JNI_FALSE;
         }
-    } /* threadClass == NULL*/
+    }
 
     (*env)->CallStaticVoidMethod(env, threadClass, yieldMethodID);
     DASSERT(!((*env)->ExceptionOccurred(env)));
     if ((*env)->ExceptionCheck(env)) {
         return JNI_FALSE;
< prev index next >