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