Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SIMIX_sem_would_block() was blatantly wrong; missing {} in SIMIX_sem_acquire_any...
[simgrid.git] / src / simix / smx_synchro.c
index 92ddf27..8cee3bc 100644 (file)
@@ -1,7 +1,5 @@
-/*     $Id$     */
-
-/* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
-   All rights reserved.                                          */
+/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -351,12 +349,11 @@ smx_sem_t SIMIX_sem_init(int capacity) {
 }
 /** @brief Destroys a semaphore */
 void SIMIX_sem_destroy(smx_sem_t sem) {
+  smx_action_t action = NULL;
   DEBUG1("Destroy semaphore %p", sem);
   if (sem == NULL)
     return;
 
-  smx_action_t action = NULL;
-
   xbt_assert0(xbt_swag_size(sem->sleeping) == 0,
       "Cannot destroy semaphore since someone is still using it");
   xbt_swag_free(sem->sleeping);
@@ -404,6 +401,7 @@ void SIMIX_sem_release_forever(smx_sem_t sem) {
     xbt_swag_remove(proc, sem->sleeping);
     xbt_swag_insert(proc, simix_global->process_to_run);
   }
+  sem->capacity = SMX_SEM_NOLIMIT;
 }
 
 /**
@@ -430,7 +428,7 @@ void SIMIX_sem_block_onto(smx_sem_t sem) {
 
 /** @brief Returns true if acquiring this semaphore would block */
 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem) {
-  return (sem->capacity>0);
+  return (sem->capacity<=0);
 }
 
 /** @brief Returns the current capacity of the semaphore
@@ -454,11 +452,14 @@ void SIMIX_sem_acquire(smx_sem_t sem) {
 
   DEBUG1("Wait semaphore %p", sem);
 
-  if (sem->capacity == SMX_SEM_NOLIMIT)
+  if (sem->capacity == SMX_SEM_NOLIMIT) {
+    DEBUG1("semaphore %p wide open", sem);
     return; /* don't even decrease it if wide open */
+  }
 
   /* If capacity sufficient, decrease it */
   if (sem->capacity>0) {
+    DEBUG1("semaphore %p has enough capacity", sem);
     sem->capacity--;
     return;
   }
@@ -531,12 +532,14 @@ unsigned int SIMIX_sem_acquire_any(xbt_dynar_t sems) {
 
   xbt_assert0(xbt_dynar_length(sems),
       "I refuse to commit sucide by locking on an **empty** set of semaphores!!");
-  DEBUG1("Wait on semaphore set %p", sems);
+  DEBUG2("Wait on semaphore set %p (containing %ld semaphores)", sems,xbt_dynar_length(sems));
 
   xbt_dynar_foreach(sems,counter,sem) {
-    if (!SIMIX_sem_would_block(sem))
+    if (!SIMIX_sem_would_block(sem)) {
+      DEBUG1("Semaphore %p wouldn't block; get it without waiting",sem);
       SIMIX_sem_acquire(sem);
-    return counter;
+      return counter;
+    }
   }
 
   /* Always create an action null in case there is a host failure */
@@ -551,11 +554,11 @@ unsigned int SIMIX_sem_acquire_any(xbt_dynar_t sems) {
     xbt_swag_insert(self, sem->sleeping);
   }
   SIMIX_process_yield();
+  self->sem = NULL;
   while (self->suspended)
     SIMIX_process_yield();
 
   /* one of the semaphore unsuspended us -- great, let's search which one (and get out of the others) */
-  self->sem = NULL;
   xbt_dynar_foreach(sems,counter,sem) {
     if (xbt_swag_belongs(self,sem->sleeping))
       xbt_swag_remove(self,sem->sleeping);