Logo AND Algorithmique Numérique Distribuée

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