From 7a5d8f2a8091c6283f86171c2707eecc621eb901 Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Tue, 19 Dec 2023 12:25:23 +0100 Subject: [PATCH] :zap: (EventLoop): Loop once or multiple times --- include/interface/libs/EventLoop.h | 6 +++--- libs/EventLoopKit/include/EventLoopKit.h | 5 +++-- libs/EventLoopKit/source/EventLoopKit.cpp | 7 +++++-- tests/unit/stubs/stubs/leka/EventLoopKit.h | 2 +- tests/unit/stubs/stubs/leka/source/EventLoopKit.cpp | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/interface/libs/EventLoop.h b/include/interface/libs/EventLoop.h index bfb0a587ed..906464725c 100644 --- a/include/interface/libs/EventLoop.h +++ b/include/interface/libs/EventLoop.h @@ -16,9 +16,9 @@ class EventLoop virtual void registerCallback(const callback_t &) = 0; - virtual void start() = 0; - virtual void stop() = 0; - virtual void exit() = 0; + virtual void start(bool loop_once = true) = 0; + virtual void stop() = 0; + virtual void exit() = 0; virtual void loop() = 0; }; diff --git a/libs/EventLoopKit/include/EventLoopKit.h b/libs/EventLoopKit/include/EventLoopKit.h index 3b78790623..9eefb35289 100644 --- a/libs/EventLoopKit/include/EventLoopKit.h +++ b/libs/EventLoopKit/include/EventLoopKit.h @@ -18,7 +18,7 @@ class EventLoopKit : public interface::EventLoop void registerCallback(const callback_t &callback) final; - void start() final; + void start(bool loop_once = true) final; void stop() final; void exit() final; @@ -29,7 +29,8 @@ class EventLoopKit : public interface::EventLoop void loop() final; - bool must_exit = false; + bool _loop_once = true; + bool must_exit = false; callback_t _callback {}; rtos::Thread _thread {}; diff --git a/libs/EventLoopKit/source/EventLoopKit.cpp b/libs/EventLoopKit/source/EventLoopKit.cpp index b82517031b..c7b1121b1d 100644 --- a/libs/EventLoopKit/source/EventLoopKit.cpp +++ b/libs/EventLoopKit/source/EventLoopKit.cpp @@ -16,11 +16,12 @@ void EventLoopKit::registerCallback(const callback_t &callback) _callback = callback; } -void EventLoopKit::start() +void EventLoopKit::start(bool loop_once) { if (_callback == nullptr) { return; } + _loop_once = loop_once; _flags.set(flag::START); } @@ -43,8 +44,10 @@ void EventLoopKit::loop() return ret; }; + auto should_clear = [&] { return _loop_once; }; + while (keep_running()) { - _flags.wait_any(flag::START, true); + _flags.wait_any(flag::START, should_clear()); _callback(); } diff --git a/tests/unit/stubs/stubs/leka/EventLoopKit.h b/tests/unit/stubs/stubs/leka/EventLoopKit.h index 86d1364cc5..2a9c14a172 100644 --- a/tests/unit/stubs/stubs/leka/EventLoopKit.h +++ b/tests/unit/stubs/stubs/leka/EventLoopKit.h @@ -18,7 +18,7 @@ class EventLoopKit : public interface::EventLoop void registerCallback(const callback_t &callback) final; - void start() final; + void start(bool loop_once = true) final; void stop() final; void exit() final; diff --git a/tests/unit/stubs/stubs/leka/source/EventLoopKit.cpp b/tests/unit/stubs/stubs/leka/source/EventLoopKit.cpp index 3e81bdcead..10f9384f80 100644 --- a/tests/unit/stubs/stubs/leka/source/EventLoopKit.cpp +++ b/tests/unit/stubs/stubs/leka/source/EventLoopKit.cpp @@ -11,7 +11,7 @@ void EventLoopKit::registerCallback(const callback_t &callback) _callback = callback; } -void EventLoopKit::start() +void EventLoopKit::start(bool loop_once) { if (_callback == nullptr) { return;