Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
prepare transition of context_factory: make it private
[simgrid.git] / src / include / xbt / parmap.hpp
index 58b59cd..ae03c0c 100644 (file)
@@ -1,6 +1,6 @@
 /* A thread pool (C++ version).                                             */
 
-/* Copyright (c) 2004-2020 The SimGrid Team. All rights reserved.           */
+/* Copyright (c) 2004-2021 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. */
@@ -167,7 +167,7 @@ template <typename T> Parmap<T>::Parmap(unsigned num_workers, e_xbt_parmap_mode_
   this->workers[0] = nullptr;
 
   for (unsigned i = 1; i < num_workers; i++) {
-    ThreadData* data = new ThreadData(*this, i);
+    auto* data       = new ThreadData(*this, i);
     this->workers[i] = new std::thread(worker_main, data);
 
     /* Bind the worker to a core if possible */
@@ -288,13 +288,14 @@ template <typename T> void Parmap<T>::worker_main(ThreadData* data)
 {
   Parmap<T>& parmap     = data->parmap;
   unsigned round        = 0;
-  kernel::context::Context* context = simix_global->context_factory->create_context(std::function<void()>(), nullptr);
+  kernel::context::Context* context =
+      simix_global->get_context_factory()->create_context(std::function<void()>(), nullptr);
   kernel::context::Context::set_current(context);
 
   XBT_CDEBUG(xbt_parmap, "New worker thread created");
 
   /* Worker's main loop */
-  while (1) {
+  while (true) {
     round++; // New scheduling round
     parmap.synchro->worker_wait(round);
     if (parmap.destroying)
@@ -322,10 +323,8 @@ template <typename T> void Parmap<T>::PosixSynchro::master_signal()
 template <typename T> void Parmap<T>::PosixSynchro::master_wait()
 {
   std::unique_lock<std::mutex> lk(done_mutex);
-  while (this->parmap.thread_counter < this->parmap.num_workers) {
-    /* wait for all workers to be ready */
-    done_cond.wait(lk);
-  }
+  /* wait for all workers to be ready */
+  done_cond.wait(lk, [this]() { return this->parmap.thread_counter >= this->parmap.num_workers; });
 }
 
 template <typename T> void Parmap<T>::PosixSynchro::worker_signal()
@@ -342,9 +341,7 @@ template <typename T> void Parmap<T>::PosixSynchro::worker_wait(unsigned round)
 {
   std::unique_lock<std::mutex> lk(ready_mutex);
   /* wait for more work */
-  while (this->parmap.work_round != round) {
-    ready_cond.wait(lk);
-  }
+  ready_cond.wait(lk, [this, round]() { return this->parmap.work_round == round; });
 }
 
 #if HAVE_FUTEX_H