mirror of
https://github.com/gelldur/EventBus.git
synced 2025-01-14 01:07:59 +08:00
Add AsyncEventBus::wait() function
This commit is contained in:
parent
3b908bdaa8
commit
d51be92632
@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
@ -99,7 +100,7 @@ public:
|
||||
void unlistenAll(const int token)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
_commandsQueue.push_back([this, token]() {
|
||||
_commandsQueue.emplace_back([this, token]() {
|
||||
std::lock_guard<std::mutex> guard {_callbacksMutex};
|
||||
for(auto& element : _callbacks)
|
||||
{
|
||||
@ -141,6 +142,7 @@ public:
|
||||
void schedule(Event event)
|
||||
{
|
||||
static_assert(Internal::validateEvent<Event>(), "Invalid event");
|
||||
_eventWaiting.notify_one();
|
||||
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
_eventQueue.push_back([this, event = std::move(event)]() {
|
||||
@ -183,6 +185,8 @@ public:
|
||||
* @return number of consumed events
|
||||
*/
|
||||
int consume(int max = std::numeric_limits<int>::max());
|
||||
bool wait();
|
||||
bool waitFor(std::chrono::milliseconds timeout);
|
||||
|
||||
std::size_t getQueueEventCount() const
|
||||
{
|
||||
@ -204,6 +208,10 @@ private:
|
||||
std::map<Internal::type_id_t, std::unique_ptr<Internal::CallbackVector>> _callbacks;
|
||||
mutable std::mutex _callbacksMutex;
|
||||
mutable std::mutex _eventMutex;
|
||||
|
||||
std::mutex _waitMutex;
|
||||
std::condition_variable _eventWaiting;
|
||||
|
||||
std::deque<std::function<void()>> _eventQueue;
|
||||
std::deque<std::function<void()>> _commandsQueue;
|
||||
};
|
||||
|
@ -36,4 +36,20 @@ int AsyncEventBus::consume(int max)
|
||||
return consumed;
|
||||
}
|
||||
|
||||
bool AsyncEventBus::wait()
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
std::unique_lock<std::mutex> lock(_waitMutex);
|
||||
_eventWaiting.wait(lock);
|
||||
return not _eventQueue.empty();
|
||||
}
|
||||
bool AsyncEventBus::waitFor(std::chrono::milliseconds timeout)
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
std::unique_lock<std::mutex> lock(_waitMutex);
|
||||
_eventWaiting.wait_for(lock, timeout);
|
||||
|
||||
return not _eventQueue.empty();
|
||||
}
|
||||
|
||||
} // namespace Dexode
|
||||
|
Loading…
x
Reference in New Issue
Block a user