18 #include <sys/timerfd.h>
19 #include <sys/epoll.h>
24 #include <unordered_map>
57 #define TIMER_USE_TIMERFD_EPOLL
60 #if defined(TIMER_USE_SIGEV_THREAD) && defined(TIMER_USE_TIMERFD_EPOLL)
61 #error "Cannot define both TIMER_USE_SIGEV_THREAD and TIMER_USE_TIMERFD_EPOLL"
64 #if !defined(TIMER_USE_SIGEV_THREAD) && !defined(TIMER_USE_TIMERFD_EPOLL)
65 #define TIMER_USE_TIMERFD_EPOLL
153 #ifdef TIMER_USE_TIMERFD_EPOLL
167 #ifdef TIMER_USE_TIMERFD_EPOLL
175 std::thread mTimerThread;
186 static std::unordered_map<TimerId, TimerManager*> sTimerManagerMap;
189 static std::mutex sManagerMapMutex;
200 std::atomic<bool> mRunning{
true};
201 std::unordered_map<TimerId, TimerInfo> mTimers;
202 std::mutex mTimersMutex;
203 std::weak_ptr<SLLooper> mLooper;
204 std::atomic<TimerId> mNextId{1};
231 explicit TimerManager(std::weak_ptr<SLLooper> looper);
313 bool periodic, std::atomic<bool>* cancelled);
537 #ifdef TIMER_USE_TIMERFD_EPOLL
538 return "TIMERFD_EPOLL";
540 return "SIGEV_THREAD";
545 #ifdef TIMER_USE_TIMERFD_EPOLL
559 void timerThreadFunc();
568 int createTimerFd(uint64_t delay_ms,
bool periodic);
577 void updateTimerFd(
int fd, uint64_t delay_ms,
bool periodic);
593 timer_t createSigevTimer(
TimerId id);
602 void updateSigevTimer(timer_t timer, uint64_t delay_ms,
bool periodic);
613 static void sigevTimerCallback(sigval_t sv);
633 void handleTimerExpired(
const TimerInfo& timerInfo);
RAII timer wrapper with move semantics for safe timer management.
High-performance timer management with configurable backend.
TimerId createTimer(std::function< void()> callback, uint64_t delay_ms, bool periodic, std::atomic< bool > *cancelled)
Create a new timer.
size_t getActiveTimerCount()
Get the number of currently active timers.
void updateCancelledPtr(TimerId id, std::atomic< bool > *newPtr)
Update the cancellation flag pointer for an existing timer.
~TimerManager()
Destroy the TimerManager and cleanup all resources.
static const char * getBackendName()
Get the name of the currently compiled timer backend.
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.
bool cancelTimer(TimerId id)
Cancel an active timer.
Software Timer namespace containing all timer-related classes.
uint64_t TimerId
Unique identifier type for timer instances.
Internal timer information structure.
std::function< void()> callback
User callback function to execute on timer expiry.
TimerId id
Unique timer identifier.
int fd
timerfd file descriptor for Linux backend
uint64_t interval_ms
Timer interval in milliseconds.
std::atomic< bool > * cancelled
Pointer to cancellation flag (optional)
bool periodic
true for repeating timer, false for one-shot