Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move kernel timers from simix:: to kernel::timer::
[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 public:
30   const double date;
31   std::remove_reference_t<decltype(kernel_timers())>::handle_type handle_;
32
33   Timer(double date, xbt::Task<void()>&& callback) : date(date), callback(std::move(callback)) {}
34
35   xbt::Task<void()> callback;
36   void remove();
37
38   template <class F> static inline Timer* set(double date, F callback)
39   {
40     return set(date, xbt::Task<void()>(std::move(callback)));
41   }
42
43   static Timer* set(double date, xbt::Task<void()>&& callback);
44   static double next() { return kernel_timers().empty() ? -1.0 : kernel_timers().top().first; }
45 };
46
47 } // namespace timer
48 } // namespace kernel
49 } // namespace simgrid
50
51 #endif /* SRC_KERNEL_TIMER_TIMER_HPP_ */