diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 868462d..857afa1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ set(JUCI_SHARED_FILES json.cpp menu.cpp meson.cpp + mutex.cpp project_build.cpp snippets.cpp source.cpp diff --git a/src/mutex.cpp b/src/mutex.cpp new file mode 100644 index 0000000..a2aa12e --- /dev/null +++ b/src/mutex.cpp @@ -0,0 +1,39 @@ +#include "mutex.hpp" + +#if defined(__clang__) && (!defined(SWIG)) +#pragma GCC diagnostic ignored "-Wthread-safety" +#endif + +void Mutex::lock() { + mutex.lock(); +} + +bool Mutex::try_lock() { + return mutex.try_lock(); +} + +void Mutex::unlock() { + mutex.unlock(); +} + +const Mutex &Mutex::operator!() const { return *this; } + +LockGuard::LockGuard(Mutex &mutex_) : mutex(mutex_) { + mutex.lock(); + locked = true; +} + +void LockGuard::lock() { + mutex.lock(); + locked = true; +} + +void LockGuard::unlock() { + mutex.unlock(); + locked = false; +} + +LockGuard::~LockGuard() { + if(locked) + mutex.unlock(); +} diff --git a/src/mutex.hpp b/src/mutex.hpp index 2720e7e..e3cac9a 100644 --- a/src/mutex.hpp +++ b/src/mutex.hpp @@ -3,8 +3,8 @@ #include -// Enable thread safety attributes only with clang. Exclude Apple Clang since it is too old. -#if defined(__clang__) && !defined(SWIG) && !defined(__apple_build_version__) +// Enable thread safety attributes only with clang. +#if defined(__clang__) && (!defined(SWIG)) #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) #else #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op @@ -72,19 +72,10 @@ class CAPABILITY("mutex") Mutex { std::mutex mutex; public: - void lock() ACQUIRE() { - mutex.lock(); - } - - bool try_lock() TRY_ACQUIRE(true) { - return mutex.try_lock(); - } - - void unlock() RELEASE() { - mutex.unlock(); - } - - const Mutex &operator!() const { return *this; } + void lock() ACQUIRE(); + bool try_lock() TRY_ACQUIRE(true); + void unlock() RELEASE(); + const Mutex &operator!() const; }; /// Use this class instead of std::lock_guard and std::unique_lock @@ -93,20 +84,8 @@ class SCOPED_CAPABILITY LockGuard { bool locked; public: - LockGuard(Mutex &mutex_) ACQUIRE(mutex_) : mutex(mutex_) { - mutex.lock(); - locked = true; - } - void lock() ACQUIRE() { - mutex.lock(); - locked = true; - } - void unlock() RELEASE() { - mutex.unlock(); - locked = false; - } - ~LockGuard() RELEASE() { - if(locked) - mutex.unlock(); - } + LockGuard(Mutex &mutex_) ACQUIRE(mutex_); + void lock() ACQUIRE(); + void unlock() RELEASE(); + ~LockGuard() RELEASE(); };