Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't specify RuntimeExceptions in throws clauses
[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 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   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_comm_t comm;
62   msg_task_t *task_received;
63
64   task_received = (msg_task_t*)  (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_taskBind);
65   xbt_free(task_received);
66
67   comm = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
68   MSG_comm_destroy(comm);
69 }
70
71 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
72   msg_comm_t comm;
73   comm = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
74
75   jboolean finished = env->GetBooleanField(jcomm, jcomm_field_Comm_finished);
76   if (finished == JNI_TRUE) {
77     return JNI_TRUE;
78   }
79
80   if (!comm) {
81     jxbt_throw_null(env, bprintf("comm is null"));
82     return JNI_FALSE;
83   }
84
85   if (MSG_comm_test(comm)) {
86     msg_error_t status = MSG_comm_get_status(comm);
87     if (status == MSG_OK) {
88       jcomm_bind_task(env,jcomm);
89       return JNI_TRUE;
90     } else {
91       //send the correct exception
92       jmsg_throw_status(env,status);
93     }
94   }
95   return JNI_FALSE;
96 }
97
98 JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
99   msg_comm_t comm = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
100   if (!comm) {
101     jxbt_throw_null(env, bprintf("comm is null"));
102     return;
103   }
104
105   jboolean finished = env->GetBooleanField(jcomm, jcomm_field_Comm_finished);
106   if (finished == JNI_TRUE) {
107     return;
108   }
109
110   msg_error_t status;
111   status = MSG_comm_wait(comm,static_cast<double>(timeout));
112   env->SetBooleanField(jcomm, jcomm_field_Comm_finished, JNI_TRUE);
113   if (status == MSG_OK) {
114     jcomm_bind_task(env,jcomm);
115   } else {
116     jmsg_throw_status(env,status);
117   }
118 }
119
120 static msg_comm_t* jarray_to_commArray(JNIEnv *env, jobjectArray jcomms, /* OUT */ int *count)
121 {
122   *count = env->GetArrayLength(jcomms);
123   msg_comm_t* comms = xbt_new(msg_comm_t, *count);
124
125   for (int i=0; i < *count; i++) {
126      jobject jcomm = env->GetObjectArrayElement(jcomms, i);
127      if (env->ExceptionOccurred())
128         break;
129
130      comms[i] = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
131      if (!comms[i]) {
132        jxbt_throw_null(env, bprintf("comm at rank %d is null", i));
133        return nullptr;
134      }
135
136      env->DeleteLocalRef(jcomm); // reduce the load on the garbage collector: we don't need that object anymore
137   }
138   return comms;
139 }
140 JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAll(JNIEnv *env, jclass cls, jobjectArray jcomms, jdouble timeout)
141 {
142   int count;
143   msg_comm_t* comms = jarray_to_commArray(env, jcomms, &count);
144   if (!comms)
145     return;
146
147   MSG_comm_waitall(comms, count, static_cast<double>(timeout));
148   xbt_free(comms);
149 }
150 JNIEXPORT int JNICALL Java_org_simgrid_msg_Comm_waitAny(JNIEnv *env, jclass cls, jobjectArray jcomms)
151 {
152   int count;
153   msg_comm_t* comms = jarray_to_commArray(env, jcomms, &count);
154   if (!comms)
155     return -1;
156   xbt_dynar_t dyn = xbt_dynar_new(sizeof(msg_comm_t),nullptr);
157   for (int i=0; i<count; i++) {
158     xbt_dynar_push(dyn, &(comms[i]));
159   }
160
161   int rank = MSG_comm_waitany(dyn);
162   xbt_free(comms);
163   xbt_dynar_free(&dyn);
164   return rank;
165 }