+
+static msg_comm_t* jarray_to_commArray(JNIEnv *env, jobjectArray jcomms, /* OUT */ int *count)
+{
+ *count = env->GetArrayLength(jcomms);
+ msg_comm_t* comms = xbt_new(msg_comm_t, *count);
+
+ for (int i=0; i < *count; i++) {
+ jobject jcomm = env->GetObjectArrayElement(jcomms, i);
+ if (env->ExceptionOccurred())
+ break;
+
+ comms[i] = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
+ if (not comms[i]) {
+ jxbt_throw_null(env, std::string("comm at rank ") + std::to_string(i) + " is null");
+ return nullptr;
+ }
+
+ env->DeleteLocalRef(jcomm); // reduce the load on the garbage collector: we don't need that object anymore
+ }
+ return comms;
+}
+JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAll(JNIEnv *env, jclass cls, jobjectArray jcomms, jdouble timeout)
+{
+ int count;
+ msg_comm_t* comms = jarray_to_commArray(env, jcomms, &count);
+ if (not comms)
+ return;
+
+ MSG_comm_waitall(comms, count, static_cast<double>(timeout));
+ xbt_free(comms);
+}
+JNIEXPORT int JNICALL Java_org_simgrid_msg_Comm_waitAny(JNIEnv *env, jclass cls, jobjectArray jcomms)
+{
+ int count;
+ msg_comm_t* comms = jarray_to_commArray(env, jcomms, &count);
+ if (not comms)
+ return -1;
+ xbt_dynar_t dyn = xbt_dynar_new(sizeof(msg_comm_t),nullptr);
+ for (int i=0; i<count; i++) {
+ xbt_dynar_push(dyn, &(comms[i]));
+ }
+
+ int rank = MSG_comm_waitany(dyn);
+ xbt_free(comms);
+ xbt_dynar_free(&dyn);
+ return rank;
+}
+
+SG_END_DECL()