1 /* Functions exporting the simgrid synchronization mechanisms to the Java world */
3 /* Copyright (c) 2012. The SimGrid Team. All rights reserved. */
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. */
8 #include "xbt/synchro_core.h"
9 #include "jmsg_synchro.h"
10 #include "jxbt_utilities.h"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
14 static jfieldID jsyncro_field_Mutex_bind;
16 JNIEXPORT void JNICALL
17 Java_org_simgrid_msg_Mutex_nativeInit(JNIEnv *env, jclass cls) {
18 jsyncro_field_Mutex_bind = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J");
19 if (!jsyncro_field_Mutex_bind) {
20 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
23 JNIEXPORT void JNICALL
24 Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) {
25 xbt_mutex_t mutex = xbt_mutex_init();
27 (*env)->SetLongField(env, obj, jsyncro_field_Mutex_bind, (jlong) (long) (mutex));
30 JNIEXPORT void JNICALL
31 Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) {
34 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
37 xbt_mutex_acquire(mutex);
44 JNIEXPORT void JNICALL
45 Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) {
48 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
49 xbt_mutex_release(mutex);
52 JNIEXPORT void JNICALL
53 Java_org_simgrid_msg_Mutex_exit(JNIEnv * env, jobject obj) {
56 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
57 xbt_mutex_destroy(mutex);
60 static jfieldID jsyncro_field_Semaphore_bind;
62 JNIEXPORT void JNICALL
63 Java_org_simgrid_msg_Semaphore_nativeInit(JNIEnv *env, jclass cls) {
64 jsyncro_field_Semaphore_bind = jxbt_get_sfield(env, "org/simgrid/msg/Semaphore", "bind", "J");
65 if (!jsyncro_field_Semaphore_bind) {
66 jxbt_throw_native(env,bprintf("Can't find some fields in Semaphore Java class. You should report this bug."));
69 JNIEXPORT void JNICALL
70 Java_org_simgrid_msg_Semaphore_init(JNIEnv * env, jobject obj, jint capacity) {
71 msg_sem_t sem = MSG_sem_init((int) capacity);
73 (*env)->SetLongField(env, obj, jsyncro_field_Semaphore_bind, (jlong) (long) (sem));
77 JNIEXPORT void JNICALL
78 Java_org_simgrid_msg_Semaphore_acquire(JNIEnv * env, jobject obj, jdouble timeout) {
81 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
82 msg_error_t res = MSG_sem_acquire_timeout(sem, (double) timeout);
83 jmsg_throw_status(env, res);
85 JNIEXPORT void JNICALL
86 Java_org_simgrid_msg_Semaphore_release(JNIEnv * env, jobject obj) {
89 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
92 JNIEXPORT jboolean JNICALL
93 Java_org_simgrid_msg_Semaphore_wouldBlock(JNIEnv * env, jobject obj) {
96 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
97 int res = MSG_sem_would_block(sem);
98 return (jboolean) res;
101 JNIEXPORT void JNICALL
102 Java_org_simgrid_msg_Mutex_destroy(JNIEnv * env, jobject obj) {
105 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
106 MSG_sem_destroy(sem);