Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix https://framagit.org/simgrid/simgrid/issues/29
[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 "simgrid/mutex.h"
13 #include "simgrid/semaphore.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   sg_mutex_t mutex = sg_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   sg_mutex_t mutex = (sg_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind);
32   sg_mutex_lock(mutex);
33 }
34
35 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) {
36   sg_mutex_t mutex;
37
38   mutex = (sg_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind);
39   sg_mutex_unlock(mutex);
40 }
41
42 JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_nativeFinalize(JNIEnv * env, jobject obj) {
43   sg_mutex_t mutex = (sg_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind);
44   sg_mutex_destroy(mutex);
45 }
46
47 static jfieldID jsynchro_field_Semaphore_bind;
48
49 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_nativeInit(JNIEnv *env, jclass cls) {
50   jsynchro_field_Semaphore_bind = jxbt_get_sfield(env, "org/simgrid/msg/Semaphore", "bind", "J");
51   xbt_assert(jsynchro_field_Semaphore_bind, "Native initialization of msg/Semaphore failed. Please report that bug");
52 }
53
54 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_init(JNIEnv * env, jobject obj, jint capacity) {
55   sg_sem_t sem = sg_sem_init((int)capacity);
56
57   env->SetLongField(obj, jsynchro_field_Semaphore_bind, (jlong)(uintptr_t)(sem));
58 }
59
60 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_acquire(JNIEnv * env, jobject obj, jdouble timeout) {
61   sg_sem_t sem;
62
63   sem             = (sg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind);
64   msg_error_t res = sg_sem_acquire_timeout(sem, (double)timeout) == 0 ? MSG_OK : MSG_TIMEOUT;
65   if (res != MSG_OK) {
66     jmsg_throw_status(env, res);
67   }
68 }
69
70 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_release(JNIEnv * env, jobject obj) {
71   sg_sem_t sem;
72
73   sem = (sg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind);
74   sg_sem_release(sem);
75 }
76
77 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Semaphore_wouldBlock(JNIEnv * env, jobject obj) {
78   sg_sem_t sem;
79
80   sem     = (sg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind);
81   int res = sg_sem_would_block(sem);
82   return (jboolean) res;
83 }
84
85 JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_nativeFinalize(JNIEnv * env, jobject obj) {
86   sg_sem_t sem;
87
88   sem = (sg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind);
89   sg_sem_destroy(sem);
90 }