Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add EXPERIMENTAL Coroutines support
authorSamuel Lepetit <samuel.lepetit@inria.fr>
Mon, 11 Jun 2012 21:29:19 +0000 (23:29 +0200)
committerSamuel Lepetit <samuel.lepetit@inria.fr>
Mon, 11 Jun 2012 21:29:19 +0000 (23:29 +0200)
CMakeLists.txt
org/simgrid/msg/Process.java
org/simgrid/msg/RngStream.java
src/jmsg.c
src/jmsg_process.c
src/jmsg_process.h
src/jmsg_task.c
src/smx_context_java.h

index 94aef31..4865ed3 100644 (file)
@@ -72,6 +72,8 @@ include(FindJavaSG.cmake)
 # Declare our package content #
 ###############################
 set(JMSG_C_SRC
 # Declare our package content #
 ###############################
 set(JMSG_C_SRC
+       src/smx_context_cojava.h
+       src/smx_context_cojava.c
        src/smx_context_java.h
        src/smx_context_java.c
        src/jxbt_utilities.c
        src/smx_context_java.h
        src/smx_context_java.c
        src/jxbt_utilities.c
index 82e6c4f..f8c1061 100644 (file)
@@ -371,6 +371,7 @@ public abstract class Process implements Runnable {
                 catch(ProcessKilledError pk) {
                         
                 }      
                 catch(ProcessKilledError pk) {
                         
                 }      
+               exit();
        }
 
        /**
        }
 
        /**
@@ -381,7 +382,7 @@ public abstract class Process implements Runnable {
      */
        public abstract void main(String[]args) throws MsgException;
 
      */
        public abstract void main(String[]args) throws MsgException;
 
-    
+       public native void exit();    
        /**
         * Class initializer, to initialize various JNI stuff
         */
        /**
         * Class initializer, to initialize various JNI stuff
         */
index b1b0c9d..5f4a8bf 100644 (file)
@@ -76,7 +76,7 @@ public class RngStream {
         */
        public native void resetNextSubstream();
        /**
         */
        public native void resetNextSubstream();
        /**
-        * If a = true the stream g will start generating antithetic variates, i.e., 1  U instead of U , until
+        * If a = true the stream g will start generating antithetic variates, i.e., 1 - U instead of U , until
         *  this method is called again with a = false.
         */
        public native void setAntithetic(boolean a);
         *  this method is called again with a = false.
         */
        public native void setAntithetic(boolean a);
index aaf5b10..92d5d97 100644 (file)
@@ -12,6 +12,7 @@
 #include <locale.h>
 
 #include "smx_context_java.h"
 #include <locale.h>
 
 #include "smx_context_java.h"
+#include "smx_context_cojava.h"
 
 #include "jmsg_process.h"
 
 
 #include "jmsg_process.h"
 
@@ -87,7 +88,18 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
 
   (*env)->GetJavaVM(env, &__java_vm);
 
 
   (*env)->GetJavaVM(env, &__java_vm);
 
-  smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
+  if ((*env)->FindClass(env, "java/dyn/Coroutine")) {
+       XBT_VERB("Using Coroutines");
+       smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
+  }
+  else {
+       XBT_VERB("Using java threads");
+       smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
+  }
+  jthrowable exc = (*env)->ExceptionOccurred(env);
+  if (exc) {
+    (*env)->ExceptionClear(env);
+  }
 
   setlocale(LC_NUMERIC,"C");
 
 
   setlocale(LC_NUMERIC,"C");
 
@@ -231,7 +243,6 @@ static int create_jprocess(int argc, char *argv[]) {
   /* sets the PID and the PPID of the process */
   (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
   (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
   /* sets the PID and the PPID of the process */
   (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
   (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
-
        jprocess_bind(jprocess, process, env);
 
        return 0;
        jprocess_bind(jprocess, process, env);
 
        return 0;
index 6b2284d..1fa964c 100644 (file)
 #include "jmsg_host.h"
 #include "jxbt_utilities.h"
 #include "smx_context_java.h"
 #include "jmsg_host.h"
 #include "jxbt_utilities.h"
 #include "smx_context_java.h"
+#include "smx_context_cojava.h"
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Process_exit(JNIEnv *env, jobject jprocess) {
+       if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init) {
+                m_process_t process = jprocess_to_native_process(jprocess, env);
+                smx_context_t context = MSG_process_get_smx_ctx(process);
+                smx_ctx_cojava_stop(context);
+        }
+}
+
 jobject native_to_java_process(m_process_t process)
 {
   return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
 jobject native_to_java_process(m_process_t process)
 {
   return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
@@ -325,3 +335,6 @@ Java_org_simgrid_msg_Process_migrate(JNIEnv * env,
   /* change the host java side */
   (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
 }
   /* change the host java side */
   (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
 }
+
+
+
index a97c0dd..3b87be6 100644 (file)
@@ -22,6 +22,9 @@ jfieldID jprocess_field_Process_name;
 jfieldID jprocess_field_Process_pid;
 jfieldID jprocess_field_Process_ppid;
 
 jfieldID jprocess_field_Process_pid;
 jfieldID jprocess_field_Process_ppid;
 
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Process_exit(JNIEnv *env, jobject);
+
 
 jobject native_to_java_process(m_process_t process);
 
 
 jobject native_to_java_process(m_process_t process);
 
@@ -229,4 +232,5 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill
  */
 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate
     (JNIEnv *, jobject, jobject);
  */
 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate
     (JNIEnv *, jobject, jobject);
+
 #endif                          /* !MSG_JPROCESS_H */
 #endif                          /* !MSG_JPROCESS_H */
index 9eb973c..b758464 100644 (file)
@@ -56,7 +56,7 @@ Java_org_simgrid_msg_Task_nativeInit(JNIEnv *env, jclass cls) {
        if (!jtask_field_Task_bind || !jtask_class_Task || !jtask_field_Comm_bind || !jtask_field_Comm_taskBind ||
                  !jtask_field_Comm_receiving || !jtask_method_Comm_constructor) {
                        jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
        if (!jtask_field_Task_bind || !jtask_class_Task || !jtask_field_Comm_bind || !jtask_field_Comm_taskBind ||
                  !jtask_field_Comm_receiving || !jtask_method_Comm_constructor) {
                        jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
-                 }
+       }
 }
 
 JNIEXPORT void JNICALL
 }
 
 JNIEXPORT void JNICALL
@@ -410,7 +410,9 @@ Java_org_simgrid_msg_Task_receive(JNIEnv * env, jclass cls,
                                        jobject jhost)
 {
   MSG_error_t rv;
                                        jobject jhost)
 {
   MSG_error_t rv;
-  m_task_t task = NULL;
+  m_task_t *task = xbt_new(m_task_t,1);
+  *task = NULL;
+
   m_host_t host = NULL;
   jobject jtask_global, jtask_local;
   const char *alias;
   m_host_t host = NULL;
   jobject jtask_global, jtask_local;
   const char *alias;
@@ -426,7 +428,7 @@ Java_org_simgrid_msg_Task_receive(JNIEnv * env, jclass cls,
 
   alias = (*env)->GetStringUTFChars(env, jalias, 0);
   TRY {
 
   alias = (*env)->GetStringUTFChars(env, jalias, 0);
   TRY {
-       rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
+       rv = MSG_task_receive_ext(task, alias, (double) jtimeout, host);
   }
   CATCH_ANONYMOUS {
        return NULL;
   }
   CATCH_ANONYMOUS {
        return NULL;
@@ -435,15 +437,17 @@ Java_org_simgrid_msg_Task_receive(JNIEnv * env, jclass cls,
        jmsg_throw_status(env,rv);
        return NULL;
   }
        jmsg_throw_status(env,rv);
        return NULL;
   }
-  jtask_global = MSG_task_get_data(task);
+  jtask_global = MSG_task_get_data(*task);
 
   /* Convert the global ref into a local ref so that the JVM can free the stuff */
   jtask_local = (*env)->NewLocalRef(env, jtask_global);
   (*env)->DeleteGlobalRef(env, jtask_global);
 
   /* Convert the global ref into a local ref so that the JVM can free the stuff */
   jtask_local = (*env)->NewLocalRef(env, jtask_global);
   (*env)->DeleteGlobalRef(env, jtask_global);
-  MSG_task_set_data(task, NULL);
+  MSG_task_set_data(*task, NULL);
 
   (*env)->ReleaseStringUTFChars(env, jalias, alias);
 
 
   (*env)->ReleaseStringUTFChars(env, jalias, alias);
 
+  xbt_free(task);
+
   jxbt_check_res("MSG_task_receive_ext()", rv,
                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
                  bprintf("while receiving from mailbox %s", alias));
   jxbt_check_res("MSG_task_receive_ext()", rv,
                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
                  bprintf("while receiving from mailbox %s", alias));
index 4142bf1..084c5d4 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2009, 2010, 2012. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it