From: Samuel Lepetit Date: Wed, 23 May 2012 13:31:58 +0000 (+0200) Subject: Stop using JNI_GetCreatedJavaVMs, cache the java_vm object X-Git-Tag: v3_9_90~569^2~19^2~64 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/aee9b1148b3e207d99efa9580ed8ac3611c143e6 Stop using JNI_GetCreatedJavaVMs, cache the java_vm object --- diff --git a/src/jmsg.c b/src/jmsg.c index 7ace464cc2..aaf5b10819 100644 --- a/src/jmsg.c +++ b/src/jmsg.c @@ -34,8 +34,7 @@ static int create_jprocess(int argc, char *argv[]); XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); -static JavaVM *__java_vm = NULL; - +JavaVM *__java_vm = NULL; JavaVM *get_java_VM(void) { @@ -86,6 +85,8 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) jstring jval; const char *tmp; + (*env)->GetJavaVM(env, &__java_vm); + smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init; setlocale(LC_NUMERIC,"C"); @@ -111,8 +112,6 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) free(argv[index]); free(argv); - - (*env)->GetJavaVM(env, &__java_vm); } JNIEXPORT void JNICALL diff --git a/src/jmsg.h b/src/jmsg.h index c8a3f15d2d..16c10fbcca 100644 --- a/src/jmsg.h +++ b/src/jmsg.h @@ -12,7 +12,6 @@ #include JavaVM *get_java_VM(void); - JNIEnv *get_current_thread_env(void); /** * This function throws the correct exception according to the status diff --git a/src/smx_context_java.c b/src/smx_context_java.c index 26e54ba48f..b327df579f 100644 --- a/src/smx_context_java.c +++ b/src/smx_context_java.c @@ -13,15 +13,7 @@ #include "smx_context_java.h" #include "jxbt_utilities.h" #include "xbt/dynar.h" -JavaVM *get_current_vm(void); -JavaVM *get_current_vm(void) -{ - JavaVM *jvm; - JNI_GetCreatedJavaVMs(&jvm,1,NULL); - return jvm; -} - - +extern JavaVM *__java_vm; XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)"); @@ -98,8 +90,7 @@ static void* smx_ctx_java_thread_run(void *data) { xbt_os_thread_set_extra_data(context); //Attach the thread to the JVM JNIEnv *env; - JavaVM *jvm = get_current_vm(); - jint error = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL); + jint error = (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL); xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM"); context->jenv = get_current_thread_env(); //Wait for the first scheduling round to happen. @@ -154,8 +145,7 @@ void smx_ctx_java_stop(smx_context_t context) /* detach the thread and kills it */ JNIEnv *env = ctx_java->jenv; (*env)->DeleteGlobalRef(env,ctx_java->jprocess); - JavaVM *jvm = get_current_vm(); - jint error = (*jvm)->DetachCurrentThread(jvm); + jint error = (*__java_vm)->DetachCurrentThread(__java_vm); xbt_assert((error == JNI_OK), "The thread couldn't be detached."); xbt_os_sem_release(((smx_ctx_java_t)context)->end); xbt_os_thread_exit(NULL);