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.hpp | |
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 'src/Event.hpp')
-rw-r--r-- | src/Event.hpp | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/src/Event.hpp b/src/Event.hpp index 38541a1..0f60d1a 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -8,6 +8,8 @@ #include <list> #include <mutex> +#include <easylogging++.h> + size_t constexpr StrHash(char const *input) { return *input ? static_cast<size_t>(*input) + 33 * StrHash(input + 1) : 5381; @@ -77,11 +79,7 @@ public: void WaitEvent(); - void RegisterHandler(size_t eventId, const HandlerType &data) { - mutex.lock(); - handlers[eventId] = data; - mutex.unlock(); - } + void RegisterHandler(size_t eventId, const HandlerType &data); void RegisterHandler(const char *eventId, const HandlerType & data) { RegisterHandler(StrHash(eventId), data); @@ -99,17 +97,13 @@ public: Event event(eventId, data); for (auto& listener : listeners) { - //if (!listener->mutex.try_lock()) throw std::runtime_error("WHY?!"); - listener->mutex.lock(); + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); + auto it = listener->handlers.find(eventId); - if (it == listener->handlers.end()) { - listener->mutex.unlock(); + if (it == listener->handlers.end()) continue; - } listener->events.push(event); - - listener->mutex.unlock(); } } @@ -117,20 +111,18 @@ public: static void DirectEventCall(size_t eventId, T data) { Event event(eventId, data); - listenersMutex.lock(); for (auto & listener : listeners) { - listener->mutex.lock(); + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); + auto it = listener->handlers.find(eventId); if (it == listener->handlers.end()) continue; it->second(event); - listener->mutex.unlock(); } - listenersMutex.unlock(); } }; -#define PUSH_EVENT(eventName, data) EventSystem::PushEvent(StrHash(eventName),data) +#define PUSH_EVENT(eventName, data) EventSystem::PushEvent(StrHash(eventName),data); LOG(INFO)<<"PUSH_EVENT "<<eventName; -#define DIRECT_EVENT_CALL(eventName,data) EventSystem::DirectEventCall(StrHash(eventName),data)
\ No newline at end of file +#define DIRECT_EVENT_CALL(eventName,data) EventSystem::DirectEventCall(StrHash(eventName),data); LOG(INFO)<<"DIRECT_CALL "<<eventName;
\ No newline at end of file |