Skip to content

Commit

Permalink
⚡ (EventLoop): Loop once or multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Dec 19, 2023
1 parent 778bf10 commit 7a5d8f2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions include/interface/libs/EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
5 changes: 3 additions & 2 deletions libs/EventLoopKit/include/EventLoopKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {};
Expand Down
7 changes: 5 additions & 2 deletions libs/EventLoopKit/source/EventLoopKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/stubs/stubs/leka/EventLoopKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/stubs/stubs/leka/source/EventLoopKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7a5d8f2

Please sign in to comment.