Logo AND Algorithmique Numérique Distribuée

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