浏览代码

Specialized implementation for mutexes and shared mutexes for Platform Blue (#22)

jrouwe 3 年之前
父节点
当前提交
a61dc67503
共有 1 个文件被更改,包括 105 次插入13 次删除
  1. 105 13
      Jolt/Core/Mutex.h

+ 105 - 13
Jolt/Core/Mutex.h

@@ -7,20 +7,112 @@
 #include <shared_mutex>
 #include <thread>
 #include <Core/Profiler.h>
+#include <Core/NonCopyable.h>
 
 namespace JPH {
 
+#ifdef JPH_PLATFORM_BLUE
+
+// On Platform Blue the mutex class is not very fast so we implement it using the official APIs
+class MutexBase : public NonCopyable
+{
+public:
+					MutexBase()
+	{
+		JPH_PLATFORM_BLUE_MUTEX_INIT(mMutex);
+	}
+
+					~MutexBase()
+	{
+		JPH_PLATFORM_BLUE_MUTEX_DESTROY(mMutex);
+	}
+
+	inline bool		try_lock()
+	{
+		return JPH_PLATFORM_BLUE_MUTEX_TRYLOCK(mMutex);
+	}
+
+	inline void		lock()
+	{
+		JPH_PLATFORM_BLUE_MUTEX_LOCK(mMutex);
+	}
+
+	inline void		unlock()
+	{
+		JPH_PLATFORM_BLUE_MUTEX_UNLOCK(mMutex);
+	}
+
+private:
+	JPH_PLATFORM_BLUE_MUTEX		mMutex;
+};
+
+// On Platform Blue the shared_mutex class is not very fast so we implement it using the official APIs
+class SharedMutexBase : public NonCopyable
+{
+public:
+					SharedMutexBase()
+	{
+		JPH_PLATFORM_BLUE_RWLOCK_INIT(mRWLock);
+	}
+
+					~SharedMutexBase()
+	{
+		JPH_PLATFORM_BLUE_RWLOCK_DESTROY(mRWLock);
+	}
+
+	inline bool		try_lock()
+	{
+		return JPH_PLATFORM_BLUE_RWLOCK_TRYWLOCK(mRWLock);
+	}
+
+	inline bool		try_lock_shared()
+	{
+		return JPH_PLATFORM_BLUE_RWLOCK_TRYRLOCK(mRWLock);
+	}
+
+	inline void		lock()
+	{
+		JPH_PLATFORM_BLUE_RWLOCK_WLOCK(mRWLock);
+	}
+
+	inline void		unlock()
+	{
+		JPH_PLATFORM_BLUE_RWLOCK_WUNLOCK(mRWLock);
+	}
+
+	inline void		lock_shared()
+	{
+		JPH_PLATFORM_BLUE_RWLOCK_RLOCK(mRWLock);
+	}
+
+	inline void		unlock_shared()
+	{
+		JPH_PLATFORM_BLUE_RWLOCK_RUNLOCK(mRWLock);
+	}
+
+private:
+	JPH_PLATFORM_BLUE_RWLOCK	mRWLock;
+};
+
+#else
+
+// On other platforms just use the STL implementation
+using MutexBase = mutex;
+using SharedMutexBase = shared_mutex;
+
+#endif // JPH_PLATFORM_BLUE
+
 #if defined(JPH_ENABLE_ASSERTS) || defined(JPH_PROFILE_ENABLED) || defined(JPH_EXTERNAL_PROFILE)
 
-/// Very simple wrapper around std::mutex which tracks lock contention in the profiler 
+/// Very simple wrapper around MutexBase which tracks lock contention in the profiler 
 /// and asserts that locks/unlocks take place on the same thread
-class Mutex : public mutex
+class Mutex : public MutexBase
 {
 public:
 	inline bool		try_lock()
 	{
 		JPH_ASSERT(mLockedThreadID != this_thread::get_id());
-		if (mutex::try_lock())
+		if (MutexBase::try_lock())
 		{
 			JPH_IF_ENABLE_ASSERTS(mLockedThreadID = this_thread::get_id();)
 			return true;
@@ -33,7 +125,7 @@ public:
 		if (!try_lock())
 		{
 			JPH_PROFILE("Lock", 0xff00ffff);
-			mutex::lock();
+			MutexBase::lock();
 			JPH_IF_ENABLE_ASSERTS(mLockedThreadID = this_thread::get_id();)
 		}
 	}
@@ -42,7 +134,7 @@ public:
 	{
 		JPH_ASSERT(mLockedThreadID == this_thread::get_id());
 		JPH_IF_ENABLE_ASSERTS(mLockedThreadID = thread::id();)
-		mutex::unlock();
+		MutexBase::unlock();
 	}
 
 #ifdef JPH_ENABLE_ASSERTS
@@ -56,15 +148,15 @@ private:
 	JPH_IF_ENABLE_ASSERTS(thread::id mLockedThreadID;)
 };
 
-/// Very simple wrapper around std::shared_mutex which tracks lock contention in the profiler
+/// Very simple wrapper around SharedMutexBase which tracks lock contention in the profiler
 /// and asserts that locks/unlocks take place on the same thread
-class SharedMutex : public shared_mutex
+class SharedMutex : public SharedMutexBase
 {
 public:
 	inline bool		try_lock()
 	{
 		JPH_ASSERT(mLockedThreadID != this_thread::get_id());
-		if (shared_mutex::try_lock())
+		if (SharedMutexBase::try_lock())
 		{
 			JPH_IF_ENABLE_ASSERTS(mLockedThreadID = this_thread::get_id();)
 			return true;
@@ -77,7 +169,7 @@ public:
 		if (!try_lock())
 		{
 			JPH_PROFILE("Lock", 0xff00ffff);
-			shared_mutex::lock();
+			SharedMutexBase::lock();
 			JPH_IF_ENABLE_ASSERTS(mLockedThreadID = this_thread::get_id();)
 		}
 	}
@@ -86,7 +178,7 @@ public:
 	{
 		JPH_ASSERT(mLockedThreadID == this_thread::get_id());
 		JPH_IF_ENABLE_ASSERTS(mLockedThreadID = thread::id();)
-		shared_mutex::unlock();
+		SharedMutexBase::unlock();
 	}
 
 #ifdef JPH_ENABLE_ASSERTS
@@ -101,7 +193,7 @@ public:
 		if (!try_lock_shared())
 		{
 			JPH_PROFILE("LockShared", 0xff00ffff);
-			shared_mutex::lock_shared();
+			SharedMutexBase::lock_shared();
 		}
 	}
 
@@ -111,8 +203,8 @@ private:
 
 #else
 
-using Mutex = mutex;
-using SharedMutex = shared_mutex;
+using Mutex = MutexBase;
+using SharedMutex = SharedMutexBase;
 
 #endif