From 78451a4223bbc5ed1ea004de1ce4e3bc78c84e2e Mon Sep 17 00:00:00 2001 From: John Sully Date: Sat, 23 Feb 2019 01:50:02 -0500 Subject: [PATCH] Support a non-spinlock mutex if compiled in --- src/ae.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/ae.cpp b/src/ae.cpp index dd0ff3a32..e7defaa21 100644 --- a/src/ae.cpp +++ b/src/ae.cpp @@ -51,7 +51,39 @@ extern "C" { #include "config.h" } +#ifdef USE_MUTEX +thread_local int cOwnLock = 0; +class mutex_wrapper +{ + std::recursive_mutex m_mutex; +public: + void lock() { + m_mutex.lock(); + cOwnLock++; + } + + void unlock() { + cOwnLock--; + m_mutex.unlock(); + } + + bool try_lock() { + if (m_mutex.try_lock()) { + cOwnLock++; + return true; + } + return false; + } + + bool fOwnLock() { + return cOwnLock > 0; + } +}; +mutex_wrapper g_lock; + +#else fastlock g_lock; +#endif thread_local aeEventLoop *g_eventLoopThisThread = NULL; #define AE_ASSERT(x) if (!(x)) do { fprintf(stderr, "AE_ASSER FAILURE\n"); *((volatile int*)0) = 1; } while(0)