Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not include C files. Instead, add them to the project file. Hopefully, this will...
[simgrid.git] / src / java / jmsg.c
index ed3e9d9..9d7c85a 100644 (file)
@@ -11,7 +11,9 @@
  */
 #include "msg/msg.h"
 #include "msg/private.h"
-#include "java/jxbt_context.h"
+#include "simix/private.h"
+#include "xbt/xbt_jcontext.h"
+#include "xbt/xbt_context_factory.h"
 
 #include "jmsg_process.h"
 #include "jmsg_host.h"
 #include "jmsg_channel.h"
 #include "jxbt_utilities.h"
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg,"MSG for Java(TM)");
+#include "jmsg.h"
 
-/* header for windows */
-#ifdef WIN32
-#include <windows.h>
-#else
-#include <pthread.h>
-#endif
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
-#include "jmsg.h"
+static JavaVM * __java_vm = NULL;
 
+static jobject
+native_to_java_process(m_process_t process);
 
-#ifdef WIN32
-  static DWORD __current_thread_id = 0;
+JavaVM *
+get_java_VM(void) {
+       return __java_vm;
+}
 
-  int is_main_thread() {
-    return (GetCurrentThreadId() == __current_thread_id);
-  }
+JNIEnv *
+get_current_thread_env(void) {
+       JNIEnv *env;
 
-#else /* !WIN32 */
+    (*__java_vm)->AttachCurrentThread(__java_vm, (void **)&env, NULL);
 
-  static pthread_t __current_thread_id = 0;
+        return env;
+}
 
-  int is_main_thread() {
-    return (pthread_self() == __current_thread_id);
-  }
-#endif
+static jobject
+native_to_java_process(m_process_t process)
+{
+       return ((xbt_jcontext_t)(process->simdata->s_process->simdata->context))->jprocess;     
+}
 
 
 /*
@@ -85,7 +88,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 +114,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,
+                       jprocess,env,
                        &process->simdata->s_process);
+
+  
   DEBUG1("context created (s_process=%p)",process->simdata->s_process);
 
 
@@ -126,29 +131,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 
@@ -203,7 +188,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)
@@ -243,12 +228,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));
 }
 
 
@@ -287,7 +272,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"));
@@ -488,7 +473,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 +507,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 
@@ -609,7 +595,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 
@@ -626,7 +612,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 
@@ -797,7 +783,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 +931,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 +954,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 +1049,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 +1061,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 +1098,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);
+
+  xbt_context_stop(0);
 }
 
 JNIEXPORT void JNICALL 
@@ -1220,4 +1172,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"));       
+}