From: Samuel Lepetit Date: Fri, 4 May 2012 11:57:55 +0000 (+0200) Subject: Add waitCompletion to Comm as a binding of MSG_comm_wait X-Git-Tag: v3_9_90~569^2~19^2~108 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/b7233718def37e1bc1e2a469ac45c738fab0c582?ds=sidebyside Add waitCompletion to Comm as a binding of MSG_comm_wait --- diff --git a/org/simgrid/msg/Comm.java b/org/simgrid/msg/Comm.java index 0aae5c484d..899d09498c 100644 --- a/org/simgrid/msg/Comm.java +++ b/org/simgrid/msg/Comm.java @@ -53,6 +53,11 @@ public class Comm { * 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. diff --git a/src/jmsg_comm.c b/src/jmsg_comm.c index 4bdbf41e06..3ebc04e449 100644 --- a/src/jmsg_comm.c +++ b/src/jmsg_comm.c @@ -5,7 +5,52 @@ #include "jxbt_utilities.h" #include 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"); @@ -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 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; } @@ -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 (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 - 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; } } @@ -93,3 +104,32 @@ Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) { 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); + } + + +} diff --git a/src/jmsg_comm.h b/src/jmsg_comm.h index 1d5a3427b5..31c033bf0b 100644 --- a/src/jmsg_comm.h +++ b/src/jmsg_comm.h @@ -5,10 +5,15 @@ #ifndef MSG_JCOMM_H #define MSG_JCOMM_H #include - +#include +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_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout); #endif /* MSG_JCOMM_H */