1 /* Copyright (c) 2021-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SRC_KERNEL_TIMER_TIMER_HPP_
7 #define SRC_KERNEL_TIMER_TIMER_HPP_
9 #include <simgrid/forward.h>
10 #include <xbt/functional.hpp>
11 #include <xbt/utility.hpp>
13 #include <boost/heap/fibonacci_heap.hpp>
15 namespace simgrid::kernel::timer {
17 inline auto& kernel_timers() // avoid static initialization order fiasco
19 using TimerQelt = std::pair<double, Timer*>;
20 static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> value;
24 /** @brief Timer datatype */
27 xbt::Task<void()> callback;
28 std::remove_reference_t<decltype(kernel_timers())>::handle_type handle_;
31 double get_date() const { return date_; }
33 Timer(double date, xbt::Task<void()>&& callback) : date_(date), callback(std::move(callback)) {}
37 template <class F> static inline Timer* set(double date, F callback)
39 return set(date, xbt::Task<void()>(std::move(callback)));
42 static Timer* set(double date, xbt::Task<void()>&& callback);
43 static double next() { return kernel_timers().empty() ? -1.0 : kernel_timers().top().first; }
45 /** Handle any pending timer. Returns if something was actually run. */
46 static bool execute_all();
49 } // namespace simgrid::kernel::timer
51 #endif /* SRC_KERNEL_TIMER_TIMER_HPP_ */