Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/misc/GThreadHelper.java
32287 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.misc;2627import java.util.concurrent.locks.ReentrantLock;2829/**30* This class is used to prevent multiple calling of g_thread_init ()31* and gdk_thread_init ().32*33* Since version 2.24 of GLib, calling g_thread_init () multiple times is34* allowed, but it will crash for older versions. There are two ways to35* find out if g_thread_init () has been called:36* g_thread_get_initialized (), but it was introduced in 2.2037* g_thread_supported (), but it is a macro and cannot be loaded with dlsym.38*39* usage:40* <pre>41* lock();42* try {43* if (!getAndSetInitializationNeededFlag()) {44* //call to g_thread_init();45* //call to gdk_thread_init();46* }47* } finally {48* unlock();49* }50* </pre>51*/52public final class GThreadHelper {5354private static final ReentrantLock LOCK = new ReentrantLock();55private static boolean isGThreadInitialized = false;5657/**58* Acquires the lock.59*/60public static void lock() {61LOCK.lock();62}6364/**65* Releases the lock.66*/67public static void unlock() {68LOCK.unlock();69}7071/**72* Gets current value of initialization flag and sets it to {@code true}.73* MUST be called under the lock.74*75* A return value of {@code false} indicates that the calling code76* should call the g_thread_init() and gdk_thread_init() functions77* before releasing the lock.78*79* @return {@code true} if initialization has been completed.80*/81public static boolean getAndSetInitializationNeededFlag() {82boolean ret = isGThreadInitialized;83isGThreadInitialized = true;84return ret;85}86}878889