diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-12 12:48:42 +0100 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 03:39:34 +0100 |
commit | b009754d1bc09d9cb26b35e41146029491044b6d (patch) | |
tree | 2226bbd839023002dad3132ec565c99c1b1220a8 /src/Event.cpp | |
parent | Fixed some incorrect types for events (diff) | |
download | AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.gz AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.bz2 AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.lz AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.xz AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.zst AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Event.cpp | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/src/Event.cpp b/src/Event.cpp index 7604609..08c9ee7 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -17,9 +17,10 @@ void EventListener::HandleEvent() { if (!NotEmpty()) return; - std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); + std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); Event event = events.front(); - events.pop(); + events.pop(); + std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex); if (handlers[event.id]) { handlers[event.id](event); } @@ -29,7 +30,8 @@ void EventListener::HandleAllEvents() { if (!NotEmpty()) return; - std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); + std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); + std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex); while (!events.empty()) { Event event = events.front(); events.pop(); @@ -40,19 +42,27 @@ void EventListener::HandleAllEvents() { } bool EventListener::NotEmpty() { + PollEvents(); + std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); bool ret = !events.empty(); return ret; } -void EventListener::WaitEvent() { - std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); - while (events.empty()) { - mutex.unlock(); - mutex.lock(); - } -} - void EventListener::RegisterHandler(size_t eventId, const EventListener::HandlerType &data) { - std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); + std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex); handlers[eventId] = data; } + +void EventListener::PollEvents() { + std::lock_guard<std::recursive_mutex> rawLock (rawEventsMutex); + if (rawEvents.empty()) + return; + std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); + std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex); + while (!rawEvents.empty()) { + Event event = rawEvents.front(); + rawEvents.pop(); + if (handlers[event.id]) + events.push(event); + } +} |