--- old/src/solaris/native/sun/awt/awt_UNIXToolkit.c 2013-09-27 17:37:04.490040450 +0400 +++ new/src/solaris/native/sun/awt/awt_UNIXToolkit.c 2013-09-27 17:37:04.210040442 +0400 @@ -67,7 +67,7 @@ Java_sun_awt_UNIXToolkit_load_1gtk(JNIEnv *env, jclass klass) { #ifndef HEADLESS - return (jboolean)gtk2_load(); + return (jboolean)gtk2_load(env); #else return JNI_FALSE; #endif /* !HEADLESS */ --- old/src/solaris/native/sun/awt/gtk2_interface.c 2013-09-27 17:37:05.158040467 +0400 +++ new/src/solaris/native/sun/awt/gtk2_interface.c 2013-09-27 17:37:04.886040459 +0400 @@ -81,7 +81,7 @@ static void *gtk2_libhandle = NULL; static void *gthread_libhandle = NULL; -static gboolean flag_g_thread_get_initialized = FALSE; + static jmp_buf j; /* Widgets */ @@ -502,7 +502,7 @@ fp_gtk_g_slist_length = dl_symbol("g_slist_length"); } -gboolean gtk2_load() +gboolean gtk2_load(JNIEnv *env) { gboolean result; int i; @@ -533,6 +533,7 @@ } /* GLib */ + fp_glib_check_version = dl_symbol("glib_check_version"); fp_g_free = dl_symbol("g_free"); fp_g_object_unref = dl_symbol("g_object_unref"); @@ -708,6 +709,9 @@ /** * GLib thread system */ + if (fp_glib_check_version(2, 20, 0) == NULL) { + fp_g_thread_get_initialized = dl_symbol_gthread("g_thread_get_initialized"); + } fp_g_thread_init = dl_symbol_gthread("g_thread_init"); fp_gdk_threads_init = dl_symbol("gdk_threads_init"); fp_gdk_threads_enter = dl_symbol("gdk_threads_enter"); @@ -810,16 +814,31 @@ io_handler = XSetIOErrorHandler(NULL); if (fp_gtk_check_version(2, 2, 0) == NULL) { + jclass clazz = (*env)->FindClass(env, "sun/misc/GThreadHelper"); + jmethodID mid_getInitStateAndInitialize = (*env)->GetStaticMethodID(env, clazz, "getInitStateAndInitialize", "()Z"); + jmethodID mid_lock = (*env)->GetStaticMethodID(env, clazz, "lock", "()V"); + jmethodID mid_unlock = (*env)->GetStaticMethodID(env, clazz, "unlock", "()V"); + // Init the thread system to use GLib in a thread-safe mode - if (!flag_g_thread_get_initialized) { - flag_g_thread_get_initialized = TRUE; + (*env)->CallStaticVoidMethod(env, clazz, mid_lock); + + // Calling g_thread_init() multiple times leads to crash on GLib < 2.24 + // We can use g_thread_get_initialized () but it is available only for + // GLib >= 2.20. We rely on GThreadHelper for GLib < 2.20. + gboolean is_g_thread_get_initialized = FALSE; + if (fp_glib_check_version(2, 20, 0) == NULL) { + is_g_thread_get_initialized = fp_g_thread_get_initialized(); + } + if (!(*env)->CallStaticBooleanMethod(env, clazz, mid_getInitStateAndInitialize) + && !is_g_thread_get_initialized) { fp_g_thread_init(NULL); //According the GTK documentation, gdk_threads_init() should be //called before gtk_init() or gtk_init_check() fp_gdk_threads_init(); } + (*env)->CallStaticVoidMethod(env, clazz, mid_unlock); } result = (*fp_gtk_init_check)(NULL, NULL); --- old/src/solaris/native/sun/awt/gtk2_interface.h 2013-09-27 17:37:05.734040481 +0400 +++ new/src/solaris/native/sun/awt/gtk2_interface.h 2013-09-27 17:37:05.526040476 +0400 @@ -643,6 +643,14 @@ */ const char *getStrFor(JNIEnv *env, jstring value); +/** + * Returns : + * NULL if the GLib library is compatible with the given version, or a string + * describing the version mismatch. + */ +gchar* (*fp_glib_check_version)(guint required_major, guint required_minor, + guint required_micro); + /* * Check whether the gtk2 library is available and meets the minimum * version requirement. If the library is already loaded this method has no @@ -663,7 +671,7 @@ * effect and returns success. * Returns FALSE on failure and TRUE on success. */ -gboolean gtk2_load(); +gboolean gtk2_load(JNIEnv *env); /* * Loads fp_gtk_show_uri function pointer. This initialization is @@ -801,6 +809,12 @@ guint (*fp_gtk_main_level)(void); +/** + * This function is available for GLIB > 2.20, so it MUST be + * called within (fp_glib_check_version(2, 20, 0) == NULL) check. + */ +gboolean (*fp_g_thread_get_initialized)(void); + void (*fp_g_thread_init)(GThreadFunctions *vtable); void (*fp_gdk_threads_init)(void); void (*fp_gdk_threads_enter)(void); --- old/src/solaris/native/sun/xawt/awt_Desktop.c 2013-09-27 17:37:06.438040499 +0400 +++ new/src/solaris/native/sun/xawt/awt_Desktop.c 2013-09-27 17:37:06.198040493 +0400 @@ -42,7 +42,7 @@ return JNI_TRUE; } - if (gtk2_load() && gtk2_show_uri_load()) { + if (gtk2_load(env) && gtk2_show_uri_load()) { gtk_has_been_loaded = TRUE; return JNI_TRUE; } else if (gnome_load()) { --- /dev/null 2013-09-27 13:24:53.085653010 +0400 +++ new/src/solaris/classes/sun/misc/GThreadHelper.java 2013-09-27 17:37:06.874040511 +0400 @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2013, 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 + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package sun.misc; + +import java.util.concurrent.locks.ReentrantLock; + +/** + * This class is used to prevent multiple calling of g_thread_init () + * for GLib < 2.20. + * + * Since version 2.24 of GLib, calling g_thread_init () multiple times is + * allowed, but it will crash for older versions. There are two ways to + * find out if g_thread_init () has been called: + * g_thread_get_initialized (), but it was introduced in 2.20 + * g_thread_supported (), but it is a macro and cannot be loaded with dlsym. + * + * usage: + *
+ * lock();
+ * try {
+ *    if (!getInitStateAndInitialize()) {
+ *        //call to g_thread_init();
+ *    }
+ * } finally {
+ *    unlock();
+ * }
+ * 
+ */ +public final class GThreadHelper { + + private static final ReentrantLock LOCK = new ReentrantLock(); + private static boolean isGThreadInitialized = false; + + /** + * Acquires the lock. + */ + public static void lock() { + LOCK.lock(); + } + + /** + * Releases the lock. + */ + public static void unlock() { + LOCK.unlock(); + } + + /** + * Gets current g_thread_init() state and sets it to {@code true}. + * MUST be called under the lock. + * If it returns {@code false} user must call g_thread_init() on his own. + * + * @return {@code true} if g_thread_init() has been called + */ + public static boolean getInitStateAndInitialize() { + boolean ret = isGThreadInitialized; + isGThreadInitialized = true; + return ret; + } +}