Skip to content

Commit

Permalink
Fix C++11 compilability
Browse files Browse the repository at this point in the history
Previous commits broke it... :(
  • Loading branch information
est31 committed Jan 23, 2016
1 parent 0459eca commit e50c784
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_base.cpp
Expand Up @@ -68,7 +68,7 @@ class ModNameStorer
*/

ScriptApiBase::ScriptApiBase() :
m_luastackmutex(true)
m_luastackmutex()
{
#ifdef SCRIPTAPI_LOCK_DEBUG
m_lock_recursion_count = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_base.h
Expand Up @@ -108,7 +108,7 @@ class ScriptApiBase {
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
void objectrefGet(lua_State *L, u16 id);

Mutex m_luastackmutex;
RecursiveMutex m_luastackmutex;
std::string m_last_run_mod;
bool m_secure;
#ifdef SCRIPTAPI_LOCK_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_internal.h
Expand Up @@ -75,7 +75,7 @@ class LockChecker {
#endif

#define SCRIPTAPI_PRECHECKHEADER \
MutexAutoLock scriptlock(this->m_luastackmutex); \
RecursiveMutexAutoLock scriptlock(this->m_luastackmutex); \
SCRIPTAPI_LOCK_CHECK; \
realityCheck(); \
lua_State *L = getStack(); \
Expand Down
1 change: 1 addition & 0 deletions src/threading/event.h
Expand Up @@ -29,6 +29,7 @@ DEALINGS IN THE SOFTWARE.
#if __cplusplus >= 201103L
#include <condition_variable>
#include "threading/mutex.h"
#include "threading/mutex_auto_lock.h"
#elif defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
Expand Down
15 changes: 15 additions & 0 deletions src/threading/mutex.cpp
Expand Up @@ -34,7 +34,18 @@ DEALINGS IN THE SOFTWARE.

#define UNUSED(expr) do { (void)(expr); } while (0)

Mutex::Mutex()
{
init_mutex(false);
}


Mutex::Mutex(bool recursive)
{
init_mutex(recursive);
}

void Mutex::init_mutex(bool recursive)
{
#ifdef _WIN32
// Windows critical sections are recursive by default
Expand Down Expand Up @@ -89,5 +100,9 @@ void Mutex::unlock()
#endif
}

RecursiveMutex::RecursiveMutex()
: Mutex(true)
{}

#endif

14 changes: 13 additions & 1 deletion src/threading/mutex.h
Expand Up @@ -30,6 +30,7 @@ DEALINGS IN THE SOFTWARE.
#if __cplusplus >= 201103L && !defined(_WIN32)
#include <mutex>
using Mutex = std::mutex;
using RecursiveMutex = std::recursive_mutex;
#else

#ifdef _WIN32
Expand All @@ -49,11 +50,14 @@ DEALINGS IN THE SOFTWARE.
class Mutex
{
public:
Mutex(bool recursive=false);
Mutex();
~Mutex();
void lock();
void unlock();

protected:
Mutex(bool recursive);
void init_mutex(bool recursive);
private:
#ifdef _WIN32
CRITICAL_SECTION mutex;
Expand All @@ -64,6 +68,14 @@ class Mutex
DISABLE_CLASS_COPY(Mutex);
};

class RecursiveMutex : public Mutex
{
public:
RecursiveMutex();

DISABLE_CLASS_COPY(RecursiveMutex);
};

#endif // C++11

#endif
12 changes: 11 additions & 1 deletion src/threading/mutex_auto_lock.h
Expand Up @@ -28,7 +28,8 @@ DEALINGS IN THE SOFTWARE.

#if __cplusplus >= 201103L
#include <mutex>
using MutexAutoLock = std::lock_guard<std::mutex>;
using MutexAutoLock = std::unique_lock<std::mutex>;
using RecursiveMutexAutoLock = std::unique_lock<std::recursive_mutex>;
#else

#include "threading/mutex.h"
Expand All @@ -44,6 +45,15 @@ class MutexAutoLock
Mutex &mutex;
};

class RecursiveMutexAutoLock
{
public:
RecursiveMutexAutoLock(RecursiveMutex &m) : mutex(m) { mutex.lock(); }
~RecursiveMutexAutoLock() { mutex.unlock(); }

private:
RecursiveMutex &mutex;
};
#endif

#endif
Expand Down

0 comments on commit e50c784

Please sign in to comment.