From: pini Date: Fri, 9 Jul 2010 12:54:35 +0000 (+0000) Subject: Use a fifo instead of a swag of sleeping processes on a semaphore (so that waitany... X-Git-Tag: v3_5~813 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/74f69c4435c448239fe6d8d040f95fb8534db7f9?ds=sidebyside Use a fifo instead of a swag of sleeping processes on a semaphore (so that waitany actually works). git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8005 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/include/simix/datatypes.h b/src/include/simix/datatypes.h index 399b101069..d318db2691 100644 --- a/src/include/simix/datatypes.h +++ b/src/include/simix/datatypes.h @@ -31,9 +31,25 @@ SG_BEGIN_DECL() /* ******************************** Syncro ************************************ */ - typedef struct s_smx_mutex *smx_mutex_t; - typedef struct s_smx_cond *smx_cond_t; - typedef struct s_smx_sem *smx_sem_t; + typedef struct s_smx_mutex { + xbt_swag_t sleeping; /* list of sleeping process */ + int refcount; + } s_smx_mutex_t; + typedef s_smx_mutex_t *smx_mutex_t; + + typedef struct s_smx_cond { + xbt_swag_t sleeping; /* list of sleeping process */ + smx_mutex_t mutex; + xbt_fifo_t actions; /* list of actions */ + } s_smx_cond_t; + typedef s_smx_cond_t *smx_cond_t; + + typedef struct s_smx_sem { + xbt_fifo_t sleeping; /* list of sleeping process */ + int capacity; + xbt_fifo_t actions; /* list of actions */ + } s_smx_sem_t; + typedef s_smx_sem_t *smx_sem_t; /********************************** Action *************************************/ typedef struct s_smx_action *smx_action_t; diff --git a/src/simix/private.h b/src/simix/private.h index 6cf1c9a1c9..0a3e27ca68 100644 --- a/src/simix/private.h +++ b/src/simix/private.h @@ -95,36 +95,6 @@ void SIMIX_process_schedule(smx_process_t process); ex_ctx_t *SIMIX_process_get_exception(void); void SIMIX_process_exception_terminate(xbt_ex_t * e); -/*************************** Mutex and Conditional ****************************/ - -typedef struct s_smx_mutex { - - /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */ - xbt_swag_t sleeping; /* list of sleeping process */ - int refcount; - /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */ - -} s_smx_mutex_t; - -typedef struct s_smx_cond { - - /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */ - xbt_swag_t sleeping; /* list of sleeping process */ - smx_mutex_t mutex; - xbt_fifo_t actions; /* list of actions */ - /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */ - -} s_smx_cond_t; - -typedef struct s_smx_sem { - /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_sem */ - xbt_swag_t sleeping; /* list of sleeping process */ - int capacity; - xbt_fifo_t actions; /* list of actions */ - /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_sem */ - -} s_smx_sem_t; - /******************************* Networking ***********************************/ /** @brief Rendez-vous point datatype */ diff --git a/src/simix/smx_process.c b/src/simix/smx_process.c index 97296a158d..3d080f26bf 100644 --- a/src/simix/smx_process.c +++ b/src/simix/smx_process.c @@ -164,7 +164,7 @@ void SIMIX_process_kill(smx_process_t process) } if (process->sem) { - xbt_swag_remove(process, process->sem->sleeping); + xbt_fifo_remove(process->sem->sleeping, process); if (process->waiting_action) { SIMIX_unregister_action_to_semaphore(process->waiting_action, process->sem); @@ -196,7 +196,7 @@ void SIMIX_process_kill(smx_process_t process) } if (process->sem) { - xbt_swag_remove(process, process->sem->sleeping); + xbt_fifo_remove(process->sem->sleeping, process); if (process->waiting_action) { SIMIX_unregister_action_to_semaphore(process->waiting_action, process->sem); diff --git a/src/simix/smx_synchro.c b/src/simix/smx_synchro.c index 8cee3bc822..52a435dc4a 100644 --- a/src/simix/smx_synchro.c +++ b/src/simix/smx_synchro.c @@ -340,9 +340,8 @@ void SIMIX_cond_display_info(smx_cond_t cond) /** @brief Initialize a semaphore */ smx_sem_t SIMIX_sem_init(int capacity) { smx_sem_t sem = xbt_new0(s_smx_sem_t, 1); - s_smx_process_t p; - sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup)); + sem->sleeping = xbt_fifo_new(); sem->actions = xbt_fifo_new(); sem->capacity = capacity; return sem; @@ -354,9 +353,9 @@ void SIMIX_sem_destroy(smx_sem_t sem) { if (sem == NULL) return; - xbt_assert0(xbt_swag_size(sem->sleeping) == 0, + xbt_assert0(xbt_fifo_size(sem->sleeping) == 0, "Cannot destroy semaphore since someone is still using it"); - xbt_swag_free(sem->sleeping); + xbt_fifo_free(sem->sleeping); DEBUG1("%d actions registered", xbt_fifo_size(sem->actions)); while((action=xbt_fifo_pop(sem->actions))) @@ -372,9 +371,10 @@ void SIMIX_sem_destroy(smx_sem_t sem) { * If no one was blocked, the semaphore capacity is increased by 1. * */ void SIMIX_sem_release(smx_sem_t sem) { - DEBUG1("Sem release semaphore %p", sem); - if (xbt_swag_size(sem->sleeping) >= 1) { - smx_process_t proc = xbt_swag_extract(sem->sleeping); + smx_process_t proc; + + DEBUG1("Sem release semaphore %p", sem); + if ((proc = xbt_fifo_shift(sem->sleeping)) != NULL) { xbt_swag_insert(proc, simix_global->process_to_run); } else if (sem->capacity != SMX_SEM_NOLIMIT) { sem->capacity++; @@ -393,12 +393,10 @@ void SIMIX_sem_release(smx_sem_t sem) { * There is no way to reset the semaphore to a more regular state afterward. * */ void SIMIX_sem_release_forever(smx_sem_t sem) { - smx_process_t proc = NULL; - smx_process_t proc_next = NULL; + smx_process_t proc; DEBUG1("Broadcast semaphore %p", sem); - xbt_swag_foreach_safe(proc, proc_next, sem->sleeping) { - xbt_swag_remove(proc, sem->sleeping); + while ((proc = xbt_fifo_shift(sem->sleeping)) != NULL) { xbt_swag_insert(proc, simix_global->process_to_run); } sem->capacity = SMX_SEM_NOLIMIT; @@ -419,7 +417,7 @@ void SIMIX_sem_block_onto(smx_sem_t sem) { /* process status */ self->sem = sem; - xbt_swag_insert(self, sem->sleeping); + xbt_fifo_push (sem->sleeping, self); SIMIX_process_yield(); self->sem = NULL; while (self->suspended) @@ -551,7 +549,7 @@ unsigned int SIMIX_sem_acquire_any(xbt_dynar_t sems) { /* Get listed as member of all the provided semaphores */ self->sem = (smx_sem_t)sems; /* FIXME: we pass a pointer to dynar where a pointer to sem is expected...*/ xbt_dynar_foreach(sems,counter,sem) { - xbt_swag_insert(self, sem->sleeping); + xbt_fifo_push(sem->sleeping, self); } SIMIX_process_yield(); self->sem = NULL; @@ -560,10 +558,8 @@ unsigned int SIMIX_sem_acquire_any(xbt_dynar_t sems) { /* one of the semaphore unsuspended us -- great, let's search which one (and get out of the others) */ xbt_dynar_foreach(sems,counter,sem) { - if (xbt_swag_belongs(self,sem->sleeping)) - xbt_swag_remove(self,sem->sleeping); - else { - xbt_assert0(result==-1,"More than one semaphore unlocked us. Dunno what to do"); + if (!xbt_fifo_remove(sem->sleeping, self)) { + xbt_assert1(result == -1, "You're trying to wait more than once on semaphore %p, don't you ?", sem); result = counter; } } diff --git a/src/xbt/xbt_sg_synchro.c b/src/xbt/xbt_sg_synchro.c index 0dcca4cbe2..3d148a8128 100644 --- a/src/xbt/xbt_sg_synchro.c +++ b/src/xbt/xbt_sg_synchro.c @@ -127,12 +127,7 @@ void xbt_thread_yield(void) { /****** mutex related functions ******/ struct s_xbt_mutex_ { - - /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */ - xbt_swag_t sleeping; /* list of sleeping process */ - int refcount; - /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */ - + s_smx_mutex_t mutex; }; xbt_mutex_t xbt_mutex_init(void) @@ -157,13 +152,7 @@ void xbt_mutex_destroy(xbt_mutex_t mutex) /***** condition related functions *****/ struct s_xbt_cond_ { - - /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */ - xbt_swag_t sleeping; /* list of sleeping process */ - smx_mutex_t mutex; - xbt_fifo_t actions; /* list of actions */ - /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */ - + s_smx_cond_t cond; }; xbt_cond_t xbt_cond_init(void)