Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deprecate Msg.clean()
[simgrid.git] / src / jmsg.c
index aaf5b10..97ba952 100644 (file)
@@ -1,6 +1,6 @@
 /* Java Wrappers to the MSG API.                                            */
 
-/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2007-2012. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -12,6 +12,7 @@
 #include <locale.h>
 
 #include "smx_context_java.h"
+#include "smx_context_cojava.h"
 
 #include "jmsg_process.h"
 
@@ -49,20 +50,20 @@ JNIEnv *get_current_thread_env(void)
   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"));
-       }
+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"));
+  }
 }
 
 
@@ -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_INFO("Using Coroutines. Your simulation is on steroid.");
+       smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
+  }
+  else {
+       XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
+       smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
+  }
+  jthrowable exc = (*env)->ExceptionOccurred(env);
+  if (exc) {
+    (*env)->ExceptionClear(env);
+  }
 
   setlocale(LC_NUMERIC,"C");
 
@@ -106,7 +118,7 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
   }
   argv[argc] = NULL;
 
-  MSG_global_init(&argc, argv);
+  MSG_init(&argc, argv);
 
   for (index = 0; index < argc; index++)
     free(argv[index]);
@@ -117,7 +129,7 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
 JNIEXPORT void JNICALL
     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
 {
-  MSG_error_t rv;
+  msg_error_t rv;
   int index;
   xbt_dynar_t hosts;
   jobject jhost;
@@ -136,7 +148,7 @@ JNIEXPORT void JNICALL
   /* Cleanup java hosts */
   hosts = MSG_hosts_as_dynar();
   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
-    jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
+    jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,msg_host_t));
     if (jhost)
       jhost_unref(env, jhost);
 
@@ -144,15 +156,6 @@ JNIEXPORT void JNICALL
   xbt_dynar_free(&hosts);
   XBT_INFO("Clean native world");
 }
-JNIEXPORT void JNICALL
-    JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
-{
-  /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
-  MSG_error_t rv = MSG_OK != MSG_clean();
-  jxbt_check_res("MSG_clean()", rv, MSG_OK,
-                 bprintf
-                 ("unexpected error : MSG_clean() failed .. please report this bug "));
-}
 
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
@@ -174,13 +177,40 @@ Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
   (*env)->ReleaseStringUTFChars(env, js, s);
 }
 JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
+{
+  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  XBT_VERB("%s", s);
+  (*env)->ReleaseStringUTFChars(env, js, s);
+}
+JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
 {
   const char *s = (*env)->GetStringUTFChars(env, js, 0);
   XBT_INFO("%s", s);
   (*env)->ReleaseStringUTFChars(env, js, s);
 }
-
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
+{
+  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  XBT_WARN("%s", s);
+  (*env)->ReleaseStringUTFChars(env, js, s);
+}
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
+{
+  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  XBT_ERROR("%s", s);
+  (*env)->ReleaseStringUTFChars(env, js, s);
+}
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
+{
+  const char *s = (*env)->GetStringUTFChars(env, js, 0);
+  XBT_CRITICAL("%s", s);
+  (*env)->ReleaseStringUTFChars(env, js, s);
+}
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
                                        jstring jdeploymentFile)
@@ -198,42 +228,41 @@ Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
  * it HAS to be executed on the process context, else really bad things will happen.
  */
 static int create_jprocess(int argc, char *argv[]) {
-       JNIEnv *env = get_current_thread_env();
-       //Change the "." in class name for "/".
-       xbt_str_subst(argv[0],'.','/',0);
-       jclass class_Process = (*env)->FindClass(env, argv[0]);
-       xbt_str_subst(argv[0],'/','.',0);
-       //Retrieve the methodID for the constructor
-       xbt_assert((class_Process != NULL), "Class not found.");
-       jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
-       xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
-
-       //Retrieve the name of the process.
-       jstring jname = (*env)->NewStringUTF(env, argv[0]);
-       //Build the arguments
-       jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
-       (*env)->FindClass(env,"java/lang/String"),
-       (*env)->NewStringUTF(env,"")));
-       int i;
-       for (i = 1; i < argc; i++)
-               (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
-       //Retrieve the host for the process.
-       jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
-       jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
-       //creates the process
-       jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
-       xbt_assert((jprocess != NULL), "Process allocation failed.");
-       jprocess = (*env)->NewGlobalRef(env, jprocess);
-       //bind the process to the context
-       m_process_t process = MSG_process_self();
-       smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
-       context->jprocess = jprocess;
-  /* 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;
+  JNIEnv *env = get_current_thread_env();
+  //Change the "." in class name for "/".
+  xbt_str_subst(argv[0],'.','/',0);
+  jclass class_Process = (*env)->FindClass(env, argv[0]);
+  xbt_str_subst(argv[0],'/','.',0);
+  //Retrieve the methodID for the constructor
+  xbt_assert((class_Process != NULL), "Class not found.");
+  jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
+  xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
+
+  //Retrieve the name of the process.
+  jstring jname = (*env)->NewStringUTF(env, argv[0]);
+  //Build the arguments
+  jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
+  (*env)->FindClass(env,"java/lang/String"),
+  (*env)->NewStringUTF(env,"")));
+  int i;
+  for (i = 1; i < argc; i++)
+      (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
+  //Retrieve the host for the process.
+  jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
+  jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
+  //creates the process
+  jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
+  xbt_assert((jprocess != NULL), "Process allocation failed.");
+  jprocess = (*env)->NewGlobalRef(env, jprocess);
+  //bind the process to the context
+  msg_process_t process = MSG_process_self();
+  smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
+  context->jprocess = jprocess;
+/* 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;
 }