SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
Loading...
Searching...
No Matches
TimerManager.h
Go to the documentation of this file.
1
15 #pragma once
16
17 #include "Timer.h"
18 #include <sys/timerfd.h>
19 #include <sys/epoll.h>
20 #include <unistd.h>
21 #include <signal.h>
22 #include <time.h>
23 #include <functional>
24 #include <unordered_map>
25 #include <thread>
26 #include <atomic>
27 #include <memory>
28 #include <mutex>
29 #include <stdexcept>
30
53 // ========== Timer Backend Selection ==========
54 // Uncomment ONE of the following lines to select timer backend:
55
56 //#define TIMER_USE_SIGEV_THREAD ///< Use timer_create + sigev_thread
57 #define TIMER_USE_TIMERFD_EPOLL
58
59 // Validate macro selection
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"
62 #endif
63
64 #if !defined(TIMER_USE_SIGEV_THREAD) && !defined(TIMER_USE_TIMERFD_EPOLL)
65 #define TIMER_USE_TIMERFD_EPOLL
66 #endif
67
// end of TimerBackendMacros group
69
74 namespace swt {
75
76 // Forward declaration
77 class SLLooper;
78
84 using TimerId = uint64_t;
85
138 public:
152 struct TimerInfo {
153 #ifdef TIMER_USE_TIMERFD_EPOLL
154 int fd;
155 #else
156 timer_t timer;
157 TimerId timerId;
158 #endif
159 std::function<void()> callback;
160 bool periodic;
161 uint64_t interval_ms;
163 std::atomic<bool>* cancelled;
164 };
165
166 private:
167 #ifdef TIMER_USE_TIMERFD_EPOLL
174 int mEpollFd;
175 std::thread mTimerThread;
178 #else
186 static std::unordered_map<TimerId, TimerManager*> sTimerManagerMap;
187
189 static std::mutex sManagerMapMutex;
190
192 #endif
193
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};
208 public:
231 explicit TimerManager(std::weak_ptr<SLLooper> looper);
232
254
312 TimerId createTimer(std::function<void()> callback, uint64_t delay_ms,
313 bool periodic, std::atomic<bool>* cancelled);
314
356 bool cancelTimer(TimerId id);
357
389 bool hasTimer(TimerId id);
390
430 bool restartTimer(TimerId id, uint64_t delay_ms);
431
471 void updateCancelledPtr(TimerId id, std::atomic<bool>* newPtr);
472
508 size_t getActiveTimerCount();
509
536 static const char* getBackendName() {
537 #ifdef TIMER_USE_TIMERFD_EPOLL
538 return "TIMERFD_EPOLL";
539 #else
540 return "SIGEV_THREAD";
541 #endif
542 }
543
544 private:
545 #ifdef TIMER_USE_TIMERFD_EPOLL
559 void timerThreadFunc();
560
568 int createTimerFd(uint64_t delay_ms, bool periodic);
569
577 void updateTimerFd(int fd, uint64_t delay_ms, bool periodic);
578
580 #else
593 timer_t createSigevTimer(TimerId id);
594
602 void updateSigevTimer(timer_t timer, uint64_t delay_ms, bool periodic);
603
613 static void sigevTimerCallback(sigval_t sv);
614
616 #endif
617
633 void handleTimerExpired(const TimerInfo& timerInfo);
634
644 void cleanupTimer(TimerId id);
645
647 };
648
649 } // namespace swt
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.
Definition Awaitable.h:21
uint64_t TimerId
Unique identifier type for timer instances.
Definition Timer.h:25
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