32 mEventQueue = std::make_shared<EventQueue>();
42 mTimerManager = std::make_unique<TimerManager>(weak_from_this());
44 }
catch (
const std::exception& e) {
57 mTimerManager.reset();
61 if (std::this_thread::get_id() != t1.get_id()) {
68 }
catch (
const std::exception& e) {
93 while (mStarted.load()) {
95 auto item = mEventQueue->pollNext();
101 if (mEventQueue->isQuit()) {
109 <<
", type: " << (
int)item->type <<
")");
111 auto& itemRef = item.value();
114 if (itemRef.message && itemRef.message->mHandler) {
115 itemRef.message->mHandler->dispatchMessage(itemRef.message);
119 if (itemRef.task.valid()) {
123 }
catch (
const std::exception& e) {
129 std::this_thread::sleep_for(std::chrono::milliseconds(1));
134 }
catch (
const std::exception& e) {
138 mStarted.store(
false);
149 return Timer(0, weak_from_this());
152 Timer timer(0, weak_from_this());
167 return Timer(0, weak_from_this());
170 Timer timer(0, weak_from_this());
184 mTimerManager->updateCancelledPtr(
id, newPtr);
186 SLLOOPER_ERROR(
"TimerManager is null in updateTimerCancelledPtr!");
191 bool periodic, std::atomic<bool>* cancelled) {
193 <<
"ms, periodic: " << periodic);
194 if (!mTimerManager) {
198 if (!mTimerManager) {
202 TimerId id = mTimerManager->createTimer(std::move(callback), delay_ms, periodic, cancelled);
209 if (!mTimerManager) {
213 bool result = mTimerManager->cancelTimer(
id);
219 if (!mTimerManager)
return false;
220 return mTimerManager->hasTimer(
id);
225 <<
" with delay " << delay_ms <<
"ms");
226 if (!mTimerManager) {
230 bool result = mTimerManager->restartTimer(
id, delay_ms);
236 if (!mTimerManager) {
239 size_t count = mTimerManager->getActiveTimerCount();
249 mLooper->postDelayed(mDelayMs, [self, handle]() {
250 SLLOOPER_DEBUG(
"DelayAwaitable: Delay completed, resuming coroutine");
261 std::thread([self, handle]() {
263 auto result = self->getFunc()();
264 self->setResult(result);
265 SLLOOPER_DEBUG(
"WorkAwaitable: Work completed successfully with result");
266 }
catch (
const std::exception& e) {
268 self->setException(std::current_exception());
270 SLLOOPER_ERROR(
"WorkAwaitable: Unknown exception in background work");
271 self->setException(std::current_exception());
273 self->getLooper()->post([handle]() {
274 SLLOOPER_DEBUG(
"WorkAwaitable: Resuming coroutine on main thread");
284 std::thread([self, handle]() {
288 SLLOOPER_DEBUG(
"WorkAwaitable<void>: Work completed successfully");
289 }
catch (
const std::exception& e) {
291 self->setException(std::current_exception());
294 self->setException(std::current_exception());
296 self->getLooper()->post([handle]() {
297 SLLOOPER_DEBUG(
"WorkAwaitable<void>: Resuming coroutine on main thread");
308 mLooper->post([self, handle]() {
310 auto result = self->getFunc()();
311 self->setResult(result);
312 SLLOOPER_DEBUG(
"PostAwaitable: Function executed successfully with result");
313 }
catch (
const std::exception& e) {
315 self->setException(std::current_exception());
318 self->setException(std::current_exception());
329 mLooper->post([self, handle]() {
333 SLLOOPER_DEBUG(
"PostAwaitable<void>: Function executed successfully");
334 }
catch (
const std::exception& e) {
336 self->setException(std::current_exception());
339 self->setException(std::current_exception());
Direct awaitable types for coroutine integration with SLLooper.
Centralized debug and logging macros for SW Task Framework components.
#define SLLOOPER_DEBUG_ENABLED
Enable/disable debug output for SLLooper operations.
#define SLLOOPER_INFO(msg)
Always-on info logging for SLLooper lifecycle events.
#define SLLOOPER_DEBUG_STREAM(stream_expr)
Conditional debug logging for SLLooper with stream expression.
#define SLLOOPER_ERROR(msg)
Always-on error logging for SLLooper critical issues.
#define SLLOOPER_DEBUG(msg)
Conditional debug logging for SLLooper operations.
#define SLLOOPER_ERROR_STREAM(stream_expr)
Always-on error logging for SLLooper with stream expression.
Unified event queue supporting messages and functions with timed execution.
Main event loop coordinator for asynchronous task management and timer operations.
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.
void await_suspend(std::coroutine_handle<> handle) const noexcept
Suspend coroutine and start timer.
@ FUNCTION
Function task with std::packaged_task.
@ MESSAGE
Traditional message with handler.
Awaitable for executing function on main thread.
void await_suspend(std::coroutine_handle<> handle) const noexcept
Suspend coroutine and post to main thread.
void updateTimerCancelledPtr(TimerId id, std::atomic< bool > *newPtr)
Update timer cancellation pointer for moved Timer objects.
bool restartTimerInternal(TimerId id, uint64_t delay_ms)
Restart existing timer with new delay.
Timer addTimer(std::function< void()> callback, uint64_t delay_ms)
Add one-shot timer with millisecond precision.
std::shared_ptr< EventQueue > getEventQueue()
Get access to the underlying event queue.
~SLLooper()
Destructor - stops event loop and cleanup resources.
bool cancelTimerInternal(TimerId id)
Internal timer cancellation method.
Timer addPeriodicTimer(std::function< void()> callback, uint64_t interval_ms)
Add periodic timer with millisecond interval.
bool loop()
Run one iteration of the event loop.
void exit()
Request event loop to exit.
void initializeTimerManager()
Initialize TimerManager lazily.
TimerId createTimerInternal(std::function< void()> callback, uint64_t delay_ms, bool periodic, std::atomic< bool > *cancelled)
Internal timer creation method.
size_t getActiveTimerCount()
Get count of active timers.
SLLooper()
Constructor - initializes message queue and starts event loop.
bool hasTimerInternal(TimerId id)
Check if timer exists in internal management.
RAII timer wrapper with boost-style API and move semantics.
bool isActive() const
Check if timer is currently active.
Awaitable for executing work on background thread.
void await_suspend(std::coroutine_handle<> handle) const noexcept
Suspend coroutine and execute work on background thread.
Software Timer namespace containing all timer-related classes.
uint64_t TimerId
Unique identifier type for timer instances.