Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert "use smart pointers for kernel timers"
[simgrid.git] / include / simgrid / kernel / Timer.hpp
1 /* Copyright (c) 2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SRC_KERNEL_TIMER_TIMER_HPP_
8 #define SRC_KERNEL_TIMER_TIMER_HPP_
9
10 #include <simgrid/forward.h>
11 #include <xbt/functional.hpp>
12 #include <xbt/utility.hpp>
13
14 #include <boost/heap/fibonacci_heap.hpp>
15
16 namespace simgrid {
17 namespace kernel {
18 namespace timer {
19
20 inline auto& kernel_timers() // avoid static initialization order fiasco
21 {
22   using TimerQelt = std::pair<double, Timer*>;
23   static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> value;
24   return value;
25 }
26
27 /** @brief Timer datatype */
28 class Timer {
29   const double date_;
30   xbt::Task<void()> callback;
31   std::remove_reference_t<decltype(kernel_timers())>::handle_type handle_;
32
33 public:
34   double get_date() const { return date_; }
35
36
37   Timer(double date, xbt::Task<void()>&& callback) : date_(date), callback(std::move(callback)) {}
38
39   void remove();
40
41   template <class F> static inline Timer* set(double date, F callback)
42   {
43     return set(date, xbt::Task<void()>(std::move(callback)));
44   }
45
46   static Timer* set(double date, xbt::Task<void()>&& callback);
47   static double next() { return kernel_timers().empty() ? -1.0 : kernel_timers().top().first; }
48
49   /** Handle any pending timer. Returns if something was actually run. */
50   static bool execute_all();
51 };
52
53 } // namespace timer
54 } // namespace kernel
55 } // namespace simgrid
56
57 #endif /* SRC_KERNEL_TIMER_TIMER_HPP_ */