Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Quick (and probably dirty) exportation of SimGrid simulated mutexes to Java
authorMartin Quinson <martin.quinson@loria.fr>
Wed, 2 May 2012 20:19:33 +0000 (22:19 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Wed, 2 May 2012 20:21:12 +0000 (22:21 +0200)
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.

CMakeLists.txt
ChangeLog
org/simgrid/msg/Mutex.java [new file with mode: 0644]
src/jmsg_synchro.c [new file with mode: 0644]
src/jmsg_synchro.h [new file with mode: 0644]

index 4408124..72f289e 100644 (file)
@@ -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
index 823c6fe..414854b 100644 (file)
--- 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 (file)
index 0000000..bf0bac7
--- /dev/null
@@ -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 (file)
index 0000000..8cad78c
--- /dev/null
@@ -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 (file)
index 0000000..3176e35
--- /dev/null
@@ -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 <jni.h>
+#include <msg/msg.h>
+#include <simgrid/simix.h>
+
+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 */