Logo AND Algorithmique Numérique Distribuée

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