jdk/src/macosx/native/sun/osxapp/ThreadUtilities.m
author pchelko
Thu, 20 Feb 2014 09:36:58 +0400
changeset 23921 1cb9d9ff4e61
parent 23900 fd98305f0d19
child 25099 ba8a24b2576f
permissions -rw-r--r--
8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it Reviewed-by: serb, art, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 15985
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
#import <AppKit/AppKit.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
#import <JavaNativeFoundation/JavaNativeFoundation.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
#import <objc/message.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
#import "ThreadUtilities.h"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    32
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
// The following must be named "jvm", as there are extern references to it in AWT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
JavaVM *jvm = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
static JNIEnv *appKitEnv = NULL;
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    36
static jobject appkitThreadGroup = NULL;
23921
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    37
static BOOL awtEmbedded = NO;
23893
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    38
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    39
inline void attachCurrentThread(void** env) {
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    40
    if ([NSThread isMainThread]) {
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    41
        JavaVMAttachArgs args;
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    42
        args.version = JNI_VERSION_1_4;
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    43
        args.name = "AppKit Thread";
23893
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    44
        args.group = appkitThreadGroup;
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    45
        (*jvm)->AttachCurrentThreadAsDaemon(jvm, env, &args);
23893
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    46
    } else {
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    47
        (*jvm)->AttachCurrentThreadAsDaemon(jvm, env, NULL);
23893
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    48
    }
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    49
}
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
@implementation ThreadUtilities
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
+ (JNIEnv*)getJNIEnv {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    54
AWT_ASSERT_APPKIT_THREAD;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    55
    if (appKitEnv == NULL) {
23893
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    56
        attachCurrentThread((void **)&appKitEnv);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
    return appKitEnv;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
+ (JNIEnv*)getJNIEnvUncached {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
    JNIEnv *env = NULL;
23893
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    63
    attachCurrentThread((void **)&env);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
    return env;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    65
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    66
23893
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    67
+ (void)detachCurrentThread {
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    68
    (*jvm)->DetachCurrentThread(jvm);
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    69
}
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    70
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    71
+ (void)setAppkitThreadGroup:(jobject)group {
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    72
    appkitThreadGroup = group;
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    73
}
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
    74
14753
a56a685d137f 7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents: 14342
diff changeset
    75
+ (void)performOnMainThreadWaiting:(BOOL)wait block:(void (^)())block {
a56a685d137f 7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents: 14342
diff changeset
    76
    if ([NSThread isMainThread] && wait == YES) {
a56a685d137f 7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents: 14342
diff changeset
    77
        block(); 
a56a685d137f 7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents: 14342
diff changeset
    78
    } else { 
a56a685d137f 7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents: 14342
diff changeset
    79
        [JNFRunLoop performOnMainThreadWaiting:wait withBlock:block]; 
a56a685d137f 7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents: 14342
diff changeset
    80
    }
a56a685d137f 7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents: 14342
diff changeset
    81
}
a56a685d137f 7154778: [macosx] NSView-based implementation of sun.awt.EmbeddedFrame
serb
parents: 14342
diff changeset
    82
15322
3638f33225ec 7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents: 14753
diff changeset
    83
+ (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)wait {
3638f33225ec 7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents: 14753
diff changeset
    84
    if ([NSThread isMainThread] && wait == YES) {
3638f33225ec 7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents: 14753
diff changeset
    85
        [target performSelector:aSelector withObject:arg];
3638f33225ec 7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents: 14753
diff changeset
    86
    } else {
3638f33225ec 7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents: 14753
diff changeset
    87
        [JNFRunLoop performOnMainThread:aSelector on:target withObject:arg waitUntilDone:wait];
3638f33225ec 7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents: 14753
diff changeset
    88
    }
3638f33225ec 7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents: 14753
diff changeset
    89
}
3638f33225ec 7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents: 14753
diff changeset
    90
23921
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    91
+ (void)setAWTEmbedded:(BOOL)embedded {
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    92
    awtEmbedded = embedded;
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    93
}
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    94
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    95
+ (BOOL)isAWTEmbedded {
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    96
    return awtEmbedded;
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    97
}
1cb9d9ff4e61 8034870: Regression: On Mac, fx app can't be launched if setting a javaagent for it
pchelko
parents: 23900
diff changeset
    98
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
@end
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
void OSXAPP_SetJavaVM(JavaVM *vm)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
    jvm = vm;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106