diff options
author | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
commit | 15740faf9fe9fe4be08965098bbf2947e096aeeb (patch) | |
tree | a730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Threads/Mutex.h |
Diffstat (limited to 'Runtime/Threads/Mutex.h')
-rw-r--r-- | Runtime/Threads/Mutex.h | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/Runtime/Threads/Mutex.h b/Runtime/Threads/Mutex.h new file mode 100644 index 0000000..91e9927 --- /dev/null +++ b/Runtime/Threads/Mutex.h @@ -0,0 +1,86 @@ +#ifndef __MUTEX_H +#define __MUTEX_H + +#if SUPPORT_THREADS + +#if UNITY_WIN || UNITY_XENON +# include "Winapi/PlatformMutex.h" +#elif UNITY_OSX || UNITY_IPHONE || UNITY_ANDROID || UNITY_PEPPER || UNITY_LINUX || UNITY_BB10 || UNITY_TIZEN +# include "Posix/PlatformMutex.h" +#else +# include "PlatformMutex.h" +#endif + +#include "Runtime/Utilities/NonCopyable.h" + +/** + * A mutex class. Always recursive (a single thread can lock multiple times). + */ +class Mutex : public NonCopyable +{ +public: + + class AutoLock + { + public: + AutoLock( Mutex& mutex ) + : m_Mutex(&mutex) + { + mutex.Lock(); + } + + ~AutoLock() + { + m_Mutex->Unlock(); + } + + private: + AutoLock(const AutoLock&); + AutoLock& operator=(const AutoLock&); + + private: + Mutex* m_Mutex; + }; + + Mutex(); + ~Mutex(); + + void Lock(); + void Unlock(); + + // Returns true if locking succeeded + bool TryLock(); + + // Returns true if the mutex is currently locked + bool IsLocked(); + + void BlockUntilUnlocked(); + +private: + + PlatformMutex m_Mutex; +}; + +#else + +// Used for threadsafe refcounting +class Mutex +{ +public: + + class AutoLock + { + public: + AutoLock( Mutex& mutex ){} + ~AutoLock() {} + }; + + bool TryLock () { return true; } + void Lock () { } + void Unlock () { } + bool IsLocked () { return false; } + void BlockUntilUnlocked() { } +}; + +#endif //SUPPORT_THREADS +#endif |