Logo AND Algorithmique Numérique Distribuée

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