diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-12 07:54:50 +0100 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 03:39:34 +0100 |
commit | 14a629c0a15dcc095a4d413e4782bc87fc568fdd (patch) | |
tree | 5c9a7fd18f2d2470fda8d40cc0258961767962df /src/Event.cpp | |
parent | EventData is meaningless from now (diff) | |
download | AltCraft-14a629c0a15dcc095a4d413e4782bc87fc568fdd.tar AltCraft-14a629c0a15dcc095a4d413e4782bc87fc568fdd.tar.gz AltCraft-14a629c0a15dcc095a4d413e4782bc87fc568fdd.tar.bz2 AltCraft-14a629c0a15dcc095a4d413e4782bc87fc568fdd.tar.lz AltCraft-14a629c0a15dcc095a4d413e4782bc87fc568fdd.tar.xz AltCraft-14a629c0a15dcc095a4d413e4782bc87fc568fdd.tar.zst AltCraft-14a629c0a15dcc095a4d413e4782bc87fc568fdd.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Event.cpp | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/Event.cpp b/src/Event.cpp index e8531d4..7604609 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -4,32 +4,32 @@ std::list<EventListener*> EventSystem::listeners; std::recursive_mutex EventSystem::listenersMutex; EventListener::EventListener() { - EventSystem::listenersMutex.lock(); + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); EventSystem::listeners.push_back(this); - EventSystem::listenersMutex.unlock(); } EventListener::~EventListener() { - EventSystem::listenersMutex.lock(); + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); EventSystem::listeners.remove(this); - EventSystem::listenersMutex.unlock(); } void EventListener::HandleEvent() { - mutex.lock(); + if (!NotEmpty()) + return; + + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); Event event = events.front(); events.pop(); if (handlers[event.id]) { handlers[event.id](event); } - mutex.unlock(); } void EventListener::HandleAllEvents() { if (!NotEmpty()) return; - mutex.lock(); + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); while (!events.empty()) { Event event = events.front(); events.pop(); @@ -37,7 +37,6 @@ void EventListener::HandleAllEvents() { handlers[event.id](event); } } - mutex.unlock(); } bool EventListener::NotEmpty() { @@ -46,10 +45,14 @@ bool EventListener::NotEmpty() { } void EventListener::WaitEvent() { - mutex.lock(); + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); while (events.empty()) { mutex.unlock(); mutex.lock(); } - mutex.unlock(); -}
\ No newline at end of file +} + +void EventListener::RegisterHandler(size_t eventId, const EventListener::HandlerType &data) { + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); + handlers[eventId] = data; +} |