![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
FD-integrated timer utility (timerfd-backed). More...
#include <dfx-fdwatch/Timer.hpp>
Public Types | |
| enum class | Mode : bool { SingleShot , Repeating } |
| Timer firing mode. More... | |
| using | Callback = std::move_only_function<void ()> |
| Callback invoked when the timer expires. | |
Public Member Functions | |
| Timer () | |
| Constructs a detached (inactive) timer. | |
| Timer (Poller &poller, std::chrono::milliseconds timeout, Callback cb, Mode mode=Mode::SingleShot) | |
| Constructs and attaches a timer to a poller. | |
| DFX_PRIVATE_STATE_DECLARE_RULE_OF_5 (Timer) | |
| std::chrono::milliseconds | timeout () const noexcept |
| Returns the configured timeout/period. | |
| std::chrono::milliseconds | remainingTime () const |
| Returns the remaining time before the next expiration. If the timer is inactive, the returned duration is 0. | |
| Mode | mode () const noexcept |
| Returns the current mode. | |
| bool | isSingleShot () const noexcept |
| Convenience check for Mode::SingleShot. | |
| bool | isRepeating () const noexcept |
| Convenience check for Mode::Repeating. | |
| bool | isActive () const noexcept |
| Returns whether the timer is currently armed. A timer is considered active when it is registered in a Poller. | |
| void | attach (Poller &poller, std::chrono::milliseconds timeout, Callback cb, Mode mode=Mode::SingleShot) |
| Attaches the timer to a poller and configures it. | |
| void | start () |
| Arms the timer using the currently configured timeout. | |
| void | start (std::chrono::milliseconds timeout) |
| Sets a new timeout and arms the timer. | |
| void | stop () |
| Disarms the timer without releasing the underlying FD. | |
| void | close () noexcept |
| Closes the underlying timer resources and detaches from the poller. | |
FD-integrated timer utility (timerfd-backed).
Timer provides a simple API to schedule a callback after a given timeout. It integrates with Poller by registering an internal timer FD and dispatching the user callback when the timer expires.
A timer can be configured as:
A Timer can be default-constructed in a detached state and later configured via attach(). Alternatively, it can be constructed already attached to a poller.
The underlying timer FD is created lazily (implementation detail) and is not closed directly by stop(); either use close() to release the associated kernel resource explicitly or destroy it. It will also be close automatically after 30 seconds if start / attach hasn't been called in between.
Thread-safety is not guaranteed. All operations are expected to be performed from the same thread as the associated Poller event loop.
| using dfx::FdWatch::Timer::Callback = std::move_only_function<void ()> |
Callback invoked when the timer expires.
|
strong |
Timer firing mode.
| Enumerator | |
|---|---|
| SingleShot | Fire once, then automatically become inactive. |
| Repeating | Fire repeatedly at the configured interval until stopped. |
| dfx::FdWatch::Timer::Timer | ( | ) |
Constructs a detached (inactive) timer.
The timer is not associated with any poller and cannot be started until attach() has been called.
| dfx::FdWatch::Timer::Timer | ( | Poller & | poller, |
| std::chrono::milliseconds | timeout, | ||
| Callback | cb, | ||
| Mode | mode = Mode::SingleShot ) |
| void dfx::FdWatch::Timer::attach | ( | Poller & | poller, |
| std::chrono::milliseconds | timeout, | ||
| Callback | cb, | ||
| Mode | mode = Mode::SingleShot ) |
Attaches the timer to a poller and configures it.
This sets the poller, timeout, callback, and mode. The timer is not automatically started; call start() after attaching if you want it armed.
| poller | Poller used to monitor the timer FD. |
| timeout | Timeout/period to configure. Must be positive. |
| cb | Callback invoked upon expiration. |
| mode | Single-shot or repeating mode. |
|
noexcept |
Closes the underlying timer resources and detaches from the poller.
After this call, the timer is effectively reset to a detached state and must be attach()'d again before it can be started.
|
noexcept |
Returns whether the timer is currently armed. A timer is considered active when it is registered in a Poller.
|
noexcept |
Convenience check for Mode::Repeating.
|
noexcept |
Convenience check for Mode::SingleShot.
|
noexcept |
Returns the current mode.
| std::chrono::milliseconds dfx::FdWatch::Timer::remainingTime | ( | ) | const |
Returns the remaining time before the next expiration. If the timer is inactive, the returned duration is 0.
| void dfx::FdWatch::Timer::start | ( | ) |
| void dfx::FdWatch::Timer::start | ( | std::chrono::milliseconds | timeout | ) |
| void dfx::FdWatch::Timer::stop | ( | ) |
Disarms the timer without releasing the underlying FD.
After this call, the timer becomes inactive and will not fire until started again. This does not necessarily close or destroy the internal timer FD; use close() if you want to release kernel resources.
|
noexcept |
Returns the configured timeout/period.