Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fbinfer saw there another static initialization order fiasco.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 25 Jan 2021 12:14:49 +0000 (13:14 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 25 Jan 2021 13:49:10 +0000 (14:49 +0100)
include/simgrid/simix.hpp
src/simix/smx_global.cpp

index a75c594..dbafbfa 100644 (file)
@@ -96,15 +96,19 @@ template <class R, class F> R simcall_blocking(F&& code, mc::SimcallInspector* t
 namespace simgrid {
 namespace simix {
 
-using TimerQelt = std::pair<double, Timer*>;
-static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> simix_timers;
+inline auto& simix_timers() // avoid static initialization order fiasco
+{
+  using TimerQelt = std::pair<double, Timer*>;
+  static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> value;
+  return value;
+}
 
 /** @brief Timer datatype */
 class Timer {
   double date = 0.0;
 
 public:
-  decltype(simix_timers)::handle_type handle_;
+  std::remove_reference_t<decltype(simix_timers())>::handle_type handle_;
 
   Timer(double date, simgrid::xbt::Task<void()>&& callback) : date(date), callback(std::move(callback)) {}
 
@@ -118,7 +122,7 @@ public:
   }
 
   static Timer* set(double date, simgrid::xbt::Task<void()>&& callback);
-  static double next() { return simix_timers.empty() ? -1.0 : simix_timers.top().first; }
+  static double next() { return simix_timers().empty() ? -1.0 : simix_timers().top().first; }
 };
 
 } // namespace simix
index d0e91ad..f89e0db 100644 (file)
@@ -136,14 +136,14 @@ namespace simix {
 Timer* Timer::set(double date, xbt::Task<void()>&& callback)
 {
   auto* timer    = new Timer(date, std::move(callback));
-  timer->handle_ = simix_timers.emplace(std::make_pair(date, timer));
+  timer->handle_ = simix_timers().emplace(std::make_pair(date, timer));
   return timer;
 }
 
 /** @brief cancels a timer that was added earlier */
 void Timer::remove()
 {
-  simix_timers.erase(handle_);
+  simix_timers().erase(handle_);
   delete this;
 }
 
@@ -348,9 +348,9 @@ void SIMIX_clean()
   /* Exit the SIMIX network module */
   SIMIX_mailbox_exit();
 
-  while (not simgrid::simix::simix_timers.empty()) {
-    delete simgrid::simix::simix_timers.top().second;
-    simgrid::simix::simix_timers.pop();
+  while (not simgrid::simix::simix_timers().empty()) {
+    delete simgrid::simix::simix_timers().top().second;
+    simgrid::simix::simix_timers().pop();
   }
   /* Free the remaining data structures */
   simix_global->actors_to_run.clear();
@@ -394,11 +394,12 @@ double SIMIX_get_clock()
 static bool SIMIX_execute_timers()
 {
   bool result = false;
-  while (not simgrid::simix::simix_timers.empty() && SIMIX_get_clock() >= simgrid::simix::simix_timers.top().first) {
+  while (not simgrid::simix::simix_timers().empty() &&
+         SIMIX_get_clock() >= simgrid::simix::simix_timers().top().first) {
     result = true;
     // FIXME: make the timers being real callbacks (i.e. provide dispatchers that read and expand the args)
-    smx_timer_t timer = simgrid::simix::simix_timers.top().second;
-    simgrid::simix::simix_timers.pop();
+    smx_timer_t timer = simgrid::simix::simix_timers().top().second;
+    simgrid::simix::simix_timers().pop();
     timer->callback();
     delete timer;
   }