Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e2589398bf8a0fdd7eba9c0f8e97668b7d1e5383
[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 = (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   if (!jfield_class_Comm) {
49     jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
50     return;
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   if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task ||
58       !jcomm_field_Comm_finished) {
59     jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
60   }
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   xbt_free(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 (!comm) {
84     jxbt_throw_native(env,bprintf("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 (!comm) {
104     jxbt_throw_native(env,bprintf("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,(double)timeout);
115   env->SetBooleanField(jcomm, jcomm_field_Comm_finished, JNI_TRUE);
116   if (status == MSG_OK) {
117     jcomm_bind_task(env,jcomm);
118     return;
119   } else {
120     jmsg_throw_status(env,status);
121   }
122 }