From: Frederic Suter Date: Wed, 10 Jul 2019 08:30:19 +0000 (+0200) Subject: fix https://framagit.org/simgrid/simgrid/issues/29 X-Git-Tag: v3.24~352 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/d2fa4c958fad5cd1ff8f7325a933326869936268 fix https://framagit.org/simgrid/simgrid/issues/29 --- diff --git a/include/simgrid/forward.h b/include/simgrid/forward.h index 233d1852e7..65b416c976 100644 --- a/include/simgrid/forward.h +++ b/include/simgrid/forward.h @@ -179,6 +179,7 @@ typedef simgrid::s4u::Barrier s4u_Barrier; typedef simgrid::s4u::Host s4u_Host; typedef simgrid::s4u::Link s4u_Link; typedef simgrid::s4u::File s4u_File; +typedef simgrid::s4u::Mutex s4u_Mutex; typedef simgrid::s4u::Semaphore s4u_Semaphore; typedef simgrid::s4u::Storage s4u_Storage; typedef simgrid::s4u::NetZone s4u_NetZone; @@ -199,6 +200,7 @@ typedef struct s4u_Barrier s4u_Barrier; typedef struct s4u_Host s4u_Host; typedef struct s4u_Link s4u_Link; typedef struct s4u_File s4u_File; +typedef struct s4u_Mutex s4u_Mutex; typedef struct s4u_Semaphore s4u_Semaphore; typedef struct s4u_Storage s4u_Storage; typedef struct s4u_NetZone s4u_NetZone; @@ -215,6 +217,7 @@ typedef struct s_smx_sem* smx_sem_t; #endif typedef s4u_Barrier* sg_bar_t; +typedef s4u_Mutex* sg_mutex_t; typedef s4u_Semaphore* sg_sem_t; typedef s4u_NetZone* sg_netzone_t; typedef s4u_Host* sg_host_t; diff --git a/include/simgrid/msg.h b/include/simgrid/msg.h index 46b6e67943..bbb438db96 100644 --- a/include/simgrid/msg.h +++ b/include/simgrid/msg.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/include/simgrid/s4u/Mutex.hpp b/include/simgrid/s4u/Mutex.hpp index bf39701d09..05f49123ad 100644 --- a/include/simgrid/s4u/Mutex.hpp +++ b/include/simgrid/s4u/Mutex.hpp @@ -31,12 +31,12 @@ class XBT_PUBLIC Mutex { friend kernel::activity::MutexImpl; kernel::activity::MutexImpl* const pimpl_; - explicit Mutex(kernel::activity::MutexImpl* mutex) : pimpl_(mutex) {} /* refcounting */ friend XBT_PUBLIC void intrusive_ptr_add_ref(Mutex* mutex); friend XBT_PUBLIC void intrusive_ptr_release(Mutex* mutex); public: + explicit Mutex(kernel::activity::MutexImpl* mutex) : pimpl_(mutex) {} ~Mutex(); // No copy: /** You cannot create a new mutex by copying an existing one. Use MutexPtr instead */ diff --git a/src/bindings/java/jmsg_synchro.cpp b/src/bindings/java/jmsg_synchro.cpp index 021dcce711..f00245cda1 100644 --- a/src/bindings/java/jmsg_synchro.cpp +++ b/src/bindings/java/jmsg_synchro.cpp @@ -9,7 +9,8 @@ #include "jmsg.hpp" #include "jxbt_utilities.hpp" #include "simgrid/Exception.hpp" -#include "xbt/synchro.h" +#include "simgrid/mutex.h" +#include "simgrid/semaphore.h" XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java); @@ -21,26 +22,26 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_nativeInit(JNIEnv *env, jclass } JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) { - xbt_mutex_t mutex = xbt_mutex_init(); + sg_mutex_t mutex = sg_mutex_init(); env->SetLongField(obj, jsynchro_field_Mutex_bind, (jlong)(uintptr_t)(mutex)); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) { - xbt_mutex_t mutex = (xbt_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind); - xbt_mutex_acquire(mutex); + sg_mutex_t mutex = (sg_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind); + sg_mutex_lock(mutex); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) { - xbt_mutex_t mutex; + sg_mutex_t mutex; - mutex = (xbt_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind); - xbt_mutex_release(mutex); + mutex = (sg_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind); + sg_mutex_unlock(mutex); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_nativeFinalize(JNIEnv * env, jobject obj) { - xbt_mutex_t mutex = (xbt_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind); - xbt_mutex_destroy(mutex); + sg_mutex_t mutex = (sg_mutex_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Mutex_bind); + sg_mutex_destroy(mutex); } static jfieldID jsynchro_field_Semaphore_bind; @@ -51,39 +52,39 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_nativeInit(JNIEnv *env, jc } JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_init(JNIEnv * env, jobject obj, jint capacity) { - msg_sem_t sem = MSG_sem_init((int) capacity); + sg_sem_t sem = sg_sem_init((int)capacity); env->SetLongField(obj, jsynchro_field_Semaphore_bind, (jlong)(uintptr_t)(sem)); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_acquire(JNIEnv * env, jobject obj, jdouble timeout) { - msg_sem_t sem; + sg_sem_t sem; - sem = (msg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind); - msg_error_t res = MSG_sem_acquire_timeout(sem, (double)timeout) == 0 ? MSG_OK : MSG_TIMEOUT; + sem = (sg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind); + msg_error_t res = sg_sem_acquire_timeout(sem, (double)timeout) == 0 ? MSG_OK : MSG_TIMEOUT; if (res != MSG_OK) { jmsg_throw_status(env, res); } } JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_release(JNIEnv * env, jobject obj) { - msg_sem_t sem; + sg_sem_t sem; - sem = (msg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind); - MSG_sem_release(sem); + sem = (sg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind); + sg_sem_release(sem); } JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Semaphore_wouldBlock(JNIEnv * env, jobject obj) { - msg_sem_t sem; + sg_sem_t sem; - sem = (msg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind); - int res = MSG_sem_would_block(sem); + sem = (sg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind); + int res = sg_sem_would_block(sem); return (jboolean) res; } JNIEXPORT void JNICALL Java_org_simgrid_msg_Semaphore_nativeFinalize(JNIEnv * env, jobject obj) { - msg_sem_t sem; + sg_sem_t sem; - sem = (msg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind); - MSG_sem_destroy(sem); + sem = (sg_sem_t)(uintptr_t)env->GetLongField(obj, jsynchro_field_Semaphore_bind); + sg_sem_destroy(sem); } diff --git a/src/s4u/s4u_Mutex.cpp b/src/s4u/s4u_Mutex.cpp index 2f90b265d9..95658bb9fc 100644 --- a/src/s4u/s4u_Mutex.cpp +++ b/src/s4u/s4u_Mutex.cpp @@ -3,6 +3,8 @@ /* 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 "simgrid/forward.h" +#include "simgrid/mutex.h" #include "simgrid/s4u/Mutex.hpp" #include "src/kernel/activity/MutexImpl.hpp" @@ -62,3 +64,32 @@ void intrusive_ptr_release(Mutex* mutex) } // namespace s4u } // namespace simgrid + +/* **************************** Public C interface *************************** */ +sg_mutex_t sg_mutex_init() +{ + simgrid::kernel::activity::MutexImpl* mutex = + simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); }); + + return new simgrid::s4u::Mutex(mutex); +} + +void sg_mutex_lock(sg_mutex_t mutex) +{ + mutex->lock(); +} + +void sg_mutex_unlock(sg_mutex_t mutex) +{ + mutex->unlock(); +} + +int sg_mutex_try_lock(sg_mutex_t mutex) +{ + return mutex->try_lock(); +} + +void sg_mutex_destroy(sg_mutex_t mutex) +{ + delete mutex; +} diff --git a/teshsuite/mc/mutex-handling/mutex-handling.c b/teshsuite/mc/mutex-handling/mutex-handling.c index ca13fd7995..c367cdf758 100644 --- a/teshsuite/mc/mutex-handling/mutex-handling.c +++ b/teshsuite/mc/mutex-handling/mutex-handling.c @@ -27,7 +27,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example") #define BOX_NAME "box" #ifndef DISABLE_THE_MUTEX -static xbt_mutex_t mutex = NULL; +static sg_mutex_t mutex = NULL; #endif static int receiver(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[]) @@ -50,11 +50,11 @@ static int sender(int argc, char *argv[]) xbt_assert(argc == 2); char* message_name = argv[1]; #ifndef DISABLE_THE_MUTEX - xbt_mutex_acquire(mutex); + sg_mutex_lock(mutex); #endif MSG_task_send(MSG_task_create(message_name, 0.0, 0.0, NULL), BOX_NAME); #ifndef DISABLE_THE_MUTEX - xbt_mutex_release(mutex); + sg_mutex_unlock(mutex); #endif return 0; } @@ -71,11 +71,12 @@ int main(int argc, char *argv[]) MSG_launch_application(argv[2]); #ifndef DISABLE_THE_MUTEX - mutex = xbt_mutex_init(); + mutex = sg_mutex_init(); #endif msg_error_t res = MSG_main(); #ifndef DISABLE_THE_MUTEX - xbt_mutex_destroy(mutex); mutex = NULL; + sg_mutex_destroy(mutex); + mutex = NULL; #endif XBT_INFO("Simulation time %g", MSG_get_clock()); diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index f208d945b5..b05a4a8939 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -693,6 +693,7 @@ set(headers_to_install include/simgrid/kernel/future.hpp include/simgrid/host.h include/simgrid/link.h + include/simgrid/mutex.h include/simgrid/semaphore.h include/simgrid/storage.h include/simgrid/vm.h