1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import <AppKit/AppKit.h>
  27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  28 #import <objc/message.h>
  29 
  30 #import "ThreadUtilities.h"
  31 
  32 
  33 // The following must be named "jvm", as there are extern references to it in AWT
  34 JavaVM *jvm = NULL;
  35 static JNIEnv *appKitEnv = NULL;
  36 static jobject appkitThreadGroup = NULL;
  37 static BOOL awtEmbedded = NO;
  38 
  39 inline void attachCurrentThread(void** env) {
  40     if ([NSThread isMainThread]) {
  41         JavaVMAttachArgs args;
  42         args.version = JNI_VERSION_1_4;
  43         args.name = "AppKit Thread";
  44         args.group = appkitThreadGroup;
  45         (*jvm)->AttachCurrentThreadAsDaemon(jvm, env, &args);
  46     } else {
  47         (*jvm)->AttachCurrentThreadAsDaemon(jvm, env, NULL);
  48     }
  49 }
  50 
  51 @implementation ThreadUtilities
  52 
  53 + (JNIEnv*)getJNIEnv {
  54 AWT_ASSERT_APPKIT_THREAD;
  55     if (appKitEnv == NULL) {
  56         attachCurrentThread((void **)&appKitEnv);
  57     }
  58     return appKitEnv;
  59 }
  60 
  61 + (JNIEnv*)getJNIEnvUncached {
  62     JNIEnv *env = NULL;
  63     attachCurrentThread((void **)&env);
  64     return env;
  65 }
  66 
  67 + (void)detachCurrentThread {
  68     (*jvm)->DetachCurrentThread(jvm);
  69 }
  70 
  71 + (void)setAppkitThreadGroup:(jobject)group {
  72     appkitThreadGroup = group;
  73 }
  74 
  75 + (void)performOnMainThreadWaiting:(BOOL)wait block:(void (^)())block {
  76     if ([NSThread isMainThread] && wait == YES) {
  77         block(); 
  78     } else { 
  79         [JNFRunLoop performOnMainThreadWaiting:wait withBlock:block]; 
  80     }
  81 }
  82 
  83 + (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)wait {
  84     if ([NSThread isMainThread] && wait == YES) {
  85         [target performSelector:aSelector withObject:arg];
  86     } else {
  87         [JNFRunLoop performOnMainThread:aSelector on:target withObject:arg waitUntilDone:wait];
  88     }
  89 }
  90 
  91 + (void)setAWTEmbedded:(BOOL)embedded {
  92     awtEmbedded = embedded;
  93 }
  94 
  95 + (BOOL)isAWTEmbedded {
  96     return awtEmbedded;
  97 }
  98 
  99 @end
 100 
 101 
 102 void OSXAPP_SetJavaVM(JavaVM *vm)
 103 {
 104     jvm = vm;
 105 }
 106