Logo AND Algorithmique Numérique Distribuée

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