Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix the mess around task references: the JVM should now be able to free the tasks...
[simgrid.git] / src / java / jmsg.c
index c1d65df..be1987c 100644 (file)
@@ -1,14 +1,11 @@
-/*
- * $Id$
- *
- * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved. 
- *
- * This program is free software; you can redistribute it and/or modify it 
- * under the terms of the license (GNU LGPL) which comes with this package.
- *
- * This contains the implementation of the wrapper functions used to interface
- * the java object with the native functions of the MSG API.
- */
+/* Java Wrappers to the MSG API.                                            */
+
+/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+  * under the terms of the license (GNU LGPL) which comes with this package. */
+
 #include "msg/msg.h"
 #include "msg/private.h"
 #include "simix/private.h"
 #include "jmsg_application_handler.h"
 #include "jxbt_utilities.h"
 
-
 #include "jmsg.h"
-
 #include "msg/mailbox.h"
-
 #include "surf/surfxml_parse.h"
 
-
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
 static JavaVM *__java_vm = NULL;
@@ -54,7 +47,6 @@ static jobject native_to_java_process(m_process_t process)
           (process->simdata->s_process->context))->jprocess;
 }
 
-
 /*
  * The MSG process connected functions implementation.                                 
  */
@@ -163,8 +155,11 @@ Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
   }
 
   /* try to suspend the process */
-  if (MSG_OK != MSG_process_suspend(process))
-    jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
+  MSG_error_t rv = MSG_process_suspend(process);
+  
+  jxbt_check_res("MSG_process_suspend()",rv,MSG_OK,
+    bprintf("unexpected error , please report this bug"));
+  
 }
 
 JNIEXPORT void JNICALL
@@ -174,13 +169,15 @@ Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
   m_process_t process = jprocess_to_native_process(jprocess, env);
 
   if (!process) {
-    jxbt_throw_notbound(env, "process", jprocess);
+    jxbt_throw_notbound(env,"process", jprocess);
     return;
   }
 
   /* try to resume the process */
-  if (MSG_OK != MSG_process_resume(process))
-    jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
+    MSG_error_t rv = MSG_process_resume(process);
+    
+    jxbt_check_res("MSG_process_resume()",rv,MSG_OK,
+    bprintf("unexpected error , please report this bug"));
 }
 
 JNIEXPORT jboolean JNICALL
@@ -235,7 +232,7 @@ Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
   host = MSG_process_get_host(process);
 
   if (!host->data) {
-    jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
+    jxbt_throw_jni(env, "MSG_process_get_host() failed");
     return NULL;
   }
 
@@ -255,7 +252,7 @@ Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
   }
 
   if (!native_to_java_process(process)) {
-    jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
+    jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
     return NULL;
   }
 
@@ -299,32 +296,18 @@ Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
   jobject jprocess;
 
   if (!process) {
-    jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
+    jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
     return NULL;
   }
 
   jprocess = native_to_java_process(process);
 
   if (!jprocess)
-    jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
+    jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
 
   return jprocess;
 }
 
-
-JNIEXPORT jint JNICALL
-Java_simgrid_msg_MsgNative_processSelfPID(JNIEnv * env, jclass cls)
-{
-  return (jint) MSG_process_self_PID();
-}
-
-
-JNIEXPORT jint JNICALL
-Java_simgrid_msg_MsgNative_processSelfPPID(JNIEnv * env, jclass cls)
-{
-  return (jint) MSG_process_self_PPID();
-}
-
 JNIEXPORT void JNICALL
 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
                                              jobject jhost)
@@ -337,18 +320,22 @@ Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
   }
 
   /* try to change the host of the process */
-  if (MSG_OK != MSG_process_change_host(host))
-    jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
+  MSG_error_t rv = MSG_process_change_host(host);
+
+  jxbt_check_res("MSG_process_change_host()",rv,MSG_OK,
+      bprintf("unexpected error , please report this bug"));
+
 }
 
 JNIEXPORT void JNICALL
 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
                                           jdouble seconds)
 {
-  if (MSG_OK != MSG_process_sleep((double) seconds))
-    jxbt_throw_native(env,
-                      bprintf("MSG_process_change_host(%f) failed",
-                              (double) seconds));
+    MSG_error_t rv= MSG_process_sleep((double) seconds);
+    
+    jxbt_check_res("MSG_process_sleep()",rv, MSG_HOST_FAILURE,
+    bprintf("while process was waiting for %f seconds",(double)seconds));
+    
 }
 
 
