From: adfaure Date: Thu, 16 Jun 2016 15:09:36 +0000 (+0200) Subject: [simix] Refcounting with SIMIX_cond_{ref,unref}() X-Git-Tag: v3_14~983 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/f41bb0741ae67b3953b31cd80ae100ac58b86cab [simix] Refcounting with SIMIX_cond_{ref,unref}() --- diff --git a/examples/msg/actions-comm/actions-comm.c b/examples/msg/actions-comm/actions-comm.c index 86486d2c80..0e0219473c 100644 --- a/examples/msg/actions-comm/actions-comm.c +++ b/examples/msg/actions-comm/actions-comm.c @@ -182,7 +182,7 @@ static void action_barrier(const char *const *action) processes_arrived_sofar--; if (processes_arrived_sofar<=0) { - SIMIX_cond_destroy(cond); + SIMIX_cond_unref(cond); SIMIX_mutex_unref(mutex); mutex = NULL; } diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index e95b78c0ee..ebd25982e8 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -383,7 +383,7 @@ XBT_PUBLIC(int) simcall_mutex_trylock(smx_mutex_t mutex); XBT_PUBLIC(void) simcall_mutex_unlock(smx_mutex_t mutex); XBT_PUBLIC(smx_cond_t) simcall_cond_init(void); -XBT_PUBLIC(void) SIMIX_cond_destroy(smx_cond_t cond); +XBT_PUBLIC(void) SIMIX_cond_unref(smx_cond_t cond); XBT_PUBLIC(void) simcall_cond_signal(smx_cond_t cond); XBT_PUBLIC(void) simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex); XBT_PUBLIC(void) simcall_cond_wait_timeout(smx_cond_t cond, diff --git a/src/simix/smx_synchro.cpp b/src/simix/smx_synchro.cpp index 0d537621ce..3817b1744a 100644 --- a/src/simix/smx_synchro.cpp +++ b/src/simix/smx_synchro.cpp @@ -240,6 +240,8 @@ smx_cond_t SIMIX_cond_init(void) smx_cond_t cond = xbt_new0(s_smx_cond_t, 1); cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup)); cond->mutex = nullptr; + cond->refcount_ = 1; + intrusive_ptr_add_ref(cond); XBT_OUT(); return cond; } @@ -356,19 +358,34 @@ void SIMIX_cond_broadcast(smx_cond_t cond) * Destroys and frees the condition's memory. * \param cond A condition */ -void SIMIX_cond_destroy(smx_cond_t cond) +void SIMIX_cond_unref(smx_cond_t cond) { XBT_IN("(%p)",cond); XBT_DEBUG("Destroy condition %p", cond); - if (cond != nullptr) { + intrusive_ptr_release(cond); + } + XBT_OUT(); +} + + +void intrusive_ptr_add_ref(s_smx_cond_t *cond) +{ + auto previous = (cond->refcount_)++; + xbt_assert(previous != 0); + (void) previous; +} + +void intrusive_ptr_release(s_smx_cond_t *cond) +{ + auto count = --(cond->refcount_); + if (count == 0) { xbt_assert(xbt_swag_size(cond->sleeping) == 0, "Cannot destroy conditional since someone is still using it"); xbt_swag_free(cond->sleeping); xbt_free(cond); } - XBT_OUT(); } /******************************** Semaphores **********************************/ diff --git a/src/simix/smx_synchro_private.h b/src/simix/smx_synchro_private.h index d9d039175a..943e1adf1c 100644 --- a/src/simix/smx_synchro_private.h +++ b/src/simix/smx_synchro_private.h @@ -58,6 +58,7 @@ private: typedef struct s_smx_cond { smx_mutex_t mutex; xbt_swag_t sleeping; /* list of sleeping process */ + std::atomic_int_fast32_t refcount_; } s_smx_cond_t; typedef struct s_smx_sem { @@ -79,4 +80,6 @@ XBT_PRIVATE void SIMIX_sem_release(smx_sem_t sem); XBT_PRIVATE int SIMIX_sem_would_block(smx_sem_t sem); XBT_PRIVATE int SIMIX_sem_get_capacity(smx_sem_t sem); +XBT_PRIVATE void intrusive_ptr_release(s_smx_cond_t *cond); +XBT_PRIVATE void intrusive_ptr_add_ref(s_smx_cond_t *cond); #endif diff --git a/src/xbt/xbt_os_synchro.c b/src/xbt/xbt_os_synchro.c index 6c982bcf52..22a4908db4 100644 --- a/src/xbt/xbt_os_synchro.c +++ b/src/xbt/xbt_os_synchro.c @@ -70,7 +70,7 @@ void xbt_cond_broadcast(xbt_cond_t cond) void xbt_cond_destroy(xbt_cond_t cond) { - SIMIX_cond_destroy((smx_cond_t) cond); + SIMIX_cond_unref((smx_cond_t) cond); } /***** barrier related functions *****/