Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b8afdeca40f13b0811f485e9e742098f7d98f4f2
[simgrid.git] / src / bindings / java / jmsg_synchro.cpp
1 /* Java bindings of the Synchronization API.                                */
2
3 /* Copyright (c) 2012-2018. 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 "simgrid/exception.hpp"
9
10 #include "jmsg.hpp"
11 #include "jmsg_synchro.h"
12 #include "jxbt_utilities.hpp"
13 #include "xbt/synchro.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
16
17 static jfieldID jsynchro_field_Mutex_bind;
18
19 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_nativeInit(JNIEnv *env, jclass cls) {
20   jsynchro_field_Mutex_bind = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J");
21   xbt_assert(jsynchro_field_Mutex_bind, "Native initialization of msg/Mutex failed. Please report that bug");
22 }
23
24 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) {
25   xbt_mutex_t mutex = xbt_mutex_init();
26
27   env->SetLongField(obj, jsynchro_field_Mutex_bind, (jlong)(uintptr_t)(mutex));
28 }
29
30 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) {
31   xbt_mutex_t mutex = (xbt_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind);
32   try {
33     xbt_mutex_acquire(mutex);
34   } catch (xbt_ex const& e) {
35     XBT_DEBUG("Caught an exception: %s", e.what());
36   }
37 }
38
39 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) {
40   xbt_mutex_t mutex;
41
42   mutex = (xbt_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind);
43   xbt_mutex_release(mutex);
44 }
45
46 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_nativeFinalize(JNIEnv * env, jobject obj) {
47   xbt_mutex_t mutex = (xbt_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind);
48   xbt_mutex_destroy(mutex);
49 }
50
51 static jfieldID jsynchro_field_Semaphore_bind;
52
53 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_nativeInit(JNIEnv *env, jclass cls) {
54   jsynchro_field_Semaphore_bind = jxbt_get_sfield(env, "org/simgrid/msg/Semaphore", "bind", "J");
55   xbt_assert(jsynchro_field_Semaphore_bind, "Native initialization of msg/Semaphore failed. Please report that bug");
56 }
57
58 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_init(JNIEnv * env, jobject obj, jint capacity) {
59   msg_sem_t sem = MSG_sem_init((int) capacity);
60
61   env->SetLongField(obj, jsynchro_field_Semaphore_bind, (jlong)(uintptr_t)(sem));
62 }
63
64 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_acquire(JNIEnv * env, jobject obj, jdouble timeout) {
65   msg_sem_t sem;
66
67   sem             = (msg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind);
68   msg_error_t res = MSG_sem_acquire_timeout(sem, (double) timeout);
69   if (res != MSG_OK) {
70     jmsg_throw_status(env, res);
71   }
72 }
73
74 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_release(JNIEnv * env, jobject obj) {
75   msg_sem_t sem;
76
77   sem = (msg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind);
78   MSG_sem_release(sem);
79 }
80
81 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Semaphore_wouldBlock(JNIEnv * env, jobject obj) {
82   msg_sem_t sem;
83
84   sem     = (msg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind);
85   int res = MSG_sem_would_block(sem);
86   return (jboolean) res;
87 }
88
89 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_nativeFinalize(JNIEnv * env, jobject obj) {
90   msg_sem_t sem;
91
92   sem = (msg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind);
93   MSG_sem_destroy(sem);
94 }