From: Gabriel Corona Date: Thu, 16 Jun 2016 07:25:03 +0000 (+0200) Subject: [simix] Refcounting with SIMIX_{process,mutex}_{ref,unref}() X-Git-Tag: v3_14~987^2~1 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/94a6ea22dbd2f12d1015925d3c3fe7a95b38d2e3 [simix] Refcounting with SIMIX_{process,mutex}_{ref,unref}() --- diff --git a/examples/msg/actions-comm/actions-comm.c b/examples/msg/actions-comm/actions-comm.c index 126936b91b..86486d2c80 100644 --- a/examples/msg/actions-comm/actions-comm.c +++ b/examples/msg/actions-comm/actions-comm.c @@ -183,7 +183,7 @@ static void action_barrier(const char *const *action) processes_arrived_sofar--; if (processes_arrived_sofar<=0) { SIMIX_cond_destroy(cond); - SIMIX_mutex_destroy(mutex); + SIMIX_mutex_unref(mutex); mutex = NULL; } } diff --git a/include/simgrid/s4u/actor.hpp b/include/simgrid/s4u/actor.hpp index a07c2bd1d0..24728db6a9 100644 --- a/include/simgrid/s4u/actor.hpp +++ b/include/simgrid/s4u/actor.hpp @@ -164,7 +164,10 @@ public: static void killAll(); protected: - smx_process_t getInferior() {return pimpl_;} + smx_process_t getInferior() + { + return pimpl_; + } private: smx_process_t pimpl_ = nullptr; }; diff --git a/include/simgrid/s4u/mutex.hpp b/include/simgrid/s4u/mutex.hpp index da7c3df863..201f0710a9 100644 --- a/include/simgrid/s4u/mutex.hpp +++ b/include/simgrid/s4u/mutex.hpp @@ -20,14 +20,14 @@ XBT_PUBLIC_CLASS Mutex { public: Mutex() : mutex_(simcall_mutex_init()) {} - Mutex(simgrid::simix::Mutex* mutex) : mutex_(SIMIX_mutex_dup(mutex)) {} + Mutex(simgrid::simix::Mutex* mutex) : mutex_(SIMIX_mutex_ref(mutex)) {} ~Mutex() { - SIMIX_mutex_destroy(mutex_); + SIMIX_mutex_unref(mutex_); } // Copy+move (with the copy-and-swap idiom): - Mutex(Mutex const& mutex) : mutex_(SIMIX_mutex_dup(mutex.mutex_)) {} + Mutex(Mutex const& mutex) : mutex_(SIMIX_mutex_ref(mutex.mutex_)) {} friend void swap(Mutex& first, Mutex& second) { using std::swap; diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index 065bf3ba6a..e95b78c0ee 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -214,6 +214,8 @@ XBT_PUBLIC(void) SIMIX_host_self_set_data(void *data); XBT_PUBLIC(void*) SIMIX_host_self_get_data(void); /********************************* Process ************************************/ +XBT_PUBLIC(smx_process_t) SIMIX_process_ref(smx_process_t process); +XBT_PUBLIC(void) SIMIX_process_unref(smx_process_t process); XBT_PUBLIC(int) SIMIX_process_count(void); XBT_PUBLIC(smx_process_t) SIMIX_process_self(void); XBT_PUBLIC(const char*) SIMIX_process_self_get_name(void); @@ -374,8 +376,8 @@ XBT_PUBLIC(void) simcall_set_category(smx_synchro_t synchro, const char *categor /************************** Synchro simcalls **********************************/ XBT_PUBLIC(smx_mutex_t) simcall_mutex_init(void); -XBT_PUBLIC(void) SIMIX_mutex_destroy(smx_mutex_t mutex); -XBT_PUBLIC(smx_mutex_t) SIMIX_mutex_dup(smx_mutex_t mutex); +XBT_PUBLIC(smx_mutex_t) SIMIX_mutex_ref(smx_mutex_t mutex); +XBT_PUBLIC(void) SIMIX_mutex_unref(smx_mutex_t mutex); XBT_PUBLIC(void) simcall_mutex_lock(smx_mutex_t mutex); XBT_PUBLIC(int) simcall_mutex_trylock(smx_mutex_t mutex); XBT_PUBLIC(void) simcall_mutex_unlock(smx_mutex_t mutex); diff --git a/src/simix/smx_process.cpp b/src/simix/smx_process.cpp index b47211a5d4..bcd0600d2d 100644 --- a/src/simix/smx_process.cpp +++ b/src/simix/smx_process.cpp @@ -28,6 +28,21 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX unsigned long simix_process_maxpid = 0; +/** Increase the refcount for this process */ +smx_process_t SIMIX_process_ref(smx_process_t process) +{ + if (process != nullptr) + intrusive_ptr_add_ref(process); + return process; +} + +/** Decrease the refcount for this process */ +void SIMIX_process_unref(smx_process_t process) +{ + if (process != nullptr) + intrusive_ptr_release(process); +} + /** * \brief Returns the current agent. * diff --git a/src/simix/smx_synchro.cpp b/src/simix/smx_synchro.cpp index 5a4d449efb..0d537621ce 100644 --- a/src/simix/smx_synchro.cpp +++ b/src/simix/smx_synchro.cpp @@ -187,17 +187,19 @@ void Mutex::unlock(smx_process_t issuer) } } -void SIMIX_mutex_destroy(smx_mutex_t mutex) +/** Increase the refcount for this mutex */ +smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex) { if (mutex != nullptr) - intrusive_ptr_release(mutex); + intrusive_ptr_add_ref(mutex); + return mutex; } -XBT_PUBLIC(smx_mutex_t) SIMIX_mutex_dup(smx_mutex_t mutex) +/** Decrease the refcount for this mutex */ +void SIMIX_mutex_unref(smx_mutex_t mutex) { if (mutex != nullptr) - intrusive_ptr_add_ref(mutex); - return mutex; + intrusive_ptr_release(mutex); } smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall) diff --git a/src/xbt/xbt_os_synchro.c b/src/xbt/xbt_os_synchro.c index 52b1762a1c..6c982bcf52 100644 --- a/src/xbt/xbt_os_synchro.c +++ b/src/xbt/xbt_os_synchro.c @@ -39,7 +39,7 @@ void xbt_mutex_release(xbt_mutex_t mutex) void xbt_mutex_destroy(xbt_mutex_t mutex) { - SIMIX_mutex_destroy((smx_mutex_t) mutex); + SIMIX_mutex_unref((smx_mutex_t) mutex); } /***** condition related functions *****/