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)) {}
}
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
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;
}
/* 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();
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;
}