-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ExecuteUntil to event dispatchers, add EventDispatcherThrea…
…dAware (#526) * infra/event/EventDispatcher{WithWeakPtr}: Add ExecuteUntil * Add infra/event/EventDispatcherThreadAware * Apply suggestions from code review Co-authored-by: Daan Timmer <[email protected]> * Update infra/event/test/TestEventDispatcherThreadAware.cpp Co-authored-by: Daan Timmer <[email protected]> --------- Co-authored-by: Daan Timmer <[email protected]>
- Loading branch information
1 parent
9e7f949
commit d05d1f3
Showing
12 changed files
with
371 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "infra/event/EventDispatcherThreadAware.hpp" | ||
|
||
namespace infra | ||
{ | ||
void EventDispatcherThreadAwareWorker::RequestExecution() | ||
{ | ||
std::lock_guard lock{ mutex }; | ||
|
||
ready = true; | ||
condition.notify_one(); | ||
} | ||
|
||
void EventDispatcherThreadAwareWorker::Idle() | ||
{ | ||
std::unique_lock lock{ mutex }; | ||
|
||
condition.wait(lock, [this]() | ||
{ | ||
return ready; | ||
}); | ||
|
||
ready = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef INFRA_EVENT_DISPATCHER_THREAD_AWARE_HPP | ||
#define INFRA_EVENT_DISPATCHER_THREAD_AWARE_HPP | ||
|
||
#include "infra/event/EventDispatcherWithWeakPtr.hpp" | ||
#include <condition_variable> | ||
#include <mutex> | ||
|
||
namespace infra | ||
{ | ||
class EventDispatcherThreadAwareWorker | ||
: public infra::EventDispatcherWithWeakPtrWorker | ||
{ | ||
public: | ||
using EventDispatcherWithWeakPtrWorker::EventDispatcherWithWeakPtrWorker; | ||
|
||
void RequestExecution() override; | ||
void Idle() override; | ||
|
||
private: | ||
std::condition_variable condition; | ||
std::mutex mutex; | ||
bool ready{ false }; | ||
}; | ||
|
||
using EventDispatcherThreadAware = EventDispatcherWithWeakPtrConnector<EventDispatcherThreadAwareWorker>; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "infra/event/EventDispatcherThreadAware.hpp" | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include <thread> | ||
|
||
class EventDispatcherThreadAwareIdleCounting | ||
: public infra::EventDispatcherThreadAware::WithSize<50> | ||
{ | ||
protected: | ||
void Idle() override | ||
{ | ||
++idleCount; | ||
infra::EventDispatcherThreadAware::WithSize<50>::Idle(); | ||
} | ||
|
||
public: | ||
int idleCount = 0; | ||
}; | ||
|
||
class EventDispatcherThreadAwareTest | ||
: public testing::Test | ||
, public EventDispatcherThreadAwareIdleCounting | ||
{}; | ||
|
||
TEST_F(EventDispatcherThreadAwareTest, scheduled_action_is_executed) | ||
{ | ||
bool done = false; | ||
|
||
std::thread t([&]() | ||
{ | ||
std::this_thread::sleep_for(std::chrono::milliseconds(50)); | ||
|
||
infra::EventDispatcher::Instance().Schedule([&]() | ||
{ | ||
done = true; | ||
}); | ||
}); | ||
|
||
ExecuteUntil([&]() | ||
{ | ||
return done; | ||
}); | ||
|
||
EXPECT_THAT(idleCount, testing::Eq(1)); | ||
|
||
t.join(); | ||
} |
Oops, something went wrong.