src/macosx/bin/java_md_macosx.c

Print this page




  27 #include "jvm_md.h"
  28 #include <dirent.h>
  29 #include <dlfcn.h>
  30 #include <fcntl.h>
  31 #include <inttypes.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <stdlib.h>
  35 #include <sys/stat.h>
  36 #include <unistd.h>
  37 #include <sys/types.h>
  38 #include <sys/time.h>
  39 
  40 #include "manifest_info.h"
  41 #include "version_comp.h"
  42 
  43 /* Support Cocoa event loop on the main thread */
  44 #include <Cocoa/Cocoa.h>
  45 #include <objc/objc-runtime.h>
  46 #include <objc/objc-auto.h>
  47 #include <dispatch/dispatch.h>
  48 
  49 #include <errno.h>
  50 #include <spawn.h>
  51 
  52 struct NSAppArgs {
  53     int argc;
  54     char **argv;
  55 };
  56 
  57 #define JVM_DLL "libjvm.dylib"
  58 #define JAVA_DLL "libjava.dylib"
  59 /* FALLBACK avoids naming conflicts with system libraries
  60  * (eg, ImageIO's libJPEG.dylib) */
  61 #define LD_LIBRARY_PATH "DYLD_FALLBACK_LIBRARY_PATH"
  62 
  63 /*
  64  * If a processor / os combination has the ability to run binaries of
  65  * two data models and cohabitation of jre/jdk bits with both data
  66  * models is supported, then DUAL_MODE is defined. MacOSX is a hybrid
  67  * system in that, the universal library can contain all types of libraries


 984 }
 985 
 986 void
 987 SetXStartOnFirstThreadArg()
 988 {
 989     // XXX: BEGIN HACK
 990     // short circuit hack for <https://bugs.eclipse.org/bugs/show_bug.cgi?id=211625>
 991     // need a way to get AWT/Swing apps launched when spawned from Eclipse,
 992     // which currently has no UI to not pass the -XstartOnFirstThread option
 993     if (getenv("HACK_IGNORE_START_ON_FIRST_THREAD") != NULL) return;
 994     // XXX: END HACK
 995 
 996     sameThread = JNI_TRUE;
 997     // Set a variable that tells us we started on the main thread.
 998     // This is used by the AWT during startup. (See awt.m)
 999     char envVar[80];
1000     snprintf(envVar, sizeof(envVar), "JAVA_STARTED_ON_FIRST_THREAD_%d", getpid());
1001     setenv(envVar, "1", 1);
1002 }
1003 


























1004 // MacOSX we may continue in the same thread
1005 int
1006 JVMInit(InvocationFunctions* ifn, jlong threadStackSize,
1007                  int argc, char **argv,
1008                  int mode, char *what, int ret) {
1009     if (sameThread) {
1010         JLI_TraceLauncher("In same thread\n");
1011         // need to block this thread against the main thread
1012         // so signals get caught correctly
1013         __block int rslt;
1014         dispatch_sync(dispatch_get_main_queue(), ^(void) {
1015             JavaMainArgs args;
1016             args.argc = argc;
1017             args.argv = argv;
1018             args.mode = mode;
1019             args.what = what;
1020             args.ifn  = *ifn;
1021             rslt = JavaMain((void*)&args);
1022         });








1023         return rslt;
1024     } else {
1025         return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
1026     }
1027 }
1028 
1029 /*
1030  * Note the jvmInstance must be initialized first before entering into
1031  * ShowSplashScreen, as there is a callback into the JLI_GetJavaVMInstance.
1032  */
1033 void PostJVMInit(JNIEnv *env, jstring mainClass, JavaVM *vm) {
1034     jvmInstance = vm;
1035     SetMainClassForAWT(env, mainClass);
1036     ShowSplashScreen();
1037 }
1038 
1039 jboolean
1040 ProcessPlatformOption(const char* arg)
1041 {
1042     if (JLI_StrCmp(arg, "-XstartOnFirstThread") == 0) {


  27 #include "jvm_md.h"
  28 #include <dirent.h>
  29 #include <dlfcn.h>
  30 #include <fcntl.h>
  31 #include <inttypes.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <stdlib.h>
  35 #include <sys/stat.h>
  36 #include <unistd.h>
  37 #include <sys/types.h>
  38 #include <sys/time.h>
  39 
  40 #include "manifest_info.h"
  41 #include "version_comp.h"
  42 
  43 /* Support Cocoa event loop on the main thread */
  44 #include <Cocoa/Cocoa.h>
  45 #include <objc/objc-runtime.h>
  46 #include <objc/objc-auto.h>

  47 
  48 #include <errno.h>
  49 #include <spawn.h>
  50 
  51 struct NSAppArgs {
  52     int argc;
  53     char **argv;
  54 };
  55 
  56 #define JVM_DLL "libjvm.dylib"
  57 #define JAVA_DLL "libjava.dylib"
  58 /* FALLBACK avoids naming conflicts with system libraries
  59  * (eg, ImageIO's libJPEG.dylib) */
  60 #define LD_LIBRARY_PATH "DYLD_FALLBACK_LIBRARY_PATH"
  61 
  62 /*
  63  * If a processor / os combination has the ability to run binaries of
  64  * two data models and cohabitation of jre/jdk bits with both data
  65  * models is supported, then DUAL_MODE is defined. MacOSX is a hybrid
  66  * system in that, the universal library can contain all types of libraries


 983 }
 984 
 985 void
 986 SetXStartOnFirstThreadArg()
 987 {
 988     // XXX: BEGIN HACK
 989     // short circuit hack for <https://bugs.eclipse.org/bugs/show_bug.cgi?id=211625>
 990     // need a way to get AWT/Swing apps launched when spawned from Eclipse,
 991     // which currently has no UI to not pass the -XstartOnFirstThread option
 992     if (getenv("HACK_IGNORE_START_ON_FIRST_THREAD") != NULL) return;
 993     // XXX: END HACK
 994 
 995     sameThread = JNI_TRUE;
 996     // Set a variable that tells us we started on the main thread.
 997     // This is used by the AWT during startup. (See awt.m)
 998     char envVar[80];
 999     snprintf(envVar, sizeof(envVar), "JAVA_STARTED_ON_FIRST_THREAD_%d", getpid());
1000     setenv(envVar, "1", 1);
1001 }
1002 
1003 /* This class is made for performSelectorOnMainThread when java main
1004  * should be launched on main thread.
1005  * We cannot use dispatch_sync here, because it blocks the main dispatch queue 
1006  * which is used inside Cocoa
1007  */ 
1008 @interface JavaLaunchHelper : NSObject {
1009     int _returnValue;
1010 }
1011 - (void) launchJava:(NSValue*)argsValue;
1012 - (int) getReturnValue;
1013 @end
1014 
1015 @implementation JavaLaunchHelper
1016 
1017 - (void) launchJava:(NSValue*)argsValue
1018 {
1019     _returnValue = JavaMain([argsValue pointerValue]);
1020 }
1021 
1022 - (int) getReturnValue
1023 {
1024     return _returnValue;
1025 }
1026 
1027 @end
1028 
1029 // MacOSX we may continue in the same thread
1030 int
1031 JVMInit(InvocationFunctions* ifn, jlong threadStackSize,
1032                  int argc, char **argv,
1033                  int mode, char *what, int ret) {
1034     if (sameThread) {
1035         JLI_TraceLauncher("In same thread\n");
1036         // need to block this thread against the main thread
1037         // so signals get caught correctly


1038         JavaMainArgs args;
1039         args.argc = argc;
1040         args.argv = argv;
1041         args.mode = mode;
1042         args.what = what;
1043         args.ifn  = *ifn;
1044         int rslt;
1045         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1046         {
1047             JavaLaunchHelper* launcher = [[[JavaLaunchHelper alloc] init] autorelease];
1048             [launcher performSelectorOnMainThread:@selector(launchJava:)
1049                                        withObject:[NSValue valueWithPointer:(void*)&args]
1050                                     waitUntilDone:YES];
1051             rslt = [launcher getReturnValue];
1052         }
1053         [pool drain];
1054         return rslt;
1055     } else {
1056         return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
1057     }
1058 }
1059 
1060 /*
1061  * Note the jvmInstance must be initialized first before entering into
1062  * ShowSplashScreen, as there is a callback into the JLI_GetJavaVMInstance.
1063  */
1064 void PostJVMInit(JNIEnv *env, jstring mainClass, JavaVM *vm) {
1065     jvmInstance = vm;
1066     SetMainClassForAWT(env, mainClass);
1067     ShowSplashScreen();
1068 }
1069 
1070 jboolean
1071 ProcessPlatformOption(const char* arg)
1072 {
1073     if (JLI_StrCmp(arg, "-XstartOnFirstThread") == 0) {