Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add waitCompletion to Comm as a binding of MSG_comm_wait
authorSamuel Lepetit <samuel.lepetit@inria.fr>
Fri, 4 May 2012 11:57:55 +0000 (13:57 +0200)
committerSamuel Lepetit <samuel.lepetit@inria.fr>
Fri, 4 May 2012 11:57:55 +0000 (13:57 +0200)
org/simgrid/msg/Comm.java
src/jmsg_comm.c
src/jmsg_comm.h

index 0aae5c4..899d094 100644 (file)
@@ -53,6 +53,11 @@ public class Comm {
         * raise an exception.
         */
        public native boolean test() throws TransferFailureException, HostFailureException, TimeoutException ;
         * raise an exception.
         */
        public native boolean test() throws TransferFailureException, HostFailureException, TimeoutException ;
+       /**
+        * Wait for the completion of the communication.
+        * Throws an exception if there were an error in the communication.
+        */
+       public native void waitCompletion(double timeout) throws TransferFailureException, HostFailureException, TimeoutException;
        /**
         * Returns the task associated with the communication.
         * if the communication isn't finished yet, will return null.
        /**
         * Returns the task associated with the communication.
         * if the communication isn't finished yet, will return null.
index 4bdbf41..3ebc04e 100644 (file)
@@ -5,7 +5,52 @@
 #include "jxbt_utilities.h"
 #include <msg/msg.h>
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 #include "jxbt_utilities.h"
 #include <msg/msg.h>
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
+void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
+       jfieldID id_receiving = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "receiving", "Z");
+       jclass jclass = jxbt_get_class(env,"org/simgrid/msg/Comm");
+       jfieldID id_task = jxbt_get_jfield(env, jclass, "task", "Lorg/simgrid/msg/Task;");
+       jfieldID id_comm = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
+
+       msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, id_comm);
+
+       //test if we are receiving or sending a task.
+       jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, id_receiving);
+       if (jreceiving == JNI_TRUE) {
+               //bind the task object.
+               m_task_t task = MSG_comm_get_task(comm);
+               xbt_assert(task != NULL, "Task is NULL");
+               jobject jtask_global = MSG_task_get_data(task);
+               //case where the data has already been retrieved
+               if (jtask_global == NULL)
+               {
+                       return;
+               }
+
+               //Make sure the data will be correctly gc.
+               jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
+               (*env)->DeleteGlobalRef(env, jtask_global);
+
+               (*env)->SetObjectField(env, jcomm, id_task, jtask_local);
+
+               MSG_task_set_data(task, NULL);
+       }
 
 
+}
+void jcomm_throw(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"));
+       }
+}
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Comm_unbind(JNIEnv *env, jobject jcomm) {
        jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Comm_unbind(JNIEnv *env, jobject jcomm) {
        jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
@@ -31,11 +76,10 @@ Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
        jfieldID idComm = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
        jclass jclass = jxbt_get_class(env,"org/simgrid/msg/Comm");
        jfieldID idTask = jxbt_get_jfield(env, jclass, "task", "Lorg/simgrid/msg/Task;");
        jfieldID idComm = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
        jclass jclass = jxbt_get_class(env,"org/simgrid/msg/Comm");
        jfieldID idTask = jxbt_get_jfield(env, jclass, "task", "Lorg/simgrid/msg/Task;");
-       jfieldID id_receiving = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "receiving", "Z");
 
 
 
 
-       if (!idComm || !idTask || !id_receiving) {
-               jxbt_throw_native(env,bprintf("idTask or idComm is null"));
+       if (!jclass || !idComm || !idTask) {
+               jxbt_throw_native(env,bprintf("idTask/idComm/jclass/idComm is null"));
                return JNI_FALSE;
        }
 
                return JNI_FALSE;
        }
 
@@ -46,46 +90,13 @@ Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
        }
        if (MSG_comm_test(comm)) {
                MSG_error_t status = MSG_comm_get_status(comm);
        }
        if (MSG_comm_test(comm)) {
                MSG_error_t status = MSG_comm_get_status(comm);
-
                if (status == MSG_OK) {
                if (status == MSG_OK) {
-                       //test if we are receiving or sending a task.
-                       jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, id_receiving);
-                       if (jreceiving == JNI_TRUE) {
-                               //bind the task object.
-                               m_task_t task = MSG_comm_get_task(comm);
-                               xbt_assert(task != NULL, "Task is NULL");
-                               jobject jtask_global = MSG_task_get_data(task);
-                               //case where the data has already been retrieved
-                               if (jtask_global == NULL)
-                               {
-                                       return JNI_TRUE;
-                               }
-
-                               //Make sure the data will be correctly gc.
-                               jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
-                               (*env)->DeleteGlobalRef(env, jtask_global);
-
-                               (*env)->SetObjectField(env, jcomm, idTask, jtask_local);
-
-                               MSG_task_set_data(task, NULL);
-                       }
+                       jcomm_bind_task(env,jcomm);
                        return JNI_TRUE;
                }
                else {
                        //send the correct exception
                        return JNI_TRUE;
                }
                else {
                        //send the correct exception
-                       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("receive failed"));
-                       }
+                       jcomm_throw(env,status);
                        return JNI_FALSE;
                }
        }
                        return JNI_FALSE;
                }
        }
@@ -93,3 +104,32 @@ Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
                return JNI_FALSE;
        }
 }
                return JNI_FALSE;
        }
 }
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
+       jclass jclass = jxbt_get_class(env,"org/simgrid/msg/Comm");
+       jfieldID id_comm = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
+       jfieldID id_task = jxbt_get_jfield(env, jclass, "task", "Lorg/simgrid/msg/Task;");
+       jfieldID id_receiving = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "receiving", "Z");
+
+       if (!jclass || !id_comm || !id_task || !id_receiving) {
+               jxbt_throw_native(env,bprintf("idTask/idComm/jclass/idComm is null"));
+               return;
+       }
+
+       msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, id_comm);
+       if (!comm) {
+               jxbt_throw_native(env,bprintf("comm is null"));
+               return;
+       }
+
+       MSG_error_t status = MSG_comm_wait(comm,(double)timeout);
+       if (status == MSG_OK) {
+               jcomm_bind_task(env,jcomm);
+               return;
+       }
+       else {
+               jcomm_throw(env,status);
+       }
+
+
+}
index 1d5a342..31c033b 100644 (file)
@@ -5,10 +5,15 @@
 #ifndef MSG_JCOMM_H
 #define MSG_JCOMM_H
 #include <jni.h>
 #ifndef MSG_JCOMM_H
 #define MSG_JCOMM_H
 #include <jni.h>
-
+#include <msg/msg.h>
+void jcomm_bind_task(JNIEnv *env, jobject jcomm);
+void jcomm_throw(JNIEnv *env, MSG_error_t status);
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Comm_unbind(JNIEnv *env, jobject jcomm);
 
 JNIEXPORT jboolean JNICALL
 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm);
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Comm_unbind(JNIEnv *env, jobject jcomm);
 
 JNIEXPORT jboolean JNICALL
 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm);
+
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout);
 #endif /* MSG_JCOMM_H */
 #endif /* MSG_JCOMM_H */