From f096c7f1b7e7bb263674ba2d6074ec9b1df44ee6 Mon Sep 17 00:00:00 2001 From: Lionel Date: Thu, 6 Jun 2013 21:40:44 +0200 Subject: [PATCH 1/1] Added Semaphore visibility to the Java bindings --- buildtools/Cmake/DefinePackages.cmake | 1 + src/bindings/java/jmsg_synchro.c | 49 +++++++++++ .../java/org/simgrid/msg/Semaphore.java | 86 +++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/bindings/java/org/simgrid/msg/Semaphore.java diff --git a/buildtools/Cmake/DefinePackages.cmake b/buildtools/Cmake/DefinePackages.cmake index e4d5cd5020..10c1f03cd5 100644 --- a/buildtools/Cmake/DefinePackages.cmake +++ b/buildtools/Cmake/DefinePackages.cmake @@ -424,6 +424,7 @@ set(JMSG_JAVA_SRC src/bindings/java/org/simgrid/msg/Msg.java src/bindings/java/org/simgrid/msg/MsgException.java src/bindings/java/org/simgrid/msg/Mutex.java + src/bindings/java/org/simgrid/msg/Semaphore.java src/bindings/java/org/simgrid/msg/NativeException.java src/bindings/java/org/simgrid/msg/Process.java src/bindings/java/org/simgrid/msg/ProcessKilledError.java diff --git a/src/bindings/java/jmsg_synchro.c b/src/bindings/java/jmsg_synchro.c index cbccf4cd26..21c04d7a6d 100644 --- a/src/bindings/java/jmsg_synchro.c +++ b/src/bindings/java/jmsg_synchro.c @@ -56,3 +56,52 @@ Java_org_simgrid_msg_Mutex_exit(JNIEnv * env, jobject obj) { mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind); xbt_mutex_destroy(mutex); } + +static jfieldID jsyncro_field_Semaphore_bind; + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Semaphore_nativeInit(JNIEnv *env, jclass cls) { + jsyncro_field_Semaphore_bind = jxbt_get_sfield(env, "org/simgrid/msg/Semaphore", "bind", "J"); + if (!jsyncro_field_Semaphore_bind) { + jxbt_throw_native(env,bprintf("Can't find some fields in Semaphore Java class. You should report this bug.")); + } +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Semaphore_init(JNIEnv * env, jobject obj, jint capacity) { + msg_sem_t sem = MSG_sem_init((int) capacity); + + (*env)->SetLongField(env, obj, jsyncro_field_Semaphore_bind, (jlong) (long) (sem)); +} + + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Semaphore_acquire(JNIEnv * env, jobject obj, jdouble timeout) { + msg_sem_t sem; + + sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind); + msg_error_t res = MSG_sem_acquire_timeout(sem, (double) timeout); + jmsg_throw_status(env, res); +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Semaphore_release(JNIEnv * env, jobject obj) { + msg_sem_t sem; + + sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind); + MSG_sem_release(sem); +} +JNIEXPORT jboolean JNICALL +Java_org_simgrid_msg_Semaphore_wouldBlock(JNIEnv * env, jobject obj) { + msg_sem_t sem; + + sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind); + int res = MSG_sem_would_block(sem); + return (jboolean) res; +} + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Mutex_destroy(JNIEnv * env, jobject obj) { + msg_sem_t sem; + + sem = (msg_sem_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Semaphore_bind); + MSG_sem_destroy(sem); +} diff --git a/src/bindings/java/org/simgrid/msg/Semaphore.java b/src/bindings/java/org/simgrid/msg/Semaphore.java new file mode 100644 index 0000000000..f1a276b1c3 --- /dev/null +++ b/src/bindings/java/org/simgrid/msg/Semaphore.java @@ -0,0 +1,86 @@ +/* + * Copyright 2012 The SimGrid team. All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + * (GNU LGPL) which comes with this package. + * + */ +package org.simgrid.msg; +/** A semaphore implemented on top of SimGrid synchronization mechanisms. + * You can use it exactly the same way that you use classical semaphores + * but to handle the interactions between the processes within the simulation. + * + */ + +public class Semaphore { + private long bind; // The C object -- don't touch it + /** + * Semaphore capacity, defined when the semaphore is created. At most capacity + * process can acquire this semaphore at the same time. + */ + protected final int capacity; + /** + * Creates a new semaphore with the given capacity. At most capacity + * process can acquire this semaphore at the same time. + */ + public Semaphore(int capacity) { + init(capacity); + this.capacity = capacity; + } + /** The native implementation of semaphore initialization + */ + private native void init(int capacity); + + + /** Locks on the semaphore object until the provided timeout expires + * @exception TimeoutException if the timeout expired before + * the semaphore could be acquired. + */ + public native void acquire(double timeout) throws TimeoutException; + /** Locks on the semaphore object with no timeout + */ + public void acquire() { + try { + acquire(-1); + } catch (TimeoutException e) { + // This should not happen. + assert(false); + } + } + /** Releases the semaphore object + */ + public native void release(); + /** returns a boolean indicating it this semaphore would block at this very specific time + * + * Note that the returned value may be wrong right after the + * function call, when you try to use it... But that's a + * classical semaphore issue, and SimGrid's semaphores are not + * different to usual ones here. + */ + public native boolean wouldBlock(); + + /** Returns the semaphore capacity + */ + public int getCapacity(){ + return this.capacity; + } + + + /** Deletes this semaphore + */ + protected void finalize() { + destroy(); + } + /** The native implementation for destroying a semaphore + */ + private native void destroy(); + /** + * Class initializer, to initialize various JNI stuff + */ + public static native void nativeInit(); + static { + Msg.nativeInit(); + nativeInit(); + } +} \ No newline at end of file -- 2.20.1