From: Martin Quinson Date: Wed, 2 May 2012 20:19:33 +0000 (+0200) Subject: Quick (and probably dirty) exportation of SimGrid simulated mutexes to Java X-Git-Tag: v3_9_90~569^2~19^2~113 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/edba2fa1e3d17eb5400a31cdf1a600bfe56fd210?hp=e18b7e480acb53c7434bfabac2299ef32db1c7d9 Quick (and probably dirty) exportation of SimGrid simulated mutexes to Java I have unfortunately to confess that I didn't test it at all. It should work, but no promise here. Please people, break it and report this. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 44081245bb..72f289e488 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,8 @@ set(JMSG_C_SRC src/jmsg_process.h src/jmsg_task.c src/jmsg_task.h + src/jmsg_synchro.c + src/jmsg_synchro.h src/jmsg_application_handler.c src/jmsg_application_handler.h ) @@ -105,6 +107,7 @@ set(JMSG_JAVA_SRC org/simgrid/msg/TaskCancelledException.java org/simgrid/msg/TimeoutException.java org/simgrid/msg/TransferFailureException.java + org/simgrid/msg/Mutex.java ) set(JAVA_EXAMPLES diff --git a/ChangeLog b/ChangeLog index 823c6fe52c..414854bb08 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ SimGrid-java (3.6.1) unstable; urgency=low + * Introduce msg.simgrid.Mutex, exporting simgrid mutex to Java. * Do not embeed our version of semaphores, java 1.5 can be considered as sufficiently prevalent for us to not dupplicate its features. * Make Process.kill(process) an instance method, not a static one diff --git a/org/simgrid/msg/Mutex.java b/org/simgrid/msg/Mutex.java new file mode 100644 index 0000000000..bf0bac7d02 --- /dev/null +++ b/org/simgrid/msg/Mutex.java @@ -0,0 +1,22 @@ +package org.simgrid.msg; +/** A mutex implemented on top of SimGrid synchronization mechanisms. + * You can use it exactly the same way that you use the mutexes, + * but to handle the interactions between the threads within the simulation. + * + * 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. + * + */ +public class Mutex { + private long bind; // The C object -- don't touch it + + public Mutex(int capa) { + init(capa); + } + private native void init(int capacity); + public native void acquire(); + public native void release(); +} diff --git a/src/jmsg_synchro.c b/src/jmsg_synchro.c new file mode 100644 index 0000000000..8cad78cd79 --- /dev/null +++ b/src/jmsg_synchro.c @@ -0,0 +1,42 @@ +/* Functions exporting the simgrid synchronization mechanisms to the Java world */ + +/* Copyright (c) 2012. The SimGrid Team. All rights 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. */ + +#include "jmsg_synchro.h" +#include "jxbt_utilities.h" + + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) { + xbt_mutex_t mutex = xbt_mutex_init(); + + jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J"); + if (!id) return; + + (*env)->SetLongField(env, obj, id, (jlong) (long) (mutex)); +} + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) { + xbt_mutex_t mutex; + + jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J"); + if (!id) return; + + mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, id); + xbt_mutex_acquire(mutex); +} + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) { + xbt_mutex_t mutex; + + jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J"); + if (!id) return; + + mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, id); + xbt_mutex_release(mutex); +} diff --git a/src/jmsg_synchro.h b/src/jmsg_synchro.h new file mode 100644 index 0000000000..3176e35cbd --- /dev/null +++ b/src/jmsg_synchro.h @@ -0,0 +1,26 @@ +/* Functions related to the java process instances. */ + +/* Copyright (c) 2007, 2009, 2010. The SimGrid Team. + * All rights 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. */ + +#ifndef MSG_JSYNCHRO_H +#define MSG_JSYNCHRO_H + +#include +#include +#include + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj); + + +#endif /* !MSG_JPROCESS_H */