sventon subversion web client - http://www.sventon.org
[show recent changes]
 
  Help
HEAD
Rev: 10595 - svn://svn.freehep.org/svn / freehep / trunk / jaida / freehep-aidajni / src / main / c / JNICUtil.c
Unified Diff View - JNICUtil.c
Search for Help
/freehep/trunk/jaida/freehep-aidajni/src/main/c/JNICUtil.c @ revision 10594
/freehep/trunk/jaida/freehep-aidajni/src/main/c/JNICUtil.c @ revision 10595
@@ -4,147 +4,16 @@ #include <stdlib.h> #include <stdarg.h> -#include "JNICUtil.h" +/* Apple only */ +/* #include <pthread.h> */ -typedef jint (JNICALL *CreateJavaVM_t)(JavaVM **vm, void **env, void *vm_args); -typedef jint (JNICALL *GetDefaultJavaVMInitArgs_t)(void *vm_args); -typedef jint (JNICALL *GetCreatedJavaVMs_t)(JavaVM **vmBuf, jsize bufLen, jsize *nVMs); +#include "JNICUtil.h" static int JNICUtilDebug = -1; static int JNICUtilVMwasDestroyed = JNI_FALSE; static int JNICUtilVMwasCreatedByUs = JNI_FALSE; -#ifdef LINK_WITHOUT_JVM - -#ifdef WIN32 -#include "sys/JNICUtil-WIN32.h" -#endif - -#ifdef Linux -#include "sys/JNICUtil-Linux.h" -#endif - -#ifdef Darwin -#include "sys/JNICUtil-Darwin.h" -#endif - -#ifdef SOLARIS2 -#include "sys/JNICUtil-SUN.h" -#endif - -DLHandle JNICloadLibraryFromPath(char* env) { -    char* jvmpath; -    DLHandle handle; - - jvmpath = getenv(env); - if (jvmpath == NULL) return NULL; - - if (JNICdebug()) printf("Trying to find JVM at %s using %s.\n", jvmpath, env); -    handle = JNICloadLibrary(jvmpath); -    if (handle != NULL) { -        if (JNICdebug()) printf("Loaded JVM from %s using %s.\n", jvmpath, env); -        return handle; -    } - -    printf("Warning: Error loading JVM from %s using %s.\n", jvmpath, env); -    printf("%s", JNICgetLastError()); -    return NULL; -} - -DLHandle JNICloadLibraryFromAbsolutePaths() { -    char* jvmpath; -    char** paths; -    DLHandle handle; - -    paths = JNICjvmAbsolutePaths; -    while (*paths != NULL) { -        jvmpath = malloc(strlen(*paths)+sizeof(JNICseparator)+strlen(JNICjvmName)+1); -        strcpy(jvmpath, *paths); -        strcat(jvmpath, JNICseparator); -        strcat(jvmpath, JNICjvmName); - -     if (JNICdebug()) printf("Trying to find JVM at absolute path %s...\n", jvmpath); -        handle = JNICloadLibrary(jvmpath); -        if (handle != NULL) { -            if (JNICdebug()) printf("Loaded JVM from %s.\n", jvmpath); -            free(jvmpath); -            return handle; -        } -        free(jvmpath); -        paths++; -    } -    return NULL; -} - -DLHandle JNICloadLibraryFromHomePaths(char* env) { -    char* home; -    char* jvmpath; -    char** paths; -    DLHandle handle; - - home = getenv(env); - if (home == NULL) return NULL; - -    paths = JNICjvmHomePaths; -    while (*paths != NULL) { -        jvmpath = malloc(strlen(home)+sizeof(JNICseparator)+strlen(*paths)+sizeof(JNICseparator)+strlen(JNICjvmName)+1); -        strcpy(jvmpath, home); -        strcat(jvmpath, JNICseparator); -        strcat(jvmpath, *paths); -        strcat(jvmpath, JNICseparator); -        strcat(jvmpath, JNICjvmName); - -     if (JNICdebug()) printf("Trying to find JVM at %s using %s...\n", jvmpath, env); -        handle = JNICloadLibrary(jvmpath); -        if (handle != NULL) { -            if (JNICdebug()) printf("Loaded JVM from %s using %s.\n", jvmpath, env); -            free(jvmpath); -            return handle; -        } -        free(jvmpath); -        paths++; -    } -    printf("Warning: Error loading JVM using %s.\n", env); -    printf("%s", JNICgetLastError()); -    return NULL; -} - -DLHandle JNICloadLibraryFromRegistry() { -    char* jvmpath; -    DLHandle handle; - -    jvmpath = JNICgetRegistryJVMPath(); -    if (jvmpath == NULL) return NULL; - -    if (JNICdebug()) printf("Trying to find JVM at %s using Registry...\n", jvmpath); -    handle = JNICloadLibrary(jvmpath); -    if (handle != NULL) { -        if (JNICdebug()) printf("Loaded JVM from %s using Registry.\n", jvmpath); -        return handle; -    } - -    printf("Warning: Error loading JVM from %s using Registry.\n", jvmpath); -    printf("%s", JNICgetLastError()); -    return NULL; -} - -DLSymbol JNICfindSymbol(DLHandle handle, char* name) { -    DLSymbol symbol; - -    if (JNICdebug()) printf("Looking for symbol %s...\n", name); -    symbol = JNICgetSymbol(handle, name); -    if (symbol == NULL) { -        printf("Error: cannot find JNI interface %s.\n", name); -        printf("%s", JNICgetLastError()); -    } else { -        if (JNICdebug()) printf("Found symbol %s.\n", name); -    } -    return symbol; -} - -#endif /* LINK_WITHOUT_JVM */ - int JNICexceptions(JNIEnv *env) {     if ((*env)->ExceptionOccurred(env) != NULL) {         if (JNICdebug()) (*env)->ExceptionDescribe(env); @@ -162,15 +31,7 @@ } -int JNICcreateJVM(JNIEnv **env, JavaVM **jvm) { -#ifdef LINK_WITHOUT_JVM -    DLHandle handle = NULL; -#endif - -    CreateJavaVM_t CreateJavaVM = NULL; -    GetDefaultJavaVMInitArgs_t GetDefaultJavaVMInitArgs = NULL; -    GetCreatedJavaVMs_t GetCreatedJavaVMs = NULL; - +int JNICcreateJVM(JNIEnv **env, JavaVM **jvm) {  int nOptions = 0;     char *classpathOption;     char *classpathOptionKey; @@ -186,54 +47,20 @@     jsize nSize = 1;     jsize nVms;     jint nStatus; - - -#ifdef LINK_WITHOUT_JVM -    /* Look for jvm.dll or jvm.so */ -    if (JNICdebug()) printf("Looking for JVM...\n"); - -    /* Load from JVM_PATH */ -    if (handle == NULL) handle = JNICloadLibraryFromPath("JVM_PATH"); - -    /* Load from JRE_HOME */ -    if (handle == NULL) handle = JNICloadLibraryFromHomePaths("JRE_HOME"); - -    /* Load from JDK_HOME */ -    if (handle == NULL) handle = JNICloadLibraryFromHomePaths("JDK_HOME"); - -    /* Load from JAVA_HOME */ -    if (handle == NULL) handle = JNICloadLibraryFromHomePaths("JAVA_HOME"); - -    /* Load from Absolute Paths */ -    if (handle == NULL) handle = JNICloadLibraryFromAbsolutePaths(); - -    /* Load from Registry */ -    if (handle == NULL) handle = JNICloadLibraryFromRegistry(); - -    /* No other options */ -    if (handle == NULL) { -        printf("Could not find JVM"); -        return -2; -    } - -    /* Load all symbols */ -    if ((CreateJavaVM = (CreateJavaVM_t)JNICfindSymbol(handle, "JNI_CreateJavaVM")) == NULL) return -2; -    if ((GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)JNICfindSymbol(handle, "JNI_GetDefaultJavaVMInitArgs")) == NULL) return -2; -    if ((GetCreatedJavaVMs = (GetCreatedJavaVMs_t)JNICfindSymbol(handle, "JNI_GetCreatedJavaVMs")) == NULL) return -2; -#else -    CreateJavaVM = JNI_CreateJavaVM; -    GetDefaultJavaVMInitArgs = JNI_GetDefaultJavaVMInitArgs; -    GetCreatedJavaVMs = JNI_GetCreatedJavaVMs; -    if (JNICdebug()) printf("Loaded linked in JVM.\n"); -#endif /* LINK_WITHOUT_JVM */ - /* NOTE: one can only create one JVM per process, so we check if there            is already one there */     if (JNICdebug()) printf("Looking for already loaded JVMs...\n"); -    nStatus = GetCreatedJavaVMs(jvm, nSize, &nVms); +/* FIXME, this call does NOT seem to work (Bus Error) on MacOSX i386, so we ignore existing JVMs */ +#if defined(__i386__) && defined(__APPLE__) +    nStatus = JNI_OK; +    nVms = 0; +    nSize = 0; +#else +    nStatus = JNI_GetCreatedJavaVMs(jvm, nSize, &nVms); +#endif     if ((nStatus == JNI_OK) && (nVms > 0)) { -        if (JNICdebug()) printf("Number of jvm's returned: %d", (int)nVms); +     if (JNICdebug()) printf("Number of jvm's returned: %d", (int)nVms);         (**jvm)->GetEnv(*jvm, (void **)env, JNI_VERSION_1_4);         if (JNICdebug()) printf("Using already loaded JVM.\n");         return nStatus; @@ -311,6 +138,56 @@         }     } +#if 0 + /* Apple only */ + /* create a new pthread copying the stack size of the primordial pthread, see simpleJavaLauncher on apple dev */ + struct rlimit limit; + size_t stack_size = 0; + int rca = getrlimit(RLIMIT_STACK, &limit); + if (rca == 0) { +  if (limit.rlim_cur != 0LL) { +   stack_size = (size_t)limit.rlim_cur; +  } + } + + pthread_attr_t thread_attr; + pthread_attr_init(&thread_attr); + pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM); + pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); + if (stack_size > 0) { +  pthread_attr_setstacksize(&thread_attr, stack_size); + } + + /* Start the thread that we will start the JVM on. */ + pthread_t vmthread; + pthread_create(&vmthread, &thread_attr, JNI_CreateJavaVM, (void**)env, &vm_args); + pthread_attr_destroy(&thread_attr); + +    /* Create a a sourceContext to be used by our source that makes */ +    /* sure the CFRunLoop doesn't exit right away */ + CFRunLoopSourceContext sourceContext; +    sourceContext.version = 0; +    sourceContext.info = NULL; +    sourceContext.retain = NULL; +    sourceContext.release = NULL; +    sourceContext.copyDescription = NULL; +    sourceContext.equal = NULL; +    sourceContext.hash = NULL; +    sourceContext.schedule = NULL; +    sourceContext.cancel = NULL; +    sourceContext.perform = &sourceCallBack; + +    /* Create the Source from the sourceContext */ +    CFRunLoopSourceRef sourceRef = CFRunLoopSourceCreate (NULL, 0, &sourceContext); + +    /* Use the constant kCFRunLoopCommonModes to add the source to the set of objects */ +    /* monitored by all the common modes */ +    CFRunLoopAddSource (CFRunLoopGetCurrent(),sourceRef,kCFRunLoopCommonModes); + +    /* Park this thread in the runloop */ +    CFRunLoopRun(); +#endif +     if (JNICdebug()) printf("Starting JVM...");     /* create VM init args */  vm_args.version = JNI_VERSION_1_4; @@ -319,7 +196,7 @@  vm_args.ignoreUnrecognized = JNI_TRUE;     /* create VM */ - rc = CreateJavaVM(jvm, (void **)env, &vm_args); + rc = JNI_CreateJavaVM(jvm, (void **)env, &vm_args);     /* freeing memory */  free(options);


feed icon

sventon 2.5.1