SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
|
High-performance timer management with configurable backend. More...
#include <TimerManager.h>
Classes | |
struct | TimerInfo |
Internal timer information structure. More... | |
Public Member Functions | |
TimerManager (std::weak_ptr< SLLooper > looper) | |
Construct a new TimerManager. | |
~TimerManager () | |
Destroy the TimerManager and cleanup all resources. | |
TimerId | createTimer (std::function< void()> callback, uint64_t delay_ms, bool periodic, std::atomic< bool > *cancelled) |
Create a new timer. | |
bool | cancelTimer (TimerId id) |
Cancel an active timer. | |
bool | hasTimer (TimerId id) |
Check if a timer exists and is active. | |
bool | restartTimer (TimerId id, uint64_t delay_ms) |
Restart an existing timer with new delay. | |
void | updateCancelledPtr (TimerId id, std::atomic< bool > *newPtr) |
Update the cancellation flag pointer for an existing timer. | |
size_t | getActiveTimerCount () |
Get the number of currently active timers. | |
Static Public Member Functions | |
static const char * | getBackendName () |
Get the name of the currently compiled timer backend. | |
High-performance timer management with configurable backend.
TimerManager provides a unified interface for managing timers with two different backend implementations:
TIMERFD_EPOLL Backend (Linux):
SIGEV_THREAD Backend (POSIX):
Definition at line 137 of file TimerManager.h.
|
explicit |
Construct a new TimerManager.
looper | Weak reference to parent SLLooper for callback posting |
Initializes the timer management system with the compile-time selected backend. For TIMERFD_EPOLL, creates epoll instance and starts timer thread. For SIGEV_THREAD, performs minimal initialization.
std::runtime_error | If backend initialization fails (e.g., epoll_create fails) |
Definition at line 17 of file TimerManager.cpp.
References getBackendName(), TIMER_DEBUG, TIMER_DEBUG_STREAM, and TIMER_ERROR_STREAM.
swt::TimerManager::~TimerManager | ( | ) |
Destroy the TimerManager and cleanup all resources.
Safely shuts down the timer system by:
All timer callbacks in progress will complete before destruction finishes.
Definition at line 41 of file TimerManager.cpp.
References TIMER_DEBUG, TIMER_DEBUG_STREAM, and TIMER_INFO.
bool swt::TimerManager::cancelTimer | ( | TimerId | id | ) |
Cancel an active timer.
id | Timer ID returned by createTimer() |
Immediately stops the specified timer and removes it from the system. If the timer callback is currently executing, this method will wait for it to complete before returning.
After cancellation, the timer ID becomes invalid and should not be used.
Definition at line 416 of file TimerManager.cpp.
References getBackendName(), and TIMER_DEBUG_STREAM.
TimerId swt::TimerManager::createTimer | ( | std::function< void()> | callback, |
uint64_t | delay_ms, | ||
bool | periodic, | ||
std::atomic< bool > * | cancelled | ||
) |
Create a new timer.
callback | Function to call when timer expires (must not be null) |
delay_ms | Delay in milliseconds before first expiration (must be > 0) |
periodic | true for repeating timer, false for one-shot timer |
cancelled | Optional pointer to atomic bool for external cancellation |
Creates and starts a new timer that will call the provided callback after the specified delay. For periodic timers, the callback will be called repeatedly at the specified interval until cancelled.
The callback will be executed on the SLLooper main thread, ensuring thread safety with other event loop operations.
Definition at line 77 of file TimerManager.cpp.
References swt::TimerManager::TimerInfo::callback, swt::TimerManager::TimerInfo::cancelled, swt::TimerManager::TimerInfo::fd, getBackendName(), swt::TimerManager::TimerInfo::id, swt::TimerManager::TimerInfo::interval_ms, swt::TimerManager::TimerInfo::periodic, TIMER_DEBUG_STREAM, TIMER_ERROR, and TIMER_ERROR_STREAM.
size_t swt::TimerManager::getActiveTimerCount | ( | ) |
Get the number of currently active timers.
Returns the current count of active timers. This includes both one-shot and periodic timers that have not yet been cancelled or expired.
Definition at line 478 of file TimerManager.cpp.
|
inlinestatic |
Get the name of the currently compiled timer backend.
Returns a string identifying which timer backend was selected at compile time. This is useful for logging, debugging, and runtime feature detection.
Definition at line 536 of file TimerManager.h.
Referenced by cancelTimer(), createTimer(), swt::SLLooper::getTimerBackend(), restartTimer(), and TimerManager().
bool swt::TimerManager::hasTimer | ( | TimerId | id | ) |
Check if a timer exists and is active.
id | Timer ID to check |
Queries whether the specified timer ID corresponds to an active timer. This is useful for checking timer status before performing operations.
Definition at line 442 of file TimerManager.cpp.
References TIMER_DEBUG_STREAM.
bool swt::TimerManager::restartTimer | ( | TimerId | id, |
uint64_t | delay_ms | ||
) |
Restart an existing timer with new delay.
id | Timer ID to restart |
delay_ms | New delay in milliseconds |
Resets an existing timer to fire after the new specified delay. The timer becomes a one-shot timer regardless of its original periodic setting. If the timer was cancelled via external flag, the cancellation is reset.
This operation is more efficient than cancelling and creating a new timer as it reuses the existing timer resources.
Definition at line 449 of file TimerManager.cpp.
References getBackendName(), and TIMER_DEBUG_STREAM.
void swt::TimerManager::updateCancelledPtr | ( | TimerId | id, |
std::atomic< bool > * | newPtr | ||
) |
Update the cancellation flag pointer for an existing timer.
id | Timer ID to update |
newPtr | New pointer to atomic bool cancellation flag |
Updates the cancellation flag pointer for an existing timer. This allows changing the external cancellation mechanism after timer creation.
The new pointer will be checked before each callback execution. Pass nullptr to disable external cancellation for the timer.
Definition at line 483 of file TimerManager.cpp.
References TIMER_DEBUG_STREAM.