4639N/A/*
4639N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4639N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4639N/A *
4639N/A * This code is free software; you can redistribute it and/or modify it
4639N/A * under the terms of the GNU General Public License version 2 only, as
4639N/A * published by the Free Software Foundation. Oracle designates this
4639N/A * particular file as subject to the "Classpath" exception as provided
4639N/A * by Oracle in the LICENSE file that accompanied this code.
4639N/A *
4639N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4639N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4639N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4639N/A * version 2 for more details (a copy is included in the LICENSE file that
4639N/A * accompanied this code).
4639N/A *
4639N/A * You should have received a copy of the GNU General Public License version
4639N/A * 2 along with this work; if not, write to the Free Software Foundation,
4639N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4639N/A *
4639N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4639N/A * or visit www.oracle.com if you need additional information or have any
4639N/A * questions.
4639N/A */
4639N/A
4639N/A#ifndef __THREADUTILITIES_H
4639N/A#define __THREADUTILITIES_H
4639N/A
4639N/A#import <pthread.h>
4639N/A
4639N/A#import "AWT_debug.h"
4639N/A
4639N/A
4639N/A// --------------------------------------------------------------------------
4639N/A#ifndef PRODUCT_BUILD
4639N/A
4639N/A// Turn on the AWT thread assert mechanism. See below for different variants.
4639N/A// TODO: don't enable this for production builds...
4639N/A#define AWT_THREAD_ASSERTS
4639N/A
4639N/A#endif /* PRODUCT_BUILD */
4639N/A// --------------------------------------------------------------------------
4639N/A
4639N/A// --------------------------------------------------------------------------
4639N/A#ifdef AWT_THREAD_ASSERTS
4639N/A
4639N/A// Turn on to have awt thread asserts display a message on the console.
4639N/A#define AWT_THREAD_ASSERTS_MESSAGES
4639N/A
4639N/A// Turn on to have awt thread asserts use an environment variable switch to
4639N/A// determine if assert should really be called.
4639N/A//#define AWT_THREAD_ASSERTS_ENV_ASSERT
4639N/A
4639N/A// Define AWT_THREAD_ASSERTS_WAIT to make asserts halt the asserting thread
4639N/A// for debugging purposes.
4639N/A//#define AWT_THREAD_ASSERTS_WAIT
4639N/A
4639N/A#ifdef AWT_THREAD_ASSERTS_MESSAGES
4639N/A
4639N/A#define AWT_THREAD_ASSERTS_NOT_APPKIT_MESSAGE \
4639N/A AWT_DEBUG_LOG(@"Not running on AppKit thread 0 when expected.")
4639N/A
4639N/A#define AWT_THREAD_ASSERTS_ON_APPKIT_MESSAGE \
4639N/A AWT_DEBUG_LOG(@"Running on AppKit thread 0 when not expected.")
4639N/A
4639N/A#ifdef AWT_THREAD_ASSERTS_ENV_ASSERT
4639N/A
4639N/Aextern int sAWTThreadAsserts;
4639N/A#define AWT_THREAD_ASSERTS_ENV_ASSERT_CHECK \
4639N/Ado { \
4639N/A if (sAWTThreadAsserts) { \
4639N/A NSLog(@"\tPlease run this java program again with setenv COCOA_AWT_DISABLE_THREAD_ASSERTS to proceed with a warning."); \
4639N/A assert(NO); \
4639N/A } \
4639N/A} while (0)
4639N/A
4639N/A#else
4639N/A
4639N/A#define AWT_THREAD_ASSERTS_ENV_ASSERT_CHECK do {} while (0)
4639N/A
4639N/A#endif /* AWT_THREAD_ASSERTS_ENV_ASSERT */
4639N/A
4639N/A#define AWT_ASSERT_APPKIT_THREAD \
4639N/Ado { \
4639N/A if (pthread_main_np() == 0) { \
4639N/A AWT_THREAD_ASSERTS_NOT_APPKIT_MESSAGE; \
4639N/A AWT_DEBUG_BUG_REPORT_MESSAGE; \
4639N/A AWT_THREAD_ASSERTS_ENV_ASSERT_CHECK; \
4639N/A } \
4639N/A} while (0)
4639N/A
4639N/A#define AWT_ASSERT_NOT_APPKIT_THREAD \
4639N/Ado { \
4639N/A if (pthread_main_np() != 0) { \
4639N/A AWT_THREAD_ASSERTS_ON_APPKIT_MESSAGE; \
4639N/A AWT_DEBUG_BUG_REPORT_MESSAGE; \
4639N/A AWT_THREAD_ASSERTS_ENV_ASSERT_CHECK; \
4639N/A } \
4639N/A} while (0)
4639N/A
4639N/A#endif /* AWT_THREAD_ASSERTS_MESSAGES */
4639N/A
4639N/A#ifdef AWT_THREAD_ASSERTS_WAIT
4639N/A
4639N/A#define AWT_ASSERT_APPKIT_THREAD \
4639N/Ado { \
4639N/A while (pthread_main_np() == 0) {} \
4639N/A} while (0)
4639N/A
4639N/A#define AWT_ASSERT_NOT_APPKIT_THREAD \
4639N/Ado { \
4639N/A while (pthread_main_np() != 0) {} \
4639N/A} while (0)
4639N/A
4639N/A#endif /* AWT_THREAD_ASSERTS_WAIT */
4639N/A
4639N/A#else /* AWT_THREAD_ASSERTS */
4639N/A
4639N/A#define AWT_ASSERT_APPKIT_THREAD do {} while (0)
4639N/A#define AWT_ASSERT_NOT_APPKIT_THREAD do {} while (0)
4639N/A
4639N/A#endif /* AWT_THREAD_ASSERTS */
4639N/A// --------------------------------------------------------------------------
4639N/A
4639N/A__attribute__((visibility("default")))
6057N/A@interface ThreadUtilities { }
4639N/A
4639N/A+ (JNIEnv*)getJNIEnv;
4639N/A+ (JNIEnv*)getJNIEnvUncached;
4639N/A
6055N/A//Wrappers for the corresponding JNFRunLoop methods with a check for main thread
5555N/A+ (void)performOnMainThreadWaiting:(BOOL)wait block:(void (^)())block;
6055N/A+ (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)wait;
4639N/A@end
4639N/A
4639N/Avoid OSXAPP_SetJavaVM(JavaVM *vm);
4639N/A
4639N/A#endif /* __THREADUTILITIES_H */