Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge scm.gforge.inria.fr:/gitroot/simgrid/simgrid-java
authorSamuel Lepetit <samuel.lepetit@inria.fr>
Tue, 12 Jun 2012 08:36:57 +0000 (10:36 +0200)
committerSamuel Lepetit <samuel.lepetit@inria.fr>
Tue, 12 Jun 2012 08:36:57 +0000 (10:36 +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 12a9255..55a5fad 100644 (file)
@@ -72,6 +72,8 @@ include(FindJavaSG.cmake)
 # 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
index 375a142..f9572c5 100644 (file)
@@ -377,6 +377,7 @@ public abstract class Process implements Runnable {
                 catch(ProcessKilledError pk) {
                         
                 }      
+               exit();
        }
 
        /**
@@ -387,7 +388,7 @@ public abstract class Process implements Runnable {
      */
        public abstract void main(String[]args) throws MsgException;
 
-    
+       public native void exit();    
        /**
         * Class initializer, to initialize various JNI stuff
         */
index b1b0c9d..5f4a8bf 100644 (file)
@@ -76,7 +76,7 @@ public class RngStream {
         */
        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);
index aaf5b10..92d5d97 100644 (file)
@@ -12,6 +12,7 @@
 #include <locale.h>
 
 #include "smx_context_java.h"
+#include "smx_context_cojava.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);
 
-  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");
 
@@ -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));
-
        jprocess_bind(jprocess, process, env);
 
        return 0;
index 49626af..78c88dc 100644 (file)
 #include "jmsg_host.h"
 #include "jxbt_utilities.h"
 #include "smx_context_java.h"
+#include "smx_context_cojava.h"
 
 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;
index 0df3796..3866ad9 100644 (file)
@@ -22,6 +22,9 @@ jfieldID jprocess_field_Process_name;
 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);
 
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."));
-                 }
+       }
 }
 
 JNIEXPORT void JNICALL
@@ -410,7 +410,9 @@ Java_org_simgrid_msg_Task_receive(JNIEnv * env, jclass cls,
                                        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;
@@ -426,7 +428,7 @@ Java_org_simgrid_msg_Task_receive(JNIEnv * env, jclass cls,
 
   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;
@@ -435,15 +437,17 @@ Java_org_simgrid_msg_Task_receive(JNIEnv * env, jclass cls,
        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);
-  MSG_task_set_data(task, NULL);
+  MSG_task_set_data(*task, NULL);
 
   (*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));
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