1 /* Functions exporting the simgrid synchronization mechanisms to the Java world */
3 /* Copyright (c) 2012-2013. The SimGrid Team.
4 * All rights reserved. */
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. */
10 #include "xbt/synchro_core.h"
11 #include "jmsg_synchro.h"
12 #include "jxbt_utilities.h"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
16 static jfieldID jsyncro_field_Mutex_bind;
18 JNIEXPORT void JNICALL
19 Java_org_simgrid_msg_Mutex_nativeInit(JNIEnv *env, jclass cls) {
20 jsyncro_field_Mutex_bind = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J");
21 if (!jsyncro_field_Mutex_bind) {
22 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
25 JNIEXPORT void JNICALL
26 Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) {
27 xbt_mutex_t mutex = xbt_mutex_init();
29 (*env)->SetLongField(env, obj, jsyncro_field_Mutex_bind, (jlong) (long) (mutex));
32 JNIEXPORT void JNICALL
33 Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) {
36 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
39 xbt_mutex_acquire(mutex);
46 JNIEXPORT void JNICALL
47 Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) {
50 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
51 xbt_mutex_release(mutex);
54 JNIEXPORT void JNICALL
55 Java_org_simgrid_msg_Mutex_exit(JNIEnv * env, jobject obj) {
58 mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
59 xbt_mutex_destroy(mutex);
62 static jfieldID jsyncro_field_Semaphore_bind;
64 JNIEXPORT void JNICALL
65 Java_org_simgrid_msg_Semaphore_nativeInit(JNIEnv *env, jclass cls) {
66 jsyncro_field_Semaphore_bind = jxbt_get_sfield(env, "org/simgrid/msg/Semaphore", "bind", "J");
67 if (!jsyncro_field_Semaphore_bind) {
68 jxbt_throw_native(env,bprintf("Can't find some fields in Semaphore Java class. You should report this bug."));
71 JNIEXPORT void JNICALL
72 Java_org_simgrid_msg_Semaphore_init(JNIEnv * env, jobject obj, jint capacity) {
73 msg_sem_t sem = MSG_sem_init((int) capacity);
75 (*env)->SetLongField(env, obj, jsyncro_field_Semaphore_bind, (jlong) (long) (sem));
79 JNIEXPORT void JNICALL
80 Java_org_simgrid_msg_Semaphore_acquire(JNIEnv * env, jobject obj, jdouble timeout) {
83 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
84 msg_error_t res = MSG_sem_acquire_timeout(sem, (double) timeout);
86 jmsg_throw_status(env, res);
90 JNIEXPORT void JNICALL
91 Java_org_simgrid_msg_Semaphore_release(JNIEnv * env, jobject obj) {
94 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
97 JNIEXPORT jboolean JNICALL
98 Java_org_simgrid_msg_Semaphore_wouldBlock(JNIEnv * env, jobject obj) {
101 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
102 int res = MSG_sem_would_block(sem);
103 return (jboolean) res;
106 JNIEXPORT void JNICALL
107 Java_org_simgrid_msg_Mutex_destroy(JNIEnv * env, jobject obj) {
110 sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind);
111 MSG_sem_destroy(sem);