#ifndef MUTEX_H #define MUTEX_H /* mutex.h */ #include "core/error/error_list.h" #include "core/typedefs.h" #if !defined(NO_THREADS) #include template class MutexImpl { mutable StdMutexT mutex; friend class MutexLock; public: _ALWAYS_INLINE_ void lock() const { mutex.lock(); } _ALWAYS_INLINE_ void unlock() const { mutex.unlock(); } _ALWAYS_INLINE_ Error try_lock() const { return mutex.try_lock() ? OK : ERR_BUSY; } }; // This is written this way instead of being a template to overcome a limitation of C++ pre-17 // that would require MutexLock to be used like this: MutexLock lock; class MutexLock { union { std::recursive_mutex *recursive_mutex; std::mutex *mutex; }; bool recursive; public: _ALWAYS_INLINE_ explicit MutexLock(const MutexImpl &p_mutex) : recursive_mutex(&p_mutex.mutex), recursive(true) { recursive_mutex->lock(); } _ALWAYS_INLINE_ explicit MutexLock(const MutexImpl &p_mutex) : mutex(&p_mutex.mutex), recursive(false) { mutex->lock(); } _ALWAYS_INLINE_ ~MutexLock() { if (recursive) { recursive_mutex->unlock(); } else { mutex->unlock(); } } }; using Mutex = MutexImpl; // Recursive, for general use using BinaryMutex = MutexImpl; // Non-recursive, handle with care extern template class MutexImpl; extern template class MutexImpl; #else class FakeMutex { FakeMutex() {} }; template class MutexImpl { public: _ALWAYS_INLINE_ void lock() const {} _ALWAYS_INLINE_ void unlock() const {} _ALWAYS_INLINE_ Error try_lock() const { return OK; } }; class MutexLock { public: explicit MutexLock(const MutexImpl &p_mutex) {} }; using Mutex = MutexImpl; using BinaryMutex = MutexImpl; // Non-recursive, handle with care #endif // !NO_THREADS #endif // MUTEX_H