@@ -518,7 +505,7 @@ Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
                                       jdouble jmessageSize)
 {
   m_task_t task;                /* the native task to create                            */
-  const char *name;             /* the name of the task                                 */
+  const char *name=NULL;        /* the name of the task                                 */
 
   if (jcomputeDuration < 0) {
     jxbt_throw_illegal(env,
@@ -534,30 +521,23 @@ Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
     return;
   }
 
-  if (!jname) {
-    jxbt_throw_null(env, xbt_strdup("Task name cannot be null"));
-    return;
+  if (jname) {
+    /* get the C string from the java string */
+    name = (*env)->GetStringUTFChars(env, jname, 0);
   }
 
-  /* get the C string from the java string */
-  name = (*env)->GetStringUTFChars(env, jname, 0);
 
   /* create the task */
   task =
     MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
                     NULL);
 
-  (*env)->ReleaseStringUTFChars(env, jname, name);
+  if (jname)
+    (*env)->ReleaseStringUTFChars(env, jname, name);
 
   /* bind & store the task */
   jtask_bind(jtask, task, env);
-
-  /* allocate a new global reference to the java task instance */
-  task->data = (void *) jtask_new_global_ref(jtask, env);
-
-  if (!task->data)
-    jxbt_throw_jni(env, "global ref allocation failed");
-
+  task->data = jtask;
 }
 
 JNIEXPORT void JNICALL
@@ -676,7 +656,7 @@ Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
   host = MSG_task_get_source(task);
 
   if (!host->data) {
-    jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
+    jxbt_throw_jni(env, "MSG_task_get_source() failed");
     return NULL;
   }
 
@@ -708,8 +688,10 @@ Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
     return;
   }
 
-  if (MSG_OK != MSG_task_cancel(ptask))
-    jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
+  MSG_error_t rv = MSG_task_cancel(ptask);
+  
+    jxbt_check_res("MSG_task_cancel()",rv,MSG_OK,
+    bprintf("unexpected error , please report this bug"));
 }
 
 JNIEXPORT jdouble JNICALL
@@ -765,12 +747,11 @@ Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
     return;
   }
   jtask = (jobject) task->data;
-
-  if (MSG_OK != MSG_task_destroy(task))
-    jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
-
-  /* delete the global reference to the java task object */
-  jtask_delete_global_ref(jtask, env);
+    
+  MSG_error_t rv = MSG_task_destroy(task);
+    
+  jxbt_check_res("MSG_task_destroy()",rv,MSG_OK,
+    bprintf("unexpected error , please report this bug"));
 }
 
 JNIEXPORT void JNICALL
@@ -784,8 +765,10 @@ Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
     return;
   }
 
-  if (MSG_OK != MSG_task_execute(task))
-    jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
+  MSG_error_t rv = MSG_task_execute(task);
+  
+    jxbt_check_res("MSG_task_execute()",rv,MSG_HOST_FAILURE|MSG_TASK_CANCELLED,
+    bprintf("while executing task %s", MSG_task_get_name(task)));
 }
 
 /***************************************************************************************
@@ -841,31 +824,36 @@ Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) {
 
 JNIEXPORT void JNICALL
   JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) {
-  xbt_fifo_item_t item = NULL;
-  m_host_t host = NULL;
+  MSG_error_t rv;
+  int index;//xbt_fifo_item_t item = NULL;
+  m_host_t *hosts;
   jobject jhost;
 
   /* Run everything */
-  if (MSG_OK != MSG_main()) {
-    jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
-  }
+  rv= MSG_main();
+    jxbt_check_res("MSG_main()",rv,MSG_OK,
+     bprintf("unexpected error : MSG_main() failed .. please report this bug "));
+
   DEBUG0
     ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
 
   DEBUG0("Clean java world");
   /* Cleanup java hosts */
