58 template<typename Callback> requires(
std::is_invocable_v<Callback>)
61 ttlet current_time = utc_nanoseconds(std::chrono::utc_clock::now());
62 auto callback_ptr = std::make_shared<callback_type>(std::forward<Callback>(callback));
65 ttlet lock = std::scoped_lock(mutex);
69 calculate_next_wakeup(current_time,
interval),
73 if (std::ssize(callback_list) == 1) {
74 start_with_lock_held();
79 (*callback_ptr)(current_time,
false);
88 static timer &global() noexcept
90 return *start_subsystem_or_terminate(_global,
nullptr, subsystem_init, subsystem_deinit);
94 struct callback_entry {
96 utc_nanoseconds next_wakeup;
101 utc_nanoseconds next_wakeup,
114 mutable unfair_mutex mutex;
117 size_t callback_count = 0;
125 find_triggered_callbacks(utc_nanoseconds current_time)
noexcept;
129 void loop(std::stop_token stop_token)
noexcept;
135 void start_with_lock_held() noexcept;
141 void stop_with_lock_held() noexcept;
143 [[nodiscard]] static utc_nanoseconds calculate_next_wakeup(utc_nanoseconds current_time,
std::chrono::nanoseconds interval) noexcept;
145 [[nodiscard]] static timer *subsystem_init() noexcept;
146 static
void subsystem_deinit() noexcept;
std::shared_ptr< callback_type > add_callback(std::chrono::nanoseconds interval, Callback callback, bool immediate=false) noexcept
Add a callback function to be executed each interval.
Definition timer.hpp:59