From bdaf16e4bbff9c60cd8da2800fcad02e4a74daa4 Mon Sep 17 00:00:00 2001 From: mquinson Date: Tue, 23 Nov 2010 20:54:33 +0000 Subject: [PATCH] stop that misuse of semaphores and use conditions where they are expected (ok, Arnaud, you won that battle, but not the war) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8628 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- examples/msg/actions/actions.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/examples/msg/actions/actions.c b/examples/msg/actions/actions.c index b8eb96aa0c..d8439fb34d 100644 --- a/examples/msg/actions/actions.c +++ b/examples/msg/actions/actions.c @@ -182,28 +182,41 @@ static void action_wait(xbt_dynar_t action) } /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */ -smx_sem_t barrier_semaphore = NULL; static void barrier(xbt_dynar_t action) { char *name = NULL; + static smx_mutex_t mutex = NULL; + static smx_cond_t cond = NULL; + static int processes_arrived_sofar=0; if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) name = xbt_str_join(action, " "); - if (barrier_semaphore == NULL) // first arriving on the barrier - barrier_semaphore = SIMIX_sem_init(0); - DEBUG2("Entering barrier: %s (capacity: %d)", name,SIMIX_sem_get_capacity(barrier_semaphore)); + if (mutex == NULL) { // first arriving on the barrier + mutex = SIMIX_mutex_init(); + cond = SIMIX_cond_init(); + processes_arrived_sofar=0; + } + DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar); - if (SIMIX_sem_get_capacity(barrier_semaphore) == -communicator_size + 1) { // last arriving - SIMIX_sem_release_forever(barrier_semaphore); - SIMIX_sem_destroy(barrier_semaphore); - barrier_semaphore = NULL; - } else { // not last - SIMIX_sem_acquire(barrier_semaphore); + SIMIX_mutex_lock(mutex); + if (++processes_arrived_sofar == communicator_size) { + SIMIX_cond_broadcast(cond); + SIMIX_mutex_unlock(mutex); + } else { + SIMIX_cond_wait(cond,mutex); + SIMIX_mutex_unlock(mutex); } DEBUG1("Exiting barrier: %s", name); + processes_arrived_sofar--; + if (!processes_arrived_sofar) { + SIMIX_cond_destroy(cond); + SIMIX_mutex_destroy(mutex); + mutex=NULL; + } + if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) free(name); -- 2.20.1