From 8b275c0b0a1064a813ef14a109b64e1fce461893 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Mon, 7 Aug 2017 20:08:15 +0500 Subject: 2017-08-07 --- src/Event.cpp | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'src/Event.cpp') diff --git a/src/Event.cpp b/src/Event.cpp index e58a6f4..94eedba 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -19,13 +19,20 @@ void EventListener::PushEvent(Event event) { eventsMutex.lock(); handlersMutex.lock(); if (handlers[event.type]) { - //LOG(ERROR) << "Listener notified about event " << int(event.type); events.push(event); } handlersMutex.unlock(); eventsMutex.unlock(); } +void EventListener::DirectCall(Event event) +{ + handlersMutex.lock(); + if (handlers[event.type]) + handlers[event.type](event.data); + handlersMutex.unlock(); +} + bool EventListener::IsEventsQueueIsNotEmpty() { eventsMutex.lock(); bool value = !events.empty(); @@ -74,20 +81,33 @@ void EventAgregator::PushEvent(EventType type, EventData data) { Event event; event.type = type; event.data = data; + queueMutex.lock(); eventsToHandle.push(event); + queueMutex.unlock(); if (!isStarted) { isStarted = true; std::thread(&EventAgregator::EventHandlingLoop).detach(); } } +void EventAgregator::DirectEventCall(EventType type, EventData data) +{ + Event event {type, data}; + listenersMutex.lock(); + for (auto &listener : listeners) { + listenersMutex.unlock(); + listener->DirectCall(event); + listenersMutex.lock(); + } + listenersMutex.unlock(); +} + void EventAgregator::EventHandlingLoop() { + LoopExecutionTimeController timer(std::chrono::milliseconds(5)); while (true) { queueMutex.lock(); if (!eventsToHandle.empty()) { - auto queue = eventsToHandle; - while (!eventsToHandle.empty()) - eventsToHandle.pop(); + auto queue = std::move(eventsToHandle); queueMutex.unlock(); while (!queue.empty()) { @@ -103,9 +123,6 @@ void EventAgregator::EventHandlingLoop() { queueMutex.lock(); } queueMutex.unlock(); + timer.Update(); } -} - -void SetGlobalState(GlobalState state) { - EventAgregator::PushEvent(EventType::GlobalAppState, GlobalAppStateData{state}); } \ No newline at end of file -- cgit v1.2.3