Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/osxapp/ThreadUtilities.m
38829 views
/*1* Copyright (c) 2011, 2014, 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#import <AppKit/AppKit.h>26#import <JavaNativeFoundation/JavaNativeFoundation.h>27#import <objc/message.h>2829#import "ThreadUtilities.h"303132// The following must be named "jvm", as there are extern references to it in AWT33JavaVM *jvm = NULL;34static JNIEnv *appKitEnv = NULL;35static jobject appkitThreadGroup = NULL;36static BOOL awtEmbedded = NO;3738inline void attachCurrentThread(void** env) {39if ([NSThread isMainThread]) {40JavaVMAttachArgs args;41args.version = JNI_VERSION_1_4;42args.name = "AppKit Thread";43args.group = appkitThreadGroup;44(*jvm)->AttachCurrentThreadAsDaemon(jvm, env, &args);45} else {46(*jvm)->AttachCurrentThreadAsDaemon(jvm, env, NULL);47}48}4950@implementation ThreadUtilities5152+ (JNIEnv*)getJNIEnv {53AWT_ASSERT_APPKIT_THREAD;54if (appKitEnv == NULL) {55attachCurrentThread((void **)&appKitEnv);56}57return appKitEnv;58}5960+ (JNIEnv*)getJNIEnvUncached {61JNIEnv *env = NULL;62attachCurrentThread((void **)&env);63return env;64}6566+ (void)detachCurrentThread {67(*jvm)->DetachCurrentThread(jvm);68}6970+ (void)setAppkitThreadGroup:(jobject)group {71appkitThreadGroup = group;72}7374+ (void)performOnMainThreadWaiting:(BOOL)wait block:(void (^)())block {75if ([NSThread isMainThread] && wait == YES) {76block();77} else {78[JNFRunLoop performOnMainThreadWaiting:wait withBlock:block];79}80}8182+ (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)wait {83if ([NSThread isMainThread] && wait == YES) {84[target performSelector:aSelector withObject:arg];85} else {86[JNFRunLoop performOnMainThread:aSelector on:target withObject:arg waitUntilDone:wait];87}88}8990+ (void)setAWTEmbedded:(BOOL)embedded {91awtEmbedded = embedded;92}9394+ (BOOL)isAWTEmbedded {95return awtEmbedded;96}9798@end99100101void OSXAPP_SetJavaVM(JavaVM *vm)102{103jvm = vm;104}105106107108