summaryrefslogtreecommitdiffstats
path: root/src/Event.hpp
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.hpp
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.hpp')
-rw-r--r--src/Event.hpp28
1 files changed, 16 insertions, 12 deletions
diff --git a/src/Event.hpp b/src/Event.hpp
index 752fda4..38541a1 100644
--- a/src/Event.hpp
+++ b/src/Event.hpp
@@ -63,8 +63,7 @@ class EventListener {
using HandlerType = std::function<void(const Event&)>;
std::queue<Event> events;
std::map<size_t, HandlerType> handlers;
- std::mutex eventsQueueMutex;
- std::mutex handlersMutex;
+ std::recursive_mutex mutex;
public:
EventListener();
@@ -79,8 +78,9 @@ public:
void WaitEvent();
void RegisterHandler(size_t eventId, const HandlerType &data) {
- std::lock_guard<std::mutex> lock(handlersMutex);
+ mutex.lock();
handlers[eventId] = data;
+ mutex.unlock();
}
void RegisterHandler(const char *eventId, const HandlerType & data) {
@@ -91,22 +91,25 @@ public:
class EventSystem {
friend class EventListener;
static std::list<EventListener*> listeners;
- static std::mutex listenersMutex;
+ static std::recursive_mutex listenersMutex;
public:
template <typename T>
static void PushEvent(size_t eventId, T data) {
Event event(eventId, data);
- std::lock_guard<std::mutex> listenersLock(listenersMutex);
for (auto& listener : listeners) {
- std::lock_guard<std::mutex> lock(listener->eventsQueueMutex);
- std::lock_guard<std::mutex> lockHandlers(listener->handlersMutex);
+ //if (!listener->mutex.try_lock()) throw std::runtime_error("WHY?!");
+ listener->mutex.lock();
auto it = listener->handlers.find(eventId);
- if (it == listener->handlers.end())
- continue;
+ if (it == listener->handlers.end()) {
+ listener->mutex.unlock();
+ continue;
+ }
listener->events.push(event);
+
+ listener->mutex.unlock();
}
}
@@ -114,16 +117,17 @@ public:
static void DirectEventCall(size_t eventId, T data) {
Event event(eventId, data);
- std::lock_guard<std::mutex> listenersLock(listenersMutex);
+ listenersMutex.lock();
for (auto & listener : listeners) {
- std::lock_guard<std::mutex> lock(listener->eventsQueueMutex);
- std::lock_guard<std::mutex> lockHandlers(listener->handlersMutex);
+ listener->mutex.lock();
auto it = listener->handlers.find(eventId);
if (it == listener->handlers.end())
continue;
it->second(event);
+ listener->mutex.unlock();
}
+ listenersMutex.unlock();
}
};