Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / s4u / actions-comm / s4u_actions-comm.cpp
index cc77351..3e14881 100644 (file)
@@ -3,6 +3,8 @@
 /* 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. */
 
 /* 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. */
 
+#include <mutex>
+
 #include "simgrid/msg.h"
 #include "simgrid/simix.h"      /* semaphores for the barrier */
 #include <xbt/replay.h>
 #include "simgrid/msg.h"
 #include "simgrid/simix.h"      /* semaphores for the barrier */
 #include <xbt/replay.h>
@@ -55,10 +57,12 @@ static void asynchronous_cleanup(void)
   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
 
   /* Destroy any isend which correspond to completed communications */
   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
 
   /* Destroy any isend which correspond to completed communications */
-  int found;
   msg_comm_t comm;
   msg_comm_t comm;
-  while ((found = MSG_comm_testany(globals->isends)) != -1) {
-    xbt_dynar_remove_at(globals->isends, found, &comm);
+  while (true) {
+    int pos_found = MSG_comm_testany(globals->isends);
+    if (pos_found == -1) /* none remaining */
+      break;
+    xbt_dynar_remove_at(globals->isends, pos_found, &comm);
     MSG_comm_destroy(comm);
   }
 }
     MSG_comm_destroy(comm);
   }
 }
@@ -170,13 +174,16 @@ static void action_barrier(const char *const *action)
     processes_arrived_sofar = 0;
   }
   ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
     processes_arrived_sofar = 0;
   }
   ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
-  mutex->lock();
-  if (++processes_arrived_sofar == communicator_size) {
-    cond->notify_all();
-    mutex->unlock();
-  } else {
-    cond->wait(mutex);
-    mutex->unlock();
+
+  {
+    std::unique_lock<simgrid::s4u::Mutex> lock(*mutex);
+    if (++processes_arrived_sofar == communicator_size) {
+      // We can notify without the lock:
+      lock.unlock();
+      cond->notify_all();
+    } else {
+      cond->wait(lock);
+    }
   }
 
   ACT_DEBUG("Exiting barrier: %s", NAME);
   }
 
   ACT_DEBUG("Exiting barrier: %s", NAME);