15#ifndef CPPNET_TIMERS_HPP
16#define CPPNET_TIMERS_HPP
18#include "net/detail/concepts.hpp"
29static constexpr timer_id INVALID_TIMER = -1;
33using clock = std::chrono::steady_clock;
71 const event_ref &rhs) -> std::strong_ordering;
98template <InterruptSource Interrupt>
117 template <InterruptSource I>
141 template <
class Rep,
class Period>
155 std::uint64_t period = 0) ->
timer_id;
185 template <
typename T>
186 using minheap = std::priority_queue<T, std::vector<T>, std::greater<>>;
199 mutable std::mutex mtx_;
204#include "impl/timers_impl.hpp"
Provides event-loop timers.
Definition timers.hpp:99
auto add(timestamp when, handler_t handler, duration period=duration::zero()) -> timer_id
Add a new timer.
friend auto swap(timers< I > &lhs, timers< I > &rhs) noexcept -> void
Swap function.
auto resolve() -> duration
Resolves all expired event handles.
auto operator=(const timers &other)=delete
Deleted copy assignment.
~timers()=default
Default destructor.
minheap< detail::event_ref > eventq
The minheap that stores timeouts.
Definition timers.hpp:193
auto add(std::uint64_t when, handler_t handler, std::uint64_t period=0) -> timer_id
Overloaded add function that uses a uint64_t instead of a time_point for the first timeout and the pe...
std::stack< timer_id > free_ids
A pool of recyclable timer_ids.
Definition timers.hpp:195
timers(timers &&other) noexcept
Move constructor.
auto add(std::chrono::duration< Rep, Period > when, handler_t handler, duration period=duration::zero()) -> timer_id
Overloaded add function that uses a std::chrono::duration instead of a time_point for the first timeo...
auto remove(timer_id tid) noexcept -> timer_id
Removes the timer with the given id.
timers(const timers &other)=delete
Deleted copy constructor.
std::deque< detail::event > events
The vector that holds all active events.
Definition timers.hpp:191
auto operator=(timers &&other) noexcept -> timers &
Move assignment.
timers()=default
Default constructor.
This file declares interrupt related types..
auto operator==(const event_ref &lhs, const event_ref &rhs) -> bool
An equality operator to determine event_ref ordering.
auto operator<=>(const event_ref &lhs, const event_ref &rhs) -> std::strong_ordering
The spaceship operator to determine event_ref ordering.
This namespace is for timers and interrupts.
Definition interrupt.hpp:25
std::chrono::microseconds duration
duration type.
Definition timers.hpp:37
std::size_t timer_id
timer_id type.
Definition timers.hpp:27
std::function< void(timer_id)> handler_t
handler type.
Definition timers.hpp:31
std::chrono::time_point< clock > timestamp
time type.
Definition timers.hpp:35
std::chrono::steady_clock clock
clock type.
Definition timers.hpp:33
event_ref to be inserted into the priority queue.
Definition timers.hpp:56
timestamp expires_at
The timer expiry time.
Definition timers.hpp:58
The event structure.
Definition timers.hpp:42
duration period
The timer period.
Definition timers.hpp:50
timestamp start
The first time the event fired.
Definition timers.hpp:48
std::atomic_flag armed
A flag to determine if the timer is armed.
Definition timers.hpp:52
handler_t handler
An event handler.
Definition timers.hpp:44
An interrupt is an immediately run timer event.
Definition interrupt.hpp:48