Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ops, there is some more s_host fields around
[simgrid.git] / src / java / jmsg.c
index d0438cc..d27abbf 100644 (file)
@@ -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"
@@ -31,6 +32,8 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg,"MSG for Java(TM)");
 
 #include "jmsg.h"
 
+static JavaVM * __java_vm = NULL;
+
 
 #ifdef WIN32
   static DWORD __current_thread_id = 0;
@@ -48,6 +51,20 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg,"MSG for Java(TM)");
   }
 #endif
 
+JavaVM *
+get_java_VM(void) {
+       return __java_vm;
+}
+
+JNIEnv *
+get_current_thread_env(void) {
+       JNIEnv *env;
+
+    (*__java_vm)->AttachCurrentThread(__java_vm, (void **)&env, NULL);
+
+        return env;
+}
+
 
 /*
  * The MSG process connected functions implementation.                                 
@@ -85,7 +102,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,10 +128,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,
-                       __MSG_process_cleanup,
+                       jprocess,env,
                        &process->simdata->s_process);
   DEBUG1("context created (s_process=%p)",process->simdata->s_process);
 
@@ -127,29 +143,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 
@@ -489,7 +485,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 +519,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 
@@ -798,7 +795,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 +943,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 +966,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"));
 }
@@ -1069,6 +1066,8 @@ Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
 #else
   __current_thread_id = pthread_self();
 #endif
+
+(*env)->GetJavaVM(env,&__java_vm);
        
 }
 
@@ -1080,20 +1079,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 +1116,26 @@ 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;
-  }
+  xbt_os_mutex_t creation_mutex = xbt_creation_mutex_get();
+  xbt_os_cond_t creation_cond = xbt_creation_cond_get();
+  xbt_os_mutex_lock(creation_mutex);
+  xbt_os_cond_signal( creation_cond );
+  xbt_os_mutex_unlock( creation_mutex );
 
-  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);
+
+  jcontext_exit(process->simdata->s_process->simdata->context,0,get_current_thread_env());
 }
 
 JNIEXPORT void JNICALL