Reimplement Mutex with C++'s <mutex>
Main: - It's now implemented thanks to `<mutex>`. No more platform-specific implementations. - `BinaryMutex` (non-recursive) is added, as an alternative for special cases. - Doesn't need allocation/deallocation anymore. It can live in the stack and be part of other classes. - Because of that, it's methods are now `const` and the inner mutex is `mutable` so it can be easily used in `const` contexts. - A no-op implementation is provided if `NO_THREADS` is defined. No more need to add `#ifdef NO_THREADS` just for this. - `MutexLock` now takes a reference. At this point the cases of null `Mutex`es are rare. If you ever need that, just don't use `MutexLock`. - Thread-safe utilities are therefore simpler now. Misc.: - `ScopedMutexLock` is dropped and replaced by `MutexLock`, because they were pretty much the same. - Every case of lock, do-something, unlock is replaced by `MutexLock` (complex cases where it's not straightfoward are kept as as explicit lock and unlock). - `ShaderRD` contained an `std::mutex`, which has been replaced by `Mutex`.
This commit is contained in:
@ -32,42 +32,69 @@
|
||||
#define MUTEX_H
|
||||
|
||||
#include "core/error_list.h"
|
||||
#include "core/typedefs.h"
|
||||
|
||||
/**
|
||||
* @class Mutex
|
||||
* @author Juan Linietsky
|
||||
* Portable Mutex (thread-safe locking) implementation.
|
||||
* Mutexes are always recursive ( they don't self-lock in a single thread ).
|
||||
* Mutexes can be used with a Lockp object like this, to avoid having to worry about unlocking:
|
||||
* Lockp( mutex );
|
||||
*/
|
||||
#if !(defined NO_THREADS)
|
||||
|
||||
class Mutex {
|
||||
protected:
|
||||
static Mutex *(*create_func)(bool);
|
||||
#include <mutex>
|
||||
|
||||
template <class StdMutexT>
|
||||
class MutexImpl {
|
||||
mutable StdMutexT mutex;
|
||||
|
||||
public:
|
||||
virtual void lock() = 0; ///< Lock the mutex, block if locked by someone else
|
||||
virtual void unlock() = 0; ///< Unlock the mutex, let other threads continue
|
||||
virtual Error try_lock() = 0; ///< Attempt to lock the mutex, OK on success, ERROR means it can't lock.
|
||||
_ALWAYS_INLINE_ void lock() const {
|
||||
mutex.lock();
|
||||
}
|
||||
|
||||
static Mutex *create(bool p_recursive = true); ///< Create a mutex
|
||||
_ALWAYS_INLINE_ void unlock() const {
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
virtual ~Mutex();
|
||||
_ALWAYS_INLINE_ Error try_lock() const {
|
||||
return mutex.try_lock() ? OK : ERR_BUSY;
|
||||
}
|
||||
};
|
||||
|
||||
template <class MutexT>
|
||||
class MutexLock {
|
||||
|
||||
Mutex *mutex;
|
||||
const MutexT &mutex;
|
||||
|
||||
public:
|
||||
MutexLock(Mutex *p_mutex) {
|
||||
mutex = p_mutex;
|
||||
if (mutex) mutex->lock();
|
||||
_ALWAYS_INLINE_ explicit MutexLock(const MutexT &p_mutex) :
|
||||
mutex(p_mutex) {
|
||||
mutex.lock();
|
||||
}
|
||||
~MutexLock() {
|
||||
if (mutex) mutex->unlock();
|
||||
|
||||
_ALWAYS_INLINE_ ~MutexLock() {
|
||||
mutex.unlock();
|
||||
}
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
template <class StdMutexType>
|
||||
class MutexImpl {
|
||||
public:
|
||||
_ALWAYS_INLINE_ void lock() const {}
|
||||
_ALWAYS_INLINE_ void unlock() const {}
|
||||
_ALWAYS_INLINE_ Error try_lock() const { return OK; }
|
||||
};
|
||||
|
||||
template <class MutexT>
|
||||
class MutexLock {
|
||||
public:
|
||||
explicit MutexLock(const MutexT &p_mutex) {}
|
||||
};
|
||||
|
||||
#endif // !NO_THREADS
|
||||
|
||||
using Mutex = MutexImpl<std::recursive_mutex>; // Recursive, for general use
|
||||
using BinaryMutex = MutexImpl<std::mutex>; // Non-recursive, handle with care
|
||||
|
||||
extern template class MutexImpl<std::recursive_mutex>;
|
||||
extern template class MutexImpl<std::mutex>;
|
||||
extern template class MutexLock<MutexImpl<std::recursive_mutex> >;
|
||||
extern template class MutexLock<MutexImpl<std::mutex> >;
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user