X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/3913bb7b7d28a6c8f383b7973f72a4fc7a1f4f01..15a3ae325b9d004f1aed997ed476b044accb45ea:/src/java/jmsg.c diff --git a/src/java/jmsg.c b/src/java/jmsg.c index d0438ccd33..2495f52173 100644 --- a/src/java/jmsg.c +++ b/src/java/jmsg.c @@ -11,7 +11,8 @@ */ #include "msg/msg.h" #include "msg/private.h" -#include "java/jxbt_context.h" +#include "simix/private.h" +#include "xbt/xbt_context_private.h" #include "jmsg_process.h" #include "jmsg_host.h" @@ -20,33 +21,32 @@ #include "jmsg_channel.h" #include "jxbt_utilities.h" -XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg,"MSG for Java(TM)"); - -/* header for windows */ -#ifdef WIN32 -#include -#else -#include -#endif - #include "jmsg.h" +static JavaVM * __java_vm = NULL; -#ifdef WIN32 - static DWORD __current_thread_id = 0; +static jobject +native_to_java_process(m_process_t process); - int is_main_thread() { - return (GetCurrentThreadId() == __current_thread_id); - } +JavaVM * +get_java_VM(void) { + return __java_vm; +} -#else /* !WIN32 */ +JNIEnv * +get_current_thread_env(void) { + JNIEnv *env; - static pthread_t __current_thread_id = 0; + (*__java_vm)->AttachCurrentThread(__java_vm, (void **)&env, NULL); - int is_main_thread() { - return (pthread_self() == __current_thread_id); - } -#endif + return env; +} + +static jobject +native_to_java_process(m_process_t process) +{ + return ((xbt_jcontext_t)(process->simdata->s_process->simdata->context))->jprocess; +} /* @@ -85,7 +85,7 @@ Java_simgrid_msg_Msg_processCreate(JNIEnv* env, jclass cls, jobject jprocess_arg return; } - /* bind the java process instance to the native host */ + /* bind the java process instance to the native process */ jprocess_bind(jprocess,process,env); /* build the C name of the process */ @@ -111,11 +111,12 @@ Java_simgrid_msg_Msg_processCreate(JNIEnv* env, jclass cls, jobject jprocess_arg env); SIMIX_jprocess_create(process->name, - process->simdata->m_host->simdata->s_host, + process->simdata->m_host->simdata->smx_host, /*data*/ (void*)process, - jprocess_arg,env, - __MSG_process_cleanup, + jprocess,env, &process->simdata->s_process); + + DEBUG1("context created (s_process=%p)",process->simdata->s_process); @@ -127,29 +128,9 @@ Java_simgrid_msg_Msg_processCreate(JNIEnv* env, jclass cls, jobject jprocess_arg process->simdata->last_errno = MSG_OK; - -#ifdef KILLME - /* add the process in the list of the process of the host */ - xbt_fifo_unshift(host->simdata->process_list, process); - - self = msg_global->current_process; - - process->simdata->context->env = env; - - /* start the java process */ - xbt_context_start(process->simdata->context); - - msg_global->current_process = self; -#endif - /* add the process to the list of the processes of the simulation */ xbt_fifo_unshift(msg_global->process_list, process); - /* add the process to the list of the processes to run in the simulation */ - // xbt_fifo_unshift(msg_global->process_to_run, process); - - // PAJE_PROCESS_NEW(process); - //#endif } JNIEXPORT void JNICALL @@ -204,7 +185,7 @@ Java_simgrid_msg_Msg_processKill(JNIEnv* env, jclass cls, jobject jprocess) { } /* delete the global reference */ - jprocess_delete_global_ref(SIMIX_process_get_jprocess(process->simdata->s_process),env); + jprocess_delete_global_ref(native_to_java_process(process),env); /* kill the native process (this wrapper is call by the destructor of the java * process instance) @@ -244,12 +225,12 @@ Java_simgrid_msg_Msg_processFromPID(JNIEnv* env, jclass cls, jint PID) { return NULL; } - if(!SIMIX_process_get_jprocess(process->simdata->s_process)) { + if(!native_to_java_process(process)) { jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed")); return NULL; } - return (jobject)SIMIX_process_get_jprocess(process->simdata->s_process); + return (jobject)(native_to_java_process(process)); } @@ -288,7 +269,7 @@ Java_simgrid_msg_Msg_processSelf(JNIEnv* env, jclass cls) { return NULL; } - jprocess = SIMIX_process_get_jprocess(process->simdata->s_process); + jprocess = native_to_java_process(process); if(!jprocess) jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed")); @@ -489,7 +470,7 @@ Java_simgrid_msg_Msg_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jdouble jcomputeDuration, jdouble jmessageSize) { m_task_t task; /* the native task to create */ const char* name; /* the name of the task */ - + if(jcomputeDuration < 0) { jxbt_throw_illegal(env,bprintf("Task ComputeDuration (%f) cannot be negative", (double)jcomputeDuration)); @@ -523,6 +504,7 @@ Java_simgrid_msg_Msg_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring if ( ! task->data ) jxbt_throw_jni(env,"global ref allocation failed"); + } JNIEXPORT void JNICALL @@ -610,7 +592,7 @@ Java_simgrid_msg_Msg_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) { } process = MSG_task_get_sender(task); - return SIMIX_process_get_jprocess(process->simdata->s_process); + return (jobject)native_to_java_process(process); } JNIEXPORT jobject JNICALL @@ -627,7 +609,7 @@ Java_simgrid_msg_Msg_parallelTaskGetSender(JNIEnv* env , jclass cls , jobject jt process = MSG_task_get_sender(task); - return SIMIX_process_get_jprocess(process->simdata->s_process); + return (jobject)native_to_java_process(process); } JNIEXPORT jobject JNICALL @@ -798,7 +780,7 @@ Java_simgrid_msg_Msg_taskDestroy(JNIEnv* env, jclass cls, jobject jtask_arg) { jobject jtask; if(!task){ - jxbt_throw_notbound(env,"task",jtask); + jxbt_throw_notbound(env,"task",task); return; } jtask = (jobject)task->data; @@ -946,7 +928,7 @@ Java_simgrid_msg_Msg_channelGetHostWaitingTasks(JNIEnv* env, jclass cls, JNIEXPORT void JNICALL Java_simgrid_msg_Msg_channelPut(JNIEnv* env, jclass cls, jobject jchannel, jobject jtask, jobject jhost) { - + if(MSG_OK != MSG_task_put(jtask_to_native_task(jtask,env), jhost_get_native(env,jhost), (int)jchannel_get_id(jchannel,env))) @@ -969,7 +951,7 @@ Java_simgrid_msg_Msg_channelPutWithTimeout(JNIEnv* env, jclass cls, jxbt_throw_notbound(env,"task",jtask); return; } - + if(MSG_OK != MSG_task_put_with_timeout(task,host,id,(double)jtimeout)) jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed")); } @@ -1064,11 +1046,7 @@ Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) { free(argv); -#ifdef WIN32 - __current_thread_id = GetCurrentThreadId(); -#else - __current_thread_id = pthread_self(); -#endif + (*env)->GetJavaVM(env,&__java_vm); } @@ -1080,20 +1058,19 @@ JNICALL Java_simgrid_msg_Msg_run(JNIEnv* env, jclass cls) { /* Run everything */ if(MSG_OK != MSG_main()) - jxbt_throw_native(env, xbt_strdup("MSG_main() failed")); - + jxbt_throw_native(env, xbt_strdup("MSG_main() failed")); + DEBUG0("MSG_main finished. Bail out before cleanup since there is a bug in this part."); - SIMIX_display_process_status(); - exit(0); /* FIXME */ - + DEBUG0("Clean java world"); /* Cleanup java hosts */ xbt_fifo_foreach(msg_global->host,item,host,m_host_t) { jhost = (jobject)host->data; if(jhost) - jhost_unref(env,jhost); + jhost_unref(env,jhost); } + DEBUG0("Clean native world"); /* cleanup native stuff */ if(MSG_OK != MSG_clean()) @@ -1118,49 +1095,20 @@ Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatform JNIEXPORT void JNICALL Java_simgrid_msg_Msg_waitSignal(JNIEnv* env, jclass cls, jobject jprocess) { - m_process_t m_process = jprocess_to_native_process(jprocess,env); - smx_process_t s_process; - - xbt_mutex_t ctx_mutex, creation_mutex; - xbt_thcond_t ctx_cond, creation_cond; - - DEBUG3("Msg_waitSignal(m_process=%p %s/%s)", - m_process,m_process->name,m_process->simdata->m_host->name); - if (!m_process){ - jxbt_throw_notbound(env,"process",jprocess); - return; - } - - s_process = m_process->simdata->s_process; - - if (s_process == NULL) { - jxbt_throw_notbound(env,"SIMIX process",jprocess); - return; - } - - ctx_mutex = SIMIX_process_get_jmutex(s_process); - ctx_cond = SIMIX_process_get_jcond(s_process); - creation_mutex = xbt_creation_mutex_get(); - creation_cond = xbt_creation_cond_get(); - - xbt_mutex_lock(creation_mutex); - xbt_mutex_lock(ctx_mutex); - xbt_thcond_signal( creation_cond ); - xbt_mutex_unlock( creation_mutex ); - xbt_thcond_wait(ctx_cond, ctx_mutex); - xbt_mutex_unlock(ctx_mutex); } JNIEXPORT void JNICALL Java_simgrid_msg_Msg_processExit(JNIEnv* env, jclass cls, jobject jprocess) { + m_process_t process = jprocess_to_native_process(jprocess,env); if (!process){ jxbt_throw_notbound(env,"process",jprocess); return; } - MSG_process_kill(process); + + xbt_context_stop(0); } JNIEXPORT void JNICALL @@ -1221,4 +1169,20 @@ Java_simgrid_msg_Msg_allHosts(JNIEnv * env, jclass cls_arg) { return jtable; } + +JNIEXPORT void JNICALL +Java_simgrid_msg_Msg_selectContextFactory(JNIEnv * env, jclass class,jstring jname) +{ + int rv; + + /* get the C string from the java string*/ + const char* name = (*env)->GetStringUTFChars(env, jname, 0); + + rv = xbt_context_select_factory(name); + + (*env)->ReleaseStringUTFChars(env, jname, name); + + if(rv) + jxbt_throw_native(env, xbt_strdup("xbt_select_context_factory() failed")); +}