summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Event.cpp33
-rw-r--r--src/Event.hpp28
-rw-r--r--src/GlobalState.cpp3
3 files changed, 37 insertions, 27 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
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();
}
};
diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp
index 2bd4943..e947e01 100644
--- a/src/GlobalState.cpp
+++ b/src/GlobalState.cpp
@@ -45,7 +45,7 @@ void InitEvents() {
}
catch (std::exception &e) {
LOG(WARNING) << "Connection failed";
- PUSH_EVENT("ConnectionFailed", e.what());
+ PUSH_EVENT("ConnectionFailed", std::string(e.what()));
return;
}
LOG(INFO) << "Connected to server";
@@ -177,6 +177,7 @@ void PhysExec() {
}
void GsExec() {
+ el::Helpers::setThreadName("Game");
LoopExecutionTimeController timer(std::chrono::milliseconds(16));
while (isRunning) {