Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into adrien
[simgrid.git] / src / bindings / java / jmsg_comm.cpp
1 /* Java bindings to the Comm API                                            */
2
3 /* Copyright (c) 2012-2020. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "jmsg_comm.h"
9 #include "jmsg.hpp"
10 #include "jxbt_utilities.hpp"
11
12 #include <string>
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
15
16 static jfieldID jcomm_field_Comm_bind;
17 static jfieldID jcomm_field_Comm_finished;
18 static jfieldID jcomm_field_Comm_receiving;
19 static jfieldID jtask_field_Comm_task;
20 static jfieldID jcomm_field_Comm_taskBind;
21
22 void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
23   const_msg_comm_t comm = (msg_comm_t)(uintptr_t)env->GetLongField(jcomm, jcomm_field_Comm_bind);
24   //test if we are receiving or sending a task.
25   jboolean jreceiving = env->GetBooleanField(jcomm, jcomm_field_Comm_receiving);
26   if (jreceiving == JNI_TRUE) {
27     //bind the task object.
28     msg_task_t task = MSG_comm_get_task(comm);
29     xbt_assert(task != nullptr, "Task is nullptr");
30     jobject jtask_global = static_cast<jobject>(MSG_task_get_data(task));
31     //case where the data has already been retrieved
32     if (jtask_global == nullptr) {
33       return;
34     }
35
36     //Make sure the data will be correctly gc.
37     jobject jtask_local = env->NewLocalRef(jtask_global);
38     env->DeleteGlobalRef(jtask_global);
39
40     env->SetObjectField(jcomm, jtask_field_Comm_task, jtask_local);
41
42     MSG_task_set_data(task, nullptr);
43   }
44 }
45
46 JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
47   jclass jfield_class_Comm = env->FindClass("org/simgrid/msg/Comm");
48   xbt_assert(jfield_class_Comm, "Native initialization of msg/Comm failed. Please report that bug");
49
50   jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
51   jcomm_field_Comm_taskBind  = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
52   jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
53   jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
54   jcomm_field_Comm_finished = jxbt_get_jfield(env, jfield_class_Comm, "finished", "Z");
55   xbt_assert(jcomm_field_Comm_bind && jcomm_field_Comm_taskBind && jcomm_field_Comm_receiving &&
56                  jtask_field_Comm_task && jcomm_field_Comm_finished,
57              "Native initialization of msg/Comm failed. Please report that bug");
58 }
59
60 JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_nativeFinalize(JNIEnv *env, jobject jcomm) {
61   msg_task_t *task_received;
62
63   task_received = (msg_task_t*)  (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_taskBind);
64   delete task_received;
65
66   const_msg_comm_t comm = (msg_comm_t)(uintptr_t)env->GetLongField(jcomm, jcomm_field_Comm_bind);
67   MSG_comm_destroy(comm);
68 }
69
70 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
71   msg_comm_t comm;
72   comm = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
73
74   jboolean finished = env->GetBooleanField(jcomm, jcomm_field_Comm_finished);
75   if (finished == JNI_TRUE) {
76     return JNI_TRUE;
77   }
78
79   if (not comm) {
80     jxbt_throw_null(env, "comm is null");
81     return JNI_FALSE;
82   }
83
84   if (MSG_comm_test(comm)) {
85     msg_error_t status = MSG_comm_get_status(comm);
86     if (status == MSG_OK) {
87       jcomm_bind_task(env,jcomm);
88       return JNI_TRUE;
89     } else {
90       //send the correct exception
91       jmsg_throw_status(env,status);
92     }
93   }
94   return JNI_FALSE;
95 }
96
97 JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
98   msg_comm_t comm = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
99   if (not comm) {
100     jxbt_throw_null(env, "comm is null");
101     return;
102   }
103
104   jboolean finished = env->GetBooleanField(jcomm, jcomm_field_Comm_finished);
105   if (finished == JNI_TRUE) {
106     return;
107   }
108
109   msg_error_t status;
110   status = MSG_comm_wait(comm, timeout);
111   env->SetBooleanField(jcomm, jcomm_field_Comm_finished, JNI_TRUE);
112   if (status == MSG_OK) {
113     jcomm_bind_task(env,jcomm);
114   } else {
115     jmsg_throw_status(env,status);
116   }
117 }
118
119 static msg_comm_t* jarray_to_commArray(JNIEnv *env, jobjectArray jcomms, /* OUT */ int *count)
120 {
121   *count = env->GetArrayLength(jcomms);
122   msg_comm_t* comms = new msg_comm_t[*count];
123
124   for (int i=0; i < *count; i++) {
125      jobject jcomm = env->GetObjectArrayElement(jcomms, i);
126      if (env->ExceptionOccurred())
127         break;
128
129      comms[i] = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
130      if (not comms[i]) {
131        jxbt_throw_null(env, std::string("comm at rank ") + std::to_string(i) + " is null");
132        return nullptr;
133      }
134
135      env->DeleteLocalRef(jcomm); // reduce the load on the garbage collector: we don't need that object anymore
136   }
137   return comms;
138 }
139 JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAll(JNIEnv *env, jclass cls, jobjectArray jcomms, jdouble timeout)
140 {
141   int count;
142   msg_comm_t* comms = jarray_to_commArray(env, jcomms, &count);
143   if (not comms)
144     return;
145
146   MSG_comm_waitall(comms, count, timeout);
147   delete[] comms;
148 }
149 JNIEXPORT int JNICALL Java_org_simgrid_msg_Comm_waitAny(JNIEnv *env, jclass cls, jobjectArray jcomms)
150 {
151   int count;
152   msg_comm_t* comms = jarray_to_commArray(env, jcomms, &count);
153   if (not comms)
154     return -1;
155   xbt_dynar_t dyn = xbt_dynar_new(sizeof(msg_comm_t),nullptr);
156   for (int i=0; i<count; i++) {
157     xbt_dynar_push(dyn, &(comms[i]));
158   }
159
160   int rank = MSG_comm_waitany(dyn);
161   delete[] comms;
162   xbt_dynar_free(&dyn);
163   return rank;
164 }