summaryrefslogtreecommitdiffstats
path: root/src/Event.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-05 16:12:13 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 03:39:34 +0100
commitf090b30e587c7af51fde86f36c67ef139ed2ce6f (patch)
tree727f1f77da4a5a630aa18083713989aaf1a919a9 /src/Event.cpp
parentAll usages of previous event-system replaced with new event-system (diff)
downloadAltCraft-f090b30e587c7af51fde86f36c67ef139ed2ce6f.tar
AltCraft-f090b30e587c7af51fde86f36c67ef139ed2ce6f.tar.gz
AltCraft-f090b30e587c7af51fde86f36c67ef139ed2ce6f.tar.bz2
AltCraft-f090b30e587c7af51fde86f36c67ef139ed2ce6f.tar.lz
AltCraft-f090b30e587c7af51fde86f36c67ef139ed2ce6f.tar.xz
AltCraft-f090b30e587c7af51fde86f36c67ef139ed2ce6f.tar.zst
AltCraft-f090b30e587c7af51fde86f36c67ef139ed2ce6f.zip
Diffstat (limited to 'src/Event.cpp')
-rw-r--r--src/Event.cpp33
1 files changed, 19 insertions, 14 deletions
diff --git a/src/Event.cpp b/src/Event.cpp
index c857947..e8531d4 100644
--- a/src/Event.cpp
+++ b/src/Event.cpp
@@ -1,50 +1,55 @@
#include "Event.hpp"
std::list<EventListener*> EventSystem::listeners;
-std::mutex EventSystem::listenersMutex;
+std::recursive_mutex EventSystem::listenersMutex;
EventListener::EventListener() {
- std::lock_guard<std::mutex> listenersLock(EventSystem::listenersMutex);
+ EventSystem::listenersMutex.lock();
EventSystem::listeners.push_back(this);
+ EventSystem::listenersMutex.unlock();
}
EventListener::~EventListener() {
- std::lock_guard<std::mutex> listenersLock(EventSystem::listenersMutex);
+ EventSystem::listenersMutex.lock();
EventSystem::listeners.remove(this);
+ EventSystem::listenersMutex.unlock();
}
void EventListener::HandleEvent() {
- std::lock_guard<std::mutex> lock(eventsQueueMutex);
- std::lock_guard<std::mutex> lockHandlers(handlersMutex);
+ mutex.lock();
Event event = events.front();
events.pop();
if (handlers[event.id]) {
handlers[event.id](event);
}
+ mutex.unlock();
}
void EventListener::HandleAllEvents() {
- std::lock_guard<std::mutex> lock(eventsQueueMutex);
- std::lock_guard<std::mutex> lockHandlers(handlersMutex);
+ if (!NotEmpty())
+ return;
+
+ mutex.lock();
while (!events.empty()) {
Event event = events.front();
events.pop();
if (handlers[event.id]) {
handlers[event.id](event);
}
- }
+ }
+ mutex.unlock();
}
bool EventListener::NotEmpty() {
- std::lock_guard<std::mutex> lock(eventsQueueMutex);
- return !events.empty();
+ bool ret = !events.empty();
+ return ret;
}
void EventListener::WaitEvent() {
- eventsQueueMutex.lock();
+ mutex.lock();
while (events.empty()) {
- eventsQueueMutex.unlock();
- eventsQueueMutex.lock();
+ mutex.unlock();
+ mutex.lock();
}
- eventsQueueMutex.unlock();
+ mutex.unlock();
} \ No newline at end of file