Logo AND Algorithmique Numérique Distribuée

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