Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use a fifo instead of a swag of sleeping processes on a semaphore (so that waitany...
authorpini <pini@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Fri, 9 Jul 2010 12:54:35 +0000 (12:54 +0000)
committerpini <pini@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Fri, 9 Jul 2010 12:54:35 +0000 (12:54 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8005 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/include/simix/datatypes.h
src/simix/private.h
src/simix/smx_process.c
src/simix/smx_synchro.c
src/xbt/xbt_sg_synchro.c

index 399b101..d318db2 100644 (file)
@@ -31,9 +31,25 @@ SG_BEGIN_DECL()
 
 
 /* ******************************** Syncro ************************************ */
 
 
 /* ******************************** 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;
 
 /********************************** Action *************************************/
      typedef struct s_smx_action *smx_action_t;
index 6cf1c9a..0a3e27c 100644 (file)
@@ -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);
 
 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 */
 /******************************* Networking ***********************************/
 
 /** @brief Rendez-vous point datatype */
index 97296a1..3d080f2 100644 (file)
@@ -164,7 +164,7 @@ void SIMIX_process_kill(smx_process_t process)
     }
 
     if (process->sem) {
     }
 
     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);
 
       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) {
     }
 
     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);
 
       if (process->waiting_action) {
        SIMIX_unregister_action_to_semaphore(process->waiting_action, process->sem);
index 8cee3bc..52a435d 100644 (file)
@@ -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);
 /** @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;
   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;
 
   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");
       "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)))
 
   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) {
  * 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++;
     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) {
  * 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);
 
   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;
     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;
 
   /* 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)
   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) {
   /* 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;
   }
   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) {
 
   /* 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;
     }
   }
       result = counter;
     }
   }
index 0dcca4c..3d148a8 100644 (file)
@@ -127,12 +127,7 @@ void xbt_thread_yield(void) {
 
 /****** mutex related functions ******/
 struct s_xbt_mutex_ {
 
 /****** 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)
 };
 
 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_ {
 
 /***** 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)
 };
 
 xbt_cond_t xbt_cond_init(void)