Path: blob/master/src/java.desktop/macosx/native/libosxapp/ThreadUtilities.m
41149 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#import <AppKit/AppKit.h>26#import <objc/message.h>2728#import "ThreadUtilities.h"293031// The following must be named "jvm", as there are extern references to it in AWT32JavaVM *jvm = NULL;33static JNIEnv *appKitEnv = NULL;34static jobject appkitThreadGroup = NULL;35static NSString* JavaRunLoopMode = @"javaRunLoopMode";36static NSArray<NSString*> *javaModes = nil;3738static inline 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+ (void)initialize {53/* All the standard modes plus ours */54javaModes = [[NSArray alloc] initWithObjects:NSDefaultRunLoopMode,55NSModalPanelRunLoopMode,56NSEventTrackingRunLoopMode,57JavaRunLoopMode,58nil];59}6061+ (JNIEnv*)getJNIEnv {62AWT_ASSERT_APPKIT_THREAD;63if (appKitEnv == NULL) {64attachCurrentThread((void **)&appKitEnv);65}66return appKitEnv;67}6869+ (JNIEnv*)getJNIEnvUncached {70JNIEnv *env = NULL;71attachCurrentThread((void **)&env);72return env;73}7475+ (void)detachCurrentThread {76(*jvm)->DetachCurrentThread(jvm);77}7879+ (void)setAppkitThreadGroup:(jobject)group {80appkitThreadGroup = group;81}8283/* This is needed because we can't directly pass a block to84* performSelectorOnMainThreadWaiting .. since it expects a selector85*/86+ (void)invokeBlock:(void (^)())block {87block();88}8990/*91* When running a block where either we don't wait, or it needs to run on another thread92* we need to copy it from stack to heap, use the copy in the call and release after use.93* Do this only when we must because it could be expensive.94* Note : if waiting cross-thread, possibly the stack allocated copy is accessible ?95*/96+ (void)invokeBlockCopy:(void (^)(void))blockCopy {97blockCopy();98Block_release(blockCopy);99}100101+ (void)performOnMainThreadWaiting:(BOOL)wait block:(void (^)())block {102if ([NSThread isMainThread] && wait == YES) {103block();104} else {105if (wait == YES) {106[self performOnMainThread:@selector(invokeBlock:) on:self withObject:block waitUntilDone:YES];107} else {108void (^blockCopy)(void) = Block_copy(block);109[self performOnMainThread:@selector(invokeBlockCopy:) on:self withObject:blockCopy waitUntilDone:NO];110}111}112}113114+ (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)wait {115if ([NSThread isMainThread] && wait == YES) {116[target performSelector:aSelector withObject:arg];117} else {118[target performSelectorOnMainThread:aSelector withObject:arg waitUntilDone:wait modes:javaModes];119}120}121122+ (NSString*)javaRunLoopMode {123return JavaRunLoopMode;124}125126@end127128129void OSXAPP_SetJavaVM(JavaVM *vm)130{131jvm = vm;132}133134135136