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. */
9 #include "xbt/synchro_core.h"
10 #include "jmsg_synchro.h"
11 #include "jxbt_utilities.h"
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
15 static jfieldID jsyncro_field_Mutex_bind;
17 JNIEXPORT void JNICALL
18 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."));
24 JNIEXPORT void JNICALL
25 Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) {
26 xbt_mutex_t mutex = xbt_mutex_init();
28 (*env)->SetLongField(env, obj, jsyncro_field_Mutex_bind, (jlong) (long) (mutex));
31 JNIEXPORT void JNICALL
32 Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) {
35 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
38 xbt_mutex_acquire(mutex);
45 JNIEXPORT void JNICALL
46 Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) {
49 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
50 xbt_mutex_release(mutex);
53 JNIEXPORT void JNICALL
54 Java_org_simgrid_msg_Mutex_exit(JNIEnv * env, jobject obj) {
57 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
58 xbt_mutex_destroy(mutex);
61 static jfieldID jsyncro_field_Semaphore_bind;
63 JNIEXPORT void JNICALL
64 Java_org_simgrid_msg_Semaphore_nativeInit(JNIEnv *env, jclass cls) {
65 jsyncro_field_Semaphore_bind = jxbt_get_sfield(env, "org/simgrid/msg/Semaphore", "bind", "J");
66 if (!jsyncro_field_Semaphore_bind) {
67 jxbt_throw_native(env,bprintf("Can't find some fields in Semaphore Java class. You should report this bug."));
70 JNIEXPORT void JNICALL
71 Java_org_simgrid_msg_Semaphore_init(JNIEnv * env, jobject obj, jint capacity) {
72 msg_sem_t sem = MSG_sem_init((int) capacity);
74 (*env)->SetLongField(env, obj, jsyncro_field_Semaphore_bind, (jlong) (long) (sem));
78 JNIEXPORT void JNICALL
79 Java_org_simgrid_msg_Semaphore_acquire(JNIEnv * env, jobject obj, jdouble timeout) {
82 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
83 msg_error_t res = MSG_sem_acquire_timeout(sem, (double) timeout);
85 jmsg_throw_status(env, res);
89 JNIEXPORT void JNICALL
90 Java_org_simgrid_msg_Semaphore_release(JNIEnv * env, jobject obj) {
93 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
96 JNIEXPORT jboolean JNICALL
97 Java_org_simgrid_msg_Semaphore_wouldBlock(JNIEnv * env, jobject obj) {
100 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
101 int res = MSG_sem_would_block(sem);
102 return (jboolean) res;
105 JNIEXPORT void JNICALL
106 Java_org_simgrid_msg_Mutex_destroy(JNIEnv * env, jobject obj) {
109 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
110 MSG_sem_destroy(sem);