-  xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
-    jhost = (jobject) host->data;
+  hosts = MSG_get_host_table();
+  for (index=0;index<MSG_get_host_number()-1;index++)
+  {
+    jhost = (jobject)hosts[index]->data;
+    if(jhost)
+       jhost_unref(env,jhost);
 
-    if (jhost)
-      jhost_unref(env, jhost);
   }
 
   DEBUG0("Clean native world");
   /* cleanup native stuff */
-  if (MSG_OK != MSG_clean()){
-    jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
-  }
+  rv = MSG_OK != MSG_clean();
+  jxbt_check_res("MSG_clean()",rv,MSG_OK,
+       bprintf("unexpected error : MSG_clean() failed .. please report this bug "));
+
 }
 
 JNIEXPORT jint JNICALL
@@ -919,8 +907,8 @@ Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
   jstring jname;
   m_host_t host;
 
-  int count = xbt_fifo_size(msg_global->host);
-  m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
+  int count = MSG_get_host_number();
+  m_host_t *table = MSG_get_host_table();
 
   jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
 
@@ -975,7 +963,7 @@ Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
   if (errmsg) {
     char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
     free(errmsg);
-    jxbt_throw_native(env, thrown);
+    jxbt_throw_jni(env, thrown);
   }
 }
 
@@ -997,13 +985,14 @@ Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
     return;
   }
 
+  /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
+  task->data = (void *) (*env)->NewGlobalRef(env, jtask);
   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
 
   (*env)->ReleaseStringUTFChars(env, jalias, alias);
 
-  if (MSG_OK != rv)
-    jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
-
+  jxbt_check_res("MSG_task_send_with_timeout()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT,
+    bprintf("while sending task %s to mailbox %s", MSG_task_get_name(task),alias));
 }
 
 JNIEXPORT void JNICALL
@@ -1022,12 +1011,15 @@ Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
 
   alias = (*env)->GetStringUTFChars(env, jalias, 0);
 
+  /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
+  task->data = (void *) (*env)->NewGlobalRef(env, jtask);
   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
 
   (*env)->ReleaseStringUTFChars(env, jalias, alias);
 
-  if (MSG_OK != rv)
-    jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
+  jxbt_check_res("MSG_task_send_bounded()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT,
+    bprintf("while sending task %s to mailbox %s with max rate %f", MSG_task_get_name(task),alias,(double)jmaxRate));
+    
 }
 
 JNIEXPORT jobject JNICALL
@@ -1038,6 +1030,7 @@ Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
   MSG_error_t rv;
   m_task_t task = NULL;
   m_host_t host = NULL;
+  jobject jtask_global, jtask_local;
   const char *alias;
 
   if (jhost) {
@@ -1052,15 +1045,19 @@ Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
   alias = (*env)->GetStringUTFChars(env, jalias, 0);
 
   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
+  jtask_global = task->data;
+
+  /* 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);
+  task->data = NULL;
 
   (*env)->ReleaseStringUTFChars(env, jalias, alias);
 
-  if (MSG_OK != rv) {
-    jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
-    return NULL;
-  }
+  jxbt_check_res("MSG_task_receive_ext()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT,
+    bprintf("while receiving from mailbox %s",alias));
 
-  return (jobject) task->data;
+  return (jobject) jtask_local;
 }
 
 JNIEXPORT jboolean JNICALL
@@ -1084,7 +1081,6 @@ JNIEXPORT jint JNICALL
 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
                                               jstring jalias, jobject jhost)
 {
-
   int rv;
   const char *alias;
 
@@ -1094,7 +1090,6 @@ Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
     jxbt_throw_notbound(env, "host", jhost);
     return -1;
   }
-
   alias = (*env)->GetStringUTFChars(env, jalias, 0);
 
   rv = MSG_task_listen_from_host(alias, host);
@@ -1146,7 +1141,7 @@ Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
   japplication_handler_on_start_document();
 
   if (surf_parse())
-    jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
+    jxbt_throw_jni(env,"surf_parse() failed");
 
   surf_parse_close();