X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/bc0c0b491241f28c083c4b6540944ed604242ba2..4664c9757fb743652b03460ca4a08957cc0a249d:/src/java/jmsg.c diff --git a/src/java/jmsg.c b/src/java/jmsg.c index ed3e9d9af1..d40d249d78 100644 --- a/src/java/jmsg.c +++ b/src/java/jmsg.c @@ -11,6 +11,7 @@ */ #include "msg/msg.h" #include "msg/private.h" +#include "simix/private.h" #include "java/jxbt_context.h" #include "jmsg_process.h" @@ -22,31 +23,23 @@ 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; - - 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; +} /* @@ -85,7 +78,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,9 +104,9 @@ 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, + jprocess,env, &process->simdata->s_process); DEBUG1("context created (s_process=%p)",process->simdata->s_process); @@ -126,29 +119,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 @@ -488,7 +461,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)); @@ -522,6 +495,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 @@ -797,7 +771,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; @@ -945,7 +919,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))) @@ -968,7 +942,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")); } @@ -1063,11 +1037,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); } @@ -1079,20 +1049,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()) @@ -1117,49 +1086,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_os_mutex_t ctx_mutex, creation_mutex; - xbt_os_cond_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_os_mutex_lock(creation_mutex); - xbt_os_mutex_lock(ctx_mutex); - xbt_os_cond_signal( creation_cond ); - xbt_os_mutex_unlock( creation_mutex ); - xbt_os_cond_wait(ctx_cond, ctx_mutex); - xbt_os_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); + + jcontext_exit(process->simdata->s_process->simdata->context,0,get_current_thread_env()); } JNIEXPORT void JNICALL