Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge scm.gforge.inria.fr:/gitroot/simgrid/simgrid-java
[simgrid.git] / src / jmsg.c
index 1e02578..92d5d97 100644 (file)
@@ -12,6 +12,7 @@
 #include <locale.h>
 
 #include "smx_context_java.h"
+#include "smx_context_cojava.h"
 
 #include "jmsg_process.h"
 
@@ -34,8 +35,7 @@ static int create_jprocess(int argc, char *argv[]);
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
-static JavaVM *__java_vm = NULL;
-
+JavaVM *__java_vm = NULL;
 
 JavaVM *get_java_VM(void)
 {
@@ -49,6 +49,24 @@ JNIEnv *get_current_thread_env(void)
   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
   return env;
 }
+
+void jmsg_throw_status(JNIEnv *env, MSG_error_t status) {
+       switch (status) {
+               case MSG_TIMEOUT:
+                       jxbt_throw_time_out_failure(env,NULL);
+               break;
+               case MSG_TRANSFER_FAILURE:
+                       jxbt_throw_transfer_failure(env,NULL);
+               break;
+               case MSG_HOST_FAILURE:
+                       jxbt_throw_host_failure(env,NULL);
+               break;
+               default:
+                       jxbt_throw_native(env,bprintf("communication failed"));
+       }
+}
+
+
 /***************************************************************************************
  * Unsortable functions                                                        *
  ***************************************************************************************/
@@ -68,7 +86,20 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
   jstring jval;
   const char *tmp;
 
-  smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
+  (*env)->GetJavaVM(env, &__java_vm);
+
+  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");
 
@@ -93,8 +124,6 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
     free(argv[index]);
 
   free(argv);
-
-  (*env)->GetJavaVM(env, &__java_vm);
 }
 
 JNIEXPORT void JNICALL
@@ -149,7 +178,13 @@ Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
 
   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
 }
-
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
+{
+  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  XBT_DEBUG("%s", s);
+  (*env)->ReleaseStringUTFChars(env, js, s);
+}
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
 {
@@ -208,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;