Mutex.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/Profiler.h>
  6. #include <Jolt/Core/NonCopyable.h>
  7. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  8. #include <mutex>
  9. #include <shared_mutex>
  10. #include <thread>
  11. JPH_SUPPRESS_WARNINGS_STD_END
  12. JPH_NAMESPACE_BEGIN
  13. // Things we're using from STL
  14. using std::mutex;
  15. using std::shared_mutex;
  16. using std::thread;
  17. using std::lock_guard;
  18. using std::shared_lock;
  19. using std::unique_lock;
  20. #ifdef JPH_PLATFORM_BLUE
  21. // On Platform Blue the mutex class is not very fast so we implement it using the official APIs
  22. class MutexBase : public NonCopyable
  23. {
  24. public:
  25. MutexBase()
  26. {
  27. JPH_PLATFORM_BLUE_MUTEX_INIT(mMutex);
  28. }
  29. ~MutexBase()
  30. {
  31. JPH_PLATFORM_BLUE_MUTEX_DESTROY(mMutex);
  32. }
  33. inline bool try_lock()
  34. {
  35. return JPH_PLATFORM_BLUE_MUTEX_TRYLOCK(mMutex);
  36. }
  37. inline void lock()
  38. {
  39. JPH_PLATFORM_BLUE_MUTEX_LOCK(mMutex);
  40. }
  41. inline void unlock()
  42. {
  43. JPH_PLATFORM_BLUE_MUTEX_UNLOCK(mMutex);
  44. }
  45. private:
  46. JPH_PLATFORM_BLUE_MUTEX mMutex;
  47. };
  48. // On Platform Blue the shared_mutex class is not very fast so we implement it using the official APIs
  49. class SharedMutexBase : public NonCopyable
  50. {
  51. public:
  52. SharedMutexBase()
  53. {
  54. JPH_PLATFORM_BLUE_RWLOCK_INIT(mRWLock);
  55. }
  56. ~SharedMutexBase()
  57. {
  58. JPH_PLATFORM_BLUE_RWLOCK_DESTROY(mRWLock);
  59. }
  60. inline bool try_lock()
  61. {
  62. return JPH_PLATFORM_BLUE_RWLOCK_TRYWLOCK(mRWLock);
  63. }
  64. inline bool try_lock_shared()
  65. {
  66. return JPH_PLATFORM_BLUE_RWLOCK_TRYRLOCK(mRWLock);
  67. }
  68. inline void lock()
  69. {
  70. JPH_PLATFORM_BLUE_RWLOCK_WLOCK(mRWLock);
  71. }
  72. inline void unlock()
  73. {
  74. JPH_PLATFORM_BLUE_RWLOCK_WUNLOCK(mRWLock);
  75. }
  76. inline void lock_shared()
  77. {
  78. JPH_PLATFORM_BLUE_RWLOCK_RLOCK(mRWLock);
  79. }
  80. inline void unlock_shared()
  81. {
  82. JPH_PLATFORM_BLUE_RWLOCK_RUNLOCK(mRWLock);
  83. }
  84. private:
  85. JPH_PLATFORM_BLUE_RWLOCK mRWLock;
  86. };
  87. #else
  88. // On other platforms just use the STL implementation
  89. using MutexBase = mutex;
  90. using SharedMutexBase = shared_mutex;
  91. #endif // JPH_PLATFORM_BLUE
  92. #if defined(JPH_ENABLE_ASSERTS) || defined(JPH_PROFILE_ENABLED) || defined(JPH_EXTERNAL_PROFILE)
  93. /// Very simple wrapper around MutexBase which tracks lock contention in the profiler
  94. /// and asserts that locks/unlocks take place on the same thread
  95. class Mutex : public MutexBase
  96. {
  97. public:
  98. inline bool try_lock()
  99. {
  100. JPH_ASSERT(mLockedThreadID != std::this_thread::get_id());
  101. if (MutexBase::try_lock())
  102. {
  103. JPH_IF_ENABLE_ASSERTS(mLockedThreadID = std::this_thread::get_id();)
  104. return true;
  105. }
  106. return false;
  107. }
  108. inline void lock()
  109. {
  110. if (!try_lock())
  111. {
  112. JPH_PROFILE("Lock", 0xff00ffff);
  113. MutexBase::lock();
  114. JPH_IF_ENABLE_ASSERTS(mLockedThreadID = std::this_thread::get_id();)
  115. }
  116. }
  117. inline void unlock()
  118. {
  119. JPH_ASSERT(mLockedThreadID == std::this_thread::get_id());
  120. JPH_IF_ENABLE_ASSERTS(mLockedThreadID = thread::id();)
  121. MutexBase::unlock();
  122. }
  123. #ifdef JPH_ENABLE_ASSERTS
  124. inline bool is_locked()
  125. {
  126. return mLockedThreadID != thread::id();
  127. }
  128. #endif // JPH_ENABLE_ASSERTS
  129. private:
  130. JPH_IF_ENABLE_ASSERTS(thread::id mLockedThreadID;)
  131. };
  132. /// Very simple wrapper around SharedMutexBase which tracks lock contention in the profiler
  133. /// and asserts that locks/unlocks take place on the same thread
  134. class SharedMutex : public SharedMutexBase
  135. {
  136. public:
  137. inline bool try_lock()
  138. {
  139. JPH_ASSERT(mLockedThreadID != std::this_thread::get_id());
  140. if (SharedMutexBase::try_lock())
  141. {
  142. JPH_IF_ENABLE_ASSERTS(mLockedThreadID = std::this_thread::get_id();)
  143. return true;
  144. }
  145. return false;
  146. }
  147. inline void lock()
  148. {
  149. if (!try_lock())
  150. {
  151. JPH_PROFILE("WLock", 0xff00ffff);
  152. SharedMutexBase::lock();
  153. JPH_IF_ENABLE_ASSERTS(mLockedThreadID = std::this_thread::get_id();)
  154. }
  155. }
  156. inline void unlock()
  157. {
  158. JPH_ASSERT(mLockedThreadID == std::this_thread::get_id());
  159. JPH_IF_ENABLE_ASSERTS(mLockedThreadID = thread::id();)
  160. SharedMutexBase::unlock();
  161. }
  162. #ifdef JPH_ENABLE_ASSERTS
  163. inline bool is_locked()
  164. {
  165. return mLockedThreadID != thread::id();
  166. }
  167. #endif // JPH_ENABLE_ASSERTS
  168. inline void lock_shared()
  169. {
  170. if (!try_lock_shared())
  171. {
  172. JPH_PROFILE("RLock", 0xff00ffff);
  173. SharedMutexBase::lock_shared();
  174. }
  175. }
  176. private:
  177. JPH_IF_ENABLE_ASSERTS(thread::id mLockedThreadID;)
  178. };
  179. #else
  180. using Mutex = MutexBase;
  181. using SharedMutex = SharedMutexBase;
  182. #endif
  183. JPH_NAMESPACE_END