Path: blob/a-new-beginning/SharedDependencies/Sources/eventbus/AsyncEventBus.cpp
2 views
#include <eventbus/AsyncEventBus.h>12namespace Dexode3{45std::size_t AsyncEventBus::processCommandsAndGetQueuedEventsCount()6{7std::lock_guard<std::mutex> guard {_eventMutex};8while(_commandsQueue.empty() == false)9{10_commandsQueue11.front()(); //This can't add any extra commands, because in this queue we story only listen/unlisten stuff12_commandsQueue.pop_front();13}14//Yeah we want to return events count. So don't have to call getQueueEventCount15return _eventQueue.size();16}1718int AsyncEventBus::consume(int max)19{20int consumed = 0;2122std::function<void()> eventCommand;23while(processCommandsAndGetQueuedEventsCount() > 0 && consumed < max) //order is important24{25{26std::lock_guard<std::mutex> guard {_eventMutex};27eventCommand = std::move(_eventQueue.front());28_eventQueue.pop_front();29}3031eventCommand();32++consumed;33}3435return consumed;36}3738bool AsyncEventBus::wait()39{40using namespace std::chrono_literals;41std::unique_lock<std::mutex> lock(_waitMutex);42_eventWaiting.wait(lock);43return not _eventQueue.empty();44}45bool AsyncEventBus::waitFor(std::chrono::milliseconds timeout)46{47using namespace std::chrono_literals;48std::unique_lock<std::mutex> lock(_waitMutex);49_eventWaiting.wait_for(lock, timeout);5051return not _eventQueue.empty();52}5354} // namespace Dexode555657