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
SLLooper.h
Go to the documentation of this file.
1
9 #pragma once
10 #include <memory>
11 #include <thread>
12 #include <future>
13 #include <atomic>
14 #include <chrono>
15 #include <type_traits>
16 #include "Timer.h"
17 #include "EventQueue.h"
18 #include "Task.h"
19 #include "Awaitable.h"
20 #include "TimerManager.h"
21
22 namespace swt {
23 template<typename T> class Promise;
24
25 template<typename T> class WorkAwaitable;
26 class DelayAwaitable;
27 template<typename T> class PostAwaitable;
28
82 class SLLooper : public std::enable_shared_from_this<SLLooper>
83 {
84 public:
93 SLLooper();
94
103 ~SLLooper();
104
138 std::shared_ptr<EventQueue> getEventQueue();
139
140 // ========== INTERNAL DEBUG API ==========
141
159 template<typename F, typename... Args>
160 auto post_internal(F&& func, Args&&... args, const char* file, int line, const char* funcname)
161 -> std::future<decltype(func(args...))>;
162
180 template<typename F, typename... Args>
181 auto postDelayed_internal(int64_t delayMs, F&& func, Args&&... args, const char* file, int line, const char* funcname)
182 -> std::future<decltype(func(args...))>;
183
184 // ========== PUBLIC ASYNC API ==========
185
215 template<typename F, typename... Args>
216 auto post(F&& func, Args&&... args) -> std::future<decltype(func(args...))>;
217
245 template<typename F, typename... Args>
246 auto postDelayed(int64_t delayMs, F&& func, Args&&... args) -> std::future<decltype(func(args...))>;
247
248 // ========== PROMISE API ==========
249
276 template<typename T>
278
279 // ========== CPU-BOUND TASK API ==========
280
308 template<typename Func>
309 auto postWork(Func&& func) -> swt::Promise<decltype(func())>;
310
337 template<typename Func>
338 auto postWork(Func&& func, std::chrono::milliseconds timeout)
339 -> swt::Promise<decltype(func())>;
340
341 // ========== COROUTINE API ==========
342
374 template<typename Func>
375 auto awaitWork(Func&& func) -> WorkAwaitable<decltype(func())> {
376 return WorkAwaitable<decltype(func())>(shared_from_this(), std::forward<Func>(func));
377 }
378
407 return DelayAwaitable(shared_from_this(), delayMs);
408 }
409
442 template<typename Func>
443 auto awaitPost(Func&& func) -> PostAwaitable<decltype(func())> {
444 return PostAwaitable<decltype(func())>(shared_from_this(), std::forward<Func>(func));
445 }
446
456 bool loop();
457
464 void exit();
465
466 // ========== TIMER API (Boost-style) ==========
467
472 static const char* getTimerBackend() {
474 }
475
499 Timer addTimer(std::function<void()> callback, uint64_t delay_ms);
500
525 template<typename Rep, typename Period>
526 Timer addTimer(std::function<void()> callback,
527 const std::chrono::duration<Rep, Period>& delay);
528
552 Timer addPeriodicTimer(std::function<void()> callback, uint64_t interval_ms);
553
576 template<typename Rep, typename Period>
577 Timer addPeriodicTimer(std::function<void()> callback,
578 const std::chrono::duration<Rep, Period>& interval);
579
580 // ========== CONVENIENCE METHODS ==========
581
607 template<typename Function>
608 auto postWithTimeout(Function&& func, uint64_t timeout_ms)
609 -> std::enable_if_t<std::is_void_v<std::invoke_result_t<Function>>, Timer>;
610
611 // ========== INTERNAL TIMER API ==========
612
627 TimerId createTimerInternal(std::function<void()> callback, uint64_t delay_ms,
628 bool periodic, std::atomic<bool>* cancelled);
629
639
648 bool hasTimerInternal(TimerId id);
649
659 bool restartTimerInternal(TimerId id, uint64_t delay_ms);
660
668 size_t getActiveTimerCount();
669
680
692 void updateTimerCancelledPtr(TimerId id, std::atomic<bool>* newPtr);
693
694 private:
695 std::shared_ptr<EventQueue> mEventQueue;
696 std::atomic<bool> mStarted;
697 std::thread t1;
698 std::unique_ptr<TimerManager> mTimerManager;
699 };
700 } // namespace swt
701
702 // Include template implementations
703 #include "SLLooper.tpp"
704 #include "Awaitable.h"
Direct awaitable types for coroutine integration with SLLooper.
Unified event queue supporting messages and functions with timed execution.
Coroutine-based Task implementation for asynchronous execution.
High-performance timer management using Linux timerfd+epoll or sigev_thread.
RAII timer wrapper with move semantics for safe timer management.
Awaitable for delay operations.
Definition Awaitable.h:208
Awaitable for executing function on main thread.
Definition Awaitable.h:275
Type-safe promise for asynchronous result handling with continuation chaining.
Definition Promise.h:68
Central event loop coordinator providing asynchronous task execution and timer management.
Definition SLLooper.h:83
void updateTimerCancelledPtr(TimerId id, std::atomic< bool > *newPtr)
Update timer cancellation pointer for moved Timer objects.
Definition SLLooper.cpp:181
bool restartTimerInternal(TimerId id, uint64_t delay_ms)
Restart existing timer with new delay.
Definition SLLooper.cpp:223
Timer addTimer(std::function< void()> callback, uint64_t delay_ms)
Add one-shot timer with millisecond precision.
Definition SLLooper.cpp:145
swt::Promise< T > createPromise()
Create a new promise object for manual result setting.
std::shared_ptr< EventQueue > getEventQueue()
Get access to the underlying event queue.
Definition SLLooper.cpp:82
auto awaitPost(Func &&func) -> PostAwaitable< decltype(func())>
Execute function on main thread (co_await version)
Definition SLLooper.h:443
DelayAwaitable awaitDelay(int delayMs)
Delay execution (co_await version)
Definition SLLooper.h:406
auto postDelayed(int64_t delayMs, F &&func, Args &&... args) -> std::future< decltype(func(args...))>
Post function for delayed asynchronous execution.
static const char * getTimerBackend()
Get timer backend name.
Definition SLLooper.h:472
auto postWork(Func &&func, std::chrono::milliseconds timeout) -> swt::Promise< decltype(func())>
Execute CPU-intensive task with timeout.
auto post(F &&func, Args &&... args) -> std::future< decltype(func(args...))>
Post function for immediate asynchronous execution.
~SLLooper()
Destructor - stops event loop and cleanup resources.
Definition SLLooper.cpp:51
auto postDelayed_internal(int64_t delayMs, F &&func, Args &&... args, const char *file, int line, const char *funcname) -> std::future< decltype(func(args...))>
Internal postDelayed function with debug info for CPU-bound detection.
bool cancelTimerInternal(TimerId id)
Internal timer cancellation method.
Definition SLLooper.cpp:207
Timer addPeriodicTimer(std::function< void()> callback, uint64_t interval_ms)
Add periodic timer with millisecond interval.
Definition SLLooper.cpp:163
bool loop()
Run one iteration of the event loop.
Definition SLLooper.cpp:86
Timer addPeriodicTimer(std::function< void()> callback, const std::chrono::duration< Rep, Period > &interval)
Add periodic timer with chrono interval.
auto awaitWork(Func &&func) -> WorkAwaitable< decltype(func())>
Execute function on background thread (co_await version)
Definition SLLooper.h:375
void exit()
Request event loop to exit.
Definition SLLooper.cpp:75
void initializeTimerManager()
Initialize TimerManager lazily.
Definition SLLooper.cpp:38
auto post_internal(F &&func, Args &&... args, const char *file, int line, const char *funcname) -> std::future< decltype(func(args...))>
Internal post function with debug info for CPU-bound detection.
TimerId createTimerInternal(std::function< void()> callback, uint64_t delay_ms, bool periodic, std::atomic< bool > *cancelled)
Internal timer creation method.
Definition SLLooper.cpp:190
size_t getActiveTimerCount()
Get count of active timers.
Definition SLLooper.cpp:235
SLLooper()
Constructor - initializes message queue and starts event loop.
Definition SLLooper.cpp:29
Timer addTimer(std::function< void()> callback, const std::chrono::duration< Rep, Period > &delay)
Add one-shot timer with chrono duration.
auto postWork(Func &&func) -> swt::Promise< decltype(func())>
Execute CPU-intensive task asynchronously.
bool hasTimerInternal(TimerId id)
Check if timer exists in internal management.
Definition SLLooper.cpp:218
auto postWithTimeout(Function &&func, uint64_t timeout_ms) -> std::enable_if_t< std::is_void_v< std::invoke_result_t< Function > >, Timer >
Post function with timeout, returns Timer for cancellation.
static const char * getBackendName()
Get the name of the currently compiled timer backend.
RAII timer wrapper with boost-style API and move semantics.
Definition Timer.h:68
Awaitable for executing work on background thread.
Definition Awaitable.h:43
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