Path: blob/master/src/java.desktop/macosx/native/libosxapp/ThreadUtilities.h
41152 views
/*1* Copyright (c) 2011, 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*/2425#ifndef __THREADUTILITIES_H26#define __THREADUTILITIES_H2728#include "jni.h"2930#import <pthread.h>3132#import "AWT_debug.h"333435// --------------------------------------------------------------------------36#ifndef PRODUCT_BUILD3738// Turn on the AWT thread assert mechanism. See below for different variants.39// TODO: don't enable this for production builds...40#define AWT_THREAD_ASSERTS4142#endif /* PRODUCT_BUILD */43// --------------------------------------------------------------------------4445// --------------------------------------------------------------------------46#ifdef AWT_THREAD_ASSERTS4748// Turn on to have awt thread asserts display a message on the console.49#define AWT_THREAD_ASSERTS_MESSAGES5051// Turn on to have awt thread asserts use an environment variable switch to52// determine if assert should really be called.53//#define AWT_THREAD_ASSERTS_ENV_ASSERT5455// Define AWT_THREAD_ASSERTS_WAIT to make asserts halt the asserting thread56// for debugging purposes.57//#define AWT_THREAD_ASSERTS_WAIT5859#ifdef AWT_THREAD_ASSERTS_MESSAGES6061#define AWT_THREAD_ASSERTS_NOT_APPKIT_MESSAGE \62AWT_DEBUG_LOG(@"Not running on AppKit thread 0 when expected.")6364#define AWT_THREAD_ASSERTS_ON_APPKIT_MESSAGE \65AWT_DEBUG_LOG(@"Running on AppKit thread 0 when not expected.")6667#ifdef AWT_THREAD_ASSERTS_ENV_ASSERT6869extern int sAWTThreadAsserts;70#define AWT_THREAD_ASSERTS_ENV_ASSERT_CHECK \71do { \72if (sAWTThreadAsserts) { \73NSLog(@"\tPlease run this java program again with setenv COCOA_AWT_DISABLE_THREAD_ASSERTS to proceed with a warning."); \74assert(NO); \75} \76} while (0)7778#else7980#define AWT_THREAD_ASSERTS_ENV_ASSERT_CHECK do {} while (0)8182#endif /* AWT_THREAD_ASSERTS_ENV_ASSERT */8384#define AWT_ASSERT_APPKIT_THREAD \85do { \86if (pthread_main_np() == 0) { \87AWT_THREAD_ASSERTS_NOT_APPKIT_MESSAGE; \88AWT_DEBUG_BUG_REPORT_MESSAGE; \89AWT_THREAD_ASSERTS_ENV_ASSERT_CHECK; \90} \91} while (0)9293#define AWT_ASSERT_NOT_APPKIT_THREAD \94do { \95if (pthread_main_np() != 0) { \96AWT_THREAD_ASSERTS_ON_APPKIT_MESSAGE; \97AWT_DEBUG_BUG_REPORT_MESSAGE; \98AWT_THREAD_ASSERTS_ENV_ASSERT_CHECK; \99} \100} while (0)101102#endif /* AWT_THREAD_ASSERTS_MESSAGES */103104#ifdef AWT_THREAD_ASSERTS_WAIT105106#define AWT_ASSERT_APPKIT_THREAD \107do { \108while (pthread_main_np() == 0) {} \109} while (0)110111#define AWT_ASSERT_NOT_APPKIT_THREAD \112do { \113while (pthread_main_np() != 0) {} \114} while (0)115116#endif /* AWT_THREAD_ASSERTS_WAIT */117118#else /* AWT_THREAD_ASSERTS */119120#define AWT_ASSERT_APPKIT_THREAD do {} while (0)121#define AWT_ASSERT_NOT_APPKIT_THREAD do {} while (0)122123#endif /* AWT_THREAD_ASSERTS */124// --------------------------------------------------------------------------125126__attribute__((visibility("default")))127@interface ThreadUtilities : NSObject { } /* Extend NSObject so can call performSelectorOnMainThread */128129+ (JNIEnv*)getJNIEnv;130+ (JNIEnv*)getJNIEnvUncached;131+ (void)detachCurrentThread;132+ (void)setAppkitThreadGroup:(jobject)group;133134+ (void)performOnMainThreadWaiting:(BOOL)wait block:(void (^)())block;135+ (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)wait;136+ (NSString*)javaRunLoopMode;137@end138139JNIEXPORT void OSXAPP_SetJavaVM(JavaVM *vm);140141#endif /* __THREADUTILITIES_H */142143144