Logo AND Algorithmique Numérique Distribuée

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