Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / src / bindings / java / jmsg_synchro.cpp
1 /* Functions exporting the simgrid synchronization mechanisms to the Java world */
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 <xbt/ex.hpp>
10
11 #include "jmsg.h"
12 #include "jmsg_synchro.h"
13 #include "jxbt_utilities.h"
14 #include "xbt/synchro.h"
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
17
18 static jfieldID jsyncro_field_Mutex_bind;
19
20 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_nativeInit(JNIEnv *env, jclass cls) {
21   jsyncro_field_Mutex_bind = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J");
22   if (!jsyncro_field_Mutex_bind) {
23     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
24   }
25 }
26
27 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) {
28   xbt_mutex_t mutex = xbt_mutex_init();
29
30   env->SetLongField(obj, jsyncro_field_Mutex_bind, (jlong) (uintptr_t) (mutex));
31 }
32
33 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) {
34   xbt_mutex_t mutex = (xbt_mutex_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Mutex_bind);
35   try {
36     xbt_mutex_acquire(mutex);
37   }
38   catch(xbt_ex& e) {
39     // Nothing to do
40   }
41 }
42
43 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) {
44   xbt_mutex_t mutex;
45
46   mutex = (xbt_mutex_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Mutex_bind);
47   xbt_mutex_release(mutex);
48 }
49
50 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_nativeFinalize(JNIEnv * env, jobject obj) {
51   xbt_mutex_t mutex;
52
53   mutex = (xbt_mutex_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Mutex_bind);
54   xbt_mutex_destroy(mutex);
55 }
56
57 static jfieldID jsyncro_field_Semaphore_bind;
58
59 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_nativeInit(JNIEnv *env, jclass cls) {
60   jsyncro_field_Semaphore_bind = jxbt_get_sfield(env, "org/simgrid/msg/Semaphore", "bind", "J");
61   if (!jsyncro_field_Semaphore_bind) {
62     jxbt_throw_native(env,bprintf("Can't find some fields in Semaphore Java class. You should report this bug."));
63   }
64 }
65
66 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_init(JNIEnv * env, jobject obj, jint capacity) {
67   msg_sem_t sem = MSG_sem_init((int) capacity);
68
69   env->SetLongField(obj, jsyncro_field_Semaphore_bind, (jlong) (uintptr_t) (sem));
70 }
71
72 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_acquire(JNIEnv * env, jobject obj, jdouble timeout) {
73   msg_sem_t sem;
74
75   sem = (msg_sem_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Semaphore_bind);
76   msg_error_t res = MSG_sem_acquire_timeout(sem, (double) timeout);
77   if (res != MSG_OK) {
78     jmsg_throw_status(env, res);
79   }
80 }
81
82 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_release(JNIEnv * env, jobject obj) {
83   msg_sem_t sem;
84
85   sem = (msg_sem_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Semaphore_bind);
86   MSG_sem_release(sem);
87 }
88
89 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Semaphore_wouldBlock(JNIEnv * env, jobject obj) {
90   msg_sem_t sem;
91
92   sem = (msg_sem_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Semaphore_bind);
93   int res = MSG_sem_would_block(sem);
94   return (jboolean) res;
95 }
96
97 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_nativeFinalize(JNIEnv * env, jobject obj) {
98   msg_sem_t sem;
99
100   sem = (msg_sem_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Semaphore_bind);
101   MSG_sem_destroy(sem);
102 }