2
0
Эх сурвалжийг харах

Replacing ugly thread primitive macros with a non-macro alternative

BearishSun 9 жил өмнө
parent
commit
d05d39222f
56 өөрчлөгдсөн 406 нэмэгдсэн , 350 устгасан
  1. 81 0
      Documentation/Manuals/Native/utilities.md
  2. 10 10
      Source/BansheeCore/Include/BsCommandQueue.h
  3. 4 4
      Source/BansheeCore/Include/BsCoreApplication.h
  4. 2 2
      Source/BansheeCore/Include/BsCoreObjectCore.h
  5. 1 1
      Source/BansheeCore/Include/BsCoreObjectManager.h
  6. 10 10
      Source/BansheeCore/Include/BsCoreThread.h
  7. 1 1
      Source/BansheeCore/Include/BsCoreThreadAccessor.h
  8. 1 1
      Source/BansheeCore/Include/BsOSInputHandler.h
  9. 1 1
      Source/BansheeCore/Include/BsProfilerCPU.h
  10. 1 1
      Source/BansheeCore/Include/BsProfilerGPU.h
  11. 1 1
      Source/BansheeCore/Include/BsProfilingManager.h
  12. 1 1
      Source/BansheeCore/Include/BsRenderStateManager.h
  13. 2 2
      Source/BansheeCore/Include/BsRenderWindowManager.h
  14. 2 2
      Source/BansheeCore/Include/BsResourceHandle.h
  15. 1 1
      Source/BansheeCore/Include/BsResourceListenerManager.h
  16. 2 2
      Source/BansheeCore/Include/BsResources.h
  17. 1 1
      Source/BansheeCore/Include/Win32/BSWin32PlatformData.h
  18. 7 7
      Source/BansheeCore/Include/Win32/BsWin32DropTarget.h
  19. 5 5
      Source/BansheeCore/Source/BsCommandQueue.cpp
  20. 6 6
      Source/BansheeCore/Source/BsCoreApplication.cpp
  21. 6 6
      Source/BansheeCore/Source/BsCoreObjectCore.cpp
  22. 10 10
      Source/BansheeCore/Source/BsCoreObjectManager.cpp
  23. 20 20
      Source/BansheeCore/Source/BsCoreThread.cpp
  24. 8 8
      Source/BansheeCore/Source/BsOSInputHandler.cpp
  25. 2 2
      Source/BansheeCore/Source/BsPhysics.cpp
  26. 2 2
      Source/BansheeCore/Source/BsProfilerCPU.cpp
  27. 3 3
      Source/BansheeCore/Source/BsProfilerGPU.cpp
  28. 2 2
      Source/BansheeCore/Source/BsProfilingManager.cpp
  29. 9 9
      Source/BansheeCore/Source/BsRenderStateManager.cpp
  30. 14 14
      Source/BansheeCore/Source/BsRenderWindowManager.cpp
  31. 6 6
      Source/BansheeCore/Source/BsResourceHandle.cpp
  32. 6 6
      Source/BansheeCore/Source/BsResourceListenerManager.cpp
  33. 22 22
      Source/BansheeCore/Source/BsResources.cpp
  34. 24 25
      Source/BansheeCore/Source/Win32/BsWin32FolderMonitor.cpp
  35. 12 12
      Source/BansheeCore/Source/Win32/BsWin32Platform.cpp
  36. 1 1
      Source/BansheeD3D9RenderAPI/Include/BsD3D9Resource.h
  37. 1 1
      Source/BansheeD3D9RenderAPI/Include/BsD3D9ResourceManager.h
  38. 1 1
      Source/BansheeD3D9RenderAPI/Source/BsD3D9Resource.cpp
  39. 6 6
      Source/BansheeD3D9RenderAPI/Source/BsD3D9ResourceManager.cpp
  40. 3 3
      Source/BansheeEngine/Source/BsRendererMaterialManager.cpp
  41. 2 2
      Source/BansheeUtility/Include/BsAsyncOp.h
  42. 7 7
      Source/BansheeUtility/Include/BsEvent.h
  43. 2 2
      Source/BansheeUtility/Include/BsFrameAlloc.h
  44. 1 1
      Source/BansheeUtility/Include/BsLog.h
  45. 4 4
      Source/BansheeUtility/Include/BsTaskScheduler.h
  46. 19 42
      Source/BansheeUtility/Include/BsThreadDefines.h
  47. 6 6
      Source/BansheeUtility/Include/BsThreadPool.h
  48. 4 4
      Source/BansheeUtility/Source/BsAsyncOp.cpp
  49. 1 1
      Source/BansheeUtility/Source/BsFrameAlloc.cpp
  50. 6 6
      Source/BansheeUtility/Source/BsLog.cpp
  51. 16 16
      Source/BansheeUtility/Source/BsTaskScheduler.cpp
  52. 30 31
      Source/BansheeUtility/Source/BsThreadPool.cpp
  53. 2 2
      Source/BansheeUtility/Source/Win32/BsWin32CrashHandler.cpp
  54. 5 5
      Source/BansheeUtility/Source/Win32/BsWin32Window.cpp
  55. 1 1
      Source/SBansheeEngine/Include/BsScriptObjectManager.h
  56. 2 2
      Source/SBansheeEngine/Source/BsScriptObjectManager.cpp

+ 81 - 0
Documentation/Manuals/Native/utilities.md

@@ -171,8 +171,89 @@ Conversion between various types (like int, float, bool, etc.) and string is pro
 
 # Threading {#utilities_i}
 ## Primitives {#utilities_i_a}
+The primitives perform the most basic operations related to threading. All threading primitives use the standard C++ library constructs, so for more information you should read their documentation.
+
+### Thread {#utilities_i_a_a}
+To create a new thread use @ref BansheeEngine::Thread "Thread", like so:
+~~~~~~~~~~~~~{.cpp}
+void workerFunc()
+{
+	// This runs on another thread
+}
+
+Thread myThread(&workerFunc);
+~~~~~~~~~~~~~
+
+### Mutex {#utilities_i_a_b}
+Use @ref BansheeEngine::Mutex "Mutex" and @ref BansheeEngine::Lock "Lock" to synchronize access between multiple threads, like so:
+~~~~~~~~~~~~~{.cpp}
+Vector<int> output;
+int startIdx = 0;
+Mutex mutex;
+
+void workerFunc()
+{
+	// Lock the mutex before modifying either "output" or "startIdx"
+	// This ensures only one thread every accesses it at once
+	Lock lock(mutex);
+	output.push_back(startIdx++);
+}
+
+// Start two threads that write to "output"
+Thread threadA(&workerFunc);
+Thread threadB(&workerFunc);
+~~~~~~~~~~~~~
+
+If a mutex can be locked recursively, use @ref BansheeEngine::RecursiveMutex "RecursiveMutex" and @ref BansheeEngine::RecursiveLock "RecursiveLock" instead.
+
+### Signal {#utilities_i_a_c}
+Use @ref BansheeEngine::Signal "Signal" to pause thread execution until another thread reaches a certain point. For example:
+~~~~~~~~~~~~~{.cpp}
+bool isReady = false;
+int result = 0;
+
+Signal signal;
+Mutex mutex;
+
+void workerFunc()
+{
+	for(int i = 0; i < 100000; i++)
+		result += i; // Or some more complex calculation
+	
+	// Lock the mutex so we can safely modify isReady
+	{
+		Lock lock(mutex);
+		isReady = true;		
+	} // Automatically unlocked when lock goes out of scope
+	
+	// Notify everyone waiting that the signal is ready
+	signal.notify_all();
+}
+
+// Wait until the signal is triggered, or until isReady is set to true, whichever comes first
+Lock lock(mutex);
+if(!isReady)
+	signal.wait_for(lock);
+~~~~~~~~~~~~~
+
+### Other {#utilities_i_a_d}
+The previous sections covered all the primitives, but there is some more useful functionality to be aware of:
+ - @ref BS_THREAD_HARDWARE_CONCURRENCY - Returns number of logical CPU cores.
+ - @ref BS_THREAD_CURRENT_ID - Returns @ref BansheeEngine::ThreadId "ThreadId" of the current thread.
+ - @ref BS_THREAD_SLEEP - Pauses the current thread for a set number of milliseconds.
 
 ## Thread pool {#utilities_i_b}
+Instead of using thread primitive described in the previous section, you should instead use @ref BansheeEngine::ThreadPool "ThreadPool" for running threads. @ref BansheeEngine::ThreadPool "ThreadPool" allows you to re-use threads and avoid paying the cost of thread creation and destruction. It keeps any thread that was retired in idle state, and will re-use it when user requests a new thread.
+
+An example:
+~~~~~~~~~~~~~{.cpp}
+void workerFunc()
+{
+	// This runs on another thread
+}
+
+ThreadPool::instance().run("MyThread", &workerFunc);
+~~~~~~~~~~~~~
 
 ## Task scheduler {#utilities_i_c}
 

+ 10 - 10
Source/BansheeCore/Include/BsCommandQueue.h

@@ -22,7 +22,7 @@ namespace BansheeEngine
 		CommandQueueNoSync() {}
 		virtual ~CommandQueueNoSync() {}
 
-		bool isValidThread(BS_THREAD_ID_TYPE ownerThread) const
+		bool isValidThread(ThreadId ownerThread) const
 		{
 			return BS_THREAD_CURRENT_ID == ownerThread;
 		}
@@ -39,11 +39,11 @@ namespace BansheeEngine
 	{
 	public:
 		CommandQueueSync()
-			:mLock(mCommandQueueMutex, BS_DEFER_LOCK)
+			:mLock(mCommandQueueMutex, std::defer_lock)
 		{ }
 		virtual ~CommandQueueSync() {}
 
-		bool isValidThread(BS_THREAD_ID_TYPE ownerThread) const
+		bool isValidThread(ThreadId ownerThread) const
 		{
 			return true;
 		}
@@ -59,8 +59,8 @@ namespace BansheeEngine
 		}
 
 	private:
-		BS_MUTEX(mCommandQueueMutex);
-		BS_LOCK_TYPE mLock;
+		Mutex mCommandQueueMutex;
+		Lock mLock;
 	};
 
 	/**
@@ -144,7 +144,7 @@ namespace BansheeEngine
 		 *
 		 * @param[in]	threadId	   	Identifier for the thread the command queue will be getting commands from.					
 		 */
-		CommandQueueBase(BS_THREAD_ID_TYPE threadId);
+		CommandQueueBase(ThreadId threadId);
 		virtual ~CommandQueueBase();
 
 		/**
@@ -153,7 +153,7 @@ namespace BansheeEngine
 		 * @note	If the command queue is using a synchonized access policy generally this is not relevant as it may be 
 		 *			used on multiple threads.
 		 */
-		BS_THREAD_ID_TYPE getThreadId() const { return mMyThreadId; }
+		ThreadId getThreadId() const { return mMyThreadId; }
 
 		/**
 		 * Executes all provided commands one by one in order. To get the commands you should call flush().
@@ -239,7 +239,7 @@ namespace BansheeEngine
 		Stack<Queue<QueuedCommand>*> mEmptyCommandQueues; /**< List of empty queues for reuse. */
 
 		SPtr<AsyncOpSyncData> mAsyncOpSyncData;
-		BS_THREAD_ID_TYPE mMyThreadId;
+		ThreadId mMyThreadId;
 
 		// Various variables that allow for easier debugging by allowing us to trigger breakpoints
 		// when a certain command was queued.
@@ -273,7 +273,7 @@ namespace BansheeEngine
 		
 		static UINT32 MaxCommandQueueIdx;
 		static UnorderedSet<QueueBreakpoint, QueueBreakpoint::HashFunction, QueueBreakpoint::EqualFunction> SetBreakpoints;
-		BS_STATIC_MUTEX(CommandQueueBreakpointMutex);
+		static Mutex CommandQueueBreakpointMutex;
 
 		/** Checks if the specified command has a breakpoint and throw an assert if it does. */
 		static void breakIfNeeded(UINT32 queueIdx, UINT32 commandIdx);
@@ -291,7 +291,7 @@ namespace BansheeEngine
 	{
 	public:
 		/** @copydoc CommandQueueBase::CommandQueueBase */
-		CommandQueue(BS_THREAD_ID_TYPE threadId)
+		CommandQueue(ThreadId threadId)
 			:CommandQueueBase(threadId)
 		{ }
 

+ 4 - 4
Source/BansheeCore/Include/BsCoreApplication.h

@@ -67,7 +67,7 @@ namespace BansheeEngine
 			 *
 			 * @note	Thread safe.
 			 */
-			BS_THREAD_ID_TYPE getSimThreadId() { return mSimThreadId; }
+			ThreadId getSimThreadId() { return mSimThreadId; }
 
 			/**	Returns true if the application is running in an editor, false if standalone. */
 			virtual bool isEditor() const { return false; }
@@ -125,9 +125,9 @@ namespace BansheeEngine
 		Map<DynLib*, UpdatePluginFunc> mPluginUpdateFunctions;
 
 		bool mIsFrameRenderingFinished;
-		BS_MUTEX(mFrameRenderingFinishedMutex);
-		BS_THREAD_SYNCHRONISER(mFrameRenderingFinishedCondition);
-		BS_THREAD_ID_TYPE mSimThreadId;
+		Mutex mFrameRenderingFinishedMutex;
+		Signal mFrameRenderingFinishedCondition;
+		ThreadId mSimThreadId;
 
 		volatile bool mRunMainLoop;
 	};

+ 2 - 2
Source/BansheeCore/Include/BsCoreObjectCore.h

@@ -85,8 +85,8 @@ namespace BansheeEngine
 		volatile UINT8 mFlags;
 		std::weak_ptr<CoreObjectCore> mThis;
 
-		BS_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
-		BS_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
+		static Signal mCoreGpuObjectLoadedCondition;
+		static Mutex mCoreGpuObjectLoadedMutex;
 	};
 
 	/** @} */

+ 1 - 1
Source/BansheeCore/Include/BsCoreObjectManager.h

@@ -134,7 +134,7 @@ namespace BansheeEngine
 		Vector<CoreStoredSyncObjData> mDestroyedSyncData;
 		List<CoreStoredSyncData> mCoreSyncData;
 
-		BS_MUTEX(mObjectsMutex);
+		Mutex mObjectsMutex;
 	};
 
 	/** @} */

+ 10 - 10
Source/BansheeCore/Include/BsCoreThread.h

@@ -54,7 +54,7 @@ public:
 	~CoreThread();
 
 	/** Returns the id of the core thread.  */
-	BS_THREAD_ID_TYPE getCoreThreadId() { return mCoreThreadId; }
+	ThreadId getCoreThreadId() { return mCoreThreadId; }
 
 	/**
 	 * Creates or retrieves an accessor that you can use for executing commands on the core thread from a non-core thread. 
@@ -139,15 +139,15 @@ private:
 
 	HThread mCoreThread;
 	bool mCoreThreadStarted;
-	BS_THREAD_ID_TYPE mSimThreadId;
-	BS_THREAD_ID_TYPE mCoreThreadId;
-	BS_MUTEX(mCommandQueueMutex)
-	BS_MUTEX(mAccessorMutex)
-	BS_THREAD_SYNCHRONISER(mCommandReadyCondition)
-	BS_MUTEX(mCommandNotifyMutex)
-	BS_THREAD_SYNCHRONISER(mCommandCompleteCondition)
-	BS_MUTEX(mThreadStartedMutex)
-	BS_THREAD_SYNCHRONISER(mCoreThreadStartedCondition)
+	ThreadId mSimThreadId;
+	ThreadId mCoreThreadId;
+	Mutex mCommandQueueMutex;
+	Mutex mAccessorMutex;
+	Signal mCommandReadyCondition;
+	Mutex mCommandNotifyMutex;
+	Signal mCommandCompleteCondition;
+	Mutex mThreadStartedMutex;
+	Signal mCoreThreadStartedCondition;
 
 	CommandQueue<CommandQueueSync>* mCommandQueue;
 

+ 1 - 1
Source/BansheeCore/Include/BsCoreThreadAccessor.h

@@ -64,7 +64,7 @@ namespace BansheeEngine
 		 *
 		 * @param[in]	threadId		Identifier for the thread that created the accessor.
 		 */
-		CoreThreadAccessor(BS_THREAD_ID_TYPE threadId)
+		CoreThreadAccessor(ThreadId threadId)
 			:CoreThreadAccessorBase(bs_new<CommandQueue<CommandQueueSyncPolicy>>(threadId))
 		{
 

+ 1 - 1
Source/BansheeCore/Include/BsOSInputHandler.h

@@ -81,7 +81,7 @@ namespace BansheeEngine
 		Event<void(InputCommandType)> onInputCommand;
 
 	private:
-		BS_MUTEX(mOSInputMutex);
+		Mutex mOSInputMutex;
 		Vector2I mLastCursorPos;
 		Vector2I mCursorPosition;
 		Vector2I mDelta;

+ 1 - 1
Source/BansheeCore/Include/BsProfilerCPU.h

@@ -321,7 +321,7 @@ namespace BansheeEngine
 		UINT64 mPreciseSamplingOverheadCycles;
 
 		ProfilerVector<ThreadInfo*> mActiveThreads;
-		BS_MUTEX(mThreadSync);
+		Mutex mThreadSync;
 	};
 
 	/** Profiling entry containing information about a single CPU profiling block containing timing information. */

+ 1 - 1
Source/BansheeCore/Include/BsProfilerGPU.h

@@ -175,7 +175,7 @@ namespace BansheeEngine
 		mutable Stack<SPtr<TimerQuery>> mFreeTimerQueries;
 		mutable Stack<SPtr<OcclusionQuery>> mFreeOcclusionQueries;
 
-		BS_MUTEX(mMutex);
+		Mutex mMutex;
 	};
 
 	/** @} */

+ 1 - 1
Source/BansheeCore/Include/BsProfilingManager.h

@@ -68,7 +68,7 @@ namespace BansheeEngine
 		ProfilerReport* mSavedCoreReports;
 		UINT32 mNextCoreReportIdx;
 
-		BS_MUTEX(mSync);
+		mutable Mutex mSync;
 	};
 
 	/** Easy way to access ProfilingManager. */

+ 1 - 1
Source/BansheeCore/Include/BsRenderStateManager.h

@@ -236,7 +236,7 @@ namespace BansheeEngine
 		mutable UINT32 mNextRasterizerStateId;
 		mutable UINT32 mNextDepthStencilStateId;
 
-		BS_MUTEX(mMutex);
+		mutable Mutex mMutex;
 	};
 
 	/** @} */

+ 2 - 2
Source/BansheeCore/Include/BsRenderWindowManager.h

@@ -76,7 +76,7 @@ namespace BansheeEngine
 		virtual SPtr<RenderWindow> createImpl(RENDER_WINDOW_DESC& desc, UINT32 windowId, const SPtr<RenderWindow>& parentWindow) = 0;
 
 	protected:
-		BS_MUTEX(mWindowMutex);
+		mutable Mutex mWindowMutex;
 		Map<UINT32, RenderWindow*> mWindows;
 
 		RenderWindow* mWindowInFocus;
@@ -122,7 +122,7 @@ namespace BansheeEngine
 		/**	Called by the core thread when window is destroyed. */
 		void windowDestroyed(RenderWindowCore* window);
 
-		BS_MUTEX(mWindowMutex);
+		mutable Mutex mWindowMutex;
 		Vector<RenderWindowCore*> mCreatedWindows;
 		UnorderedSet<RenderWindowCore*> mDirtyProperties;
 		std::atomic_uint mNextWindowId;

+ 2 - 2
Source/BansheeCore/Include/BsResourceHandle.h

@@ -102,8 +102,8 @@ namespace BansheeEngine
 	private:
 		friend class Resources;
 
-		BS_STATIC_THREAD_SYNCHRONISER(mResourceCreatedCondition)
-		BS_STATIC_MUTEX(mResourceCreatedMutex)
+		static Signal mResourceCreatedCondition;
+		static Mutex mResourceCreatedMutex;
 
 	protected:
 		inline void throwIfNotLoaded() const;

+ 1 - 1
Source/BansheeCore/Include/BsResourceListenerManager.h

@@ -75,7 +75,7 @@ namespace BansheeEngine
 
 		Vector<HResource> mTempResourceBuffer;
 
-		BS_RECURSIVE_MUTEX(mMutex);
+		RecursiveMutex mMutex;
 
 #if BS_DEBUG_MODE
 		Set<IResourceListener*> mActiveListeners;

+ 2 - 2
Source/BansheeCore/Include/BsResources.h

@@ -313,8 +313,8 @@ namespace BansheeEngine
 		Vector<SPtr<ResourceManifest>> mResourceManifests;
 		SPtr<ResourceManifest> mDefaultResourceManifest;
 
-		BS_MUTEX(mInProgressResourcesMutex);
-		BS_MUTEX(mLoadedResourceMutex);
+		Mutex mInProgressResourcesMutex;
+		Mutex mLoadedResourceMutex;
 
 		UnorderedMap<String, WeakResourceHandle<Resource>> mHandles;
 		UnorderedMap<String, LoadedResourceData> mLoadedResources;

+ 1 - 1
Source/BansheeCore/Include/Win32/BSWin32PlatformData.h

@@ -37,7 +37,7 @@ namespace BansheeEngine
 		bool mRequiresStartUp = false;
 		bool mRequiresShutDown = false;
 
-		BS_MUTEX(mSync);
+		Mutex mSync;
 	};
 
 	/** @} */

+ 7 - 7
Source/BansheeCore/Include/Win32/BsWin32DropTarget.h

@@ -60,7 +60,7 @@ namespace BansheeEngine
 
 		~Win32DropTarget()
 		{
-			BS_LOCK_MUTEX(mSync);
+			Lock lock(mSync);
 
 			for(auto& fileList : mFileLists)
 			{
@@ -137,7 +137,7 @@ namespace BansheeEngine
 				return S_OK;
 
 			{
-				BS_LOCK_MUTEX(mSync);
+				Lock lock(mSync);
 
 				mFileLists.push_back(getFileListFromData(pDataObj));
 
@@ -165,7 +165,7 @@ namespace BansheeEngine
 				return S_OK;
 
 			{
-				BS_LOCK_MUTEX(mSync);
+				Lock lock(mSync);
 
 				ScreenToClient(mHWnd, (POINT *)&pt);
 				mQueuedDropOps.push_back(DropTargetOp(DropOpType::DragOver, Vector2I((int)pt.x, (int)pt.y)));
@@ -186,7 +186,7 @@ namespace BansheeEngine
 		HRESULT __stdcall DragLeave() override
 		{
 			{
-				BS_LOCK_MUTEX(mSync);
+				Lock lock(mSync);
 
 				mQueuedDropOps.push_back(DropTargetOp(DropOpType::Leave, Vector2I()));
 
@@ -212,7 +212,7 @@ namespace BansheeEngine
 				return S_OK;
 
 			{
-				BS_LOCK_MUTEX(mSync);
+				Lock lock(mSync);
 
 				mFileLists.push_back(getFileListFromData(pDataObj));
 
@@ -262,7 +262,7 @@ namespace BansheeEngine
 		/** Called every frame by the sim thread. Internal method. */
 		void update()
 		{
-			BS_LOCK_MUTEX(mSync);
+			Lock lock(mSync);
 
 			for(auto& op: mQueuedDropOps)
 			{
@@ -379,7 +379,7 @@ namespace BansheeEngine
 		Vector<DropTargetOp> mQueuedDropOps;
 		Vector<Vector<WString>*> mFileLists; 
 
-		BS_MUTEX(mSync);
+		Mutex mSync;
 	};
 
 	/** @} */

+ 5 - 5
Source/BansheeCore/Source/BsCommandQueue.cpp

@@ -8,20 +8,20 @@
 namespace BansheeEngine
 {
 #if BS_DEBUG_MODE
-	CommandQueueBase::CommandQueueBase(BS_THREAD_ID_TYPE threadId)
+	CommandQueueBase::CommandQueueBase(ThreadId threadId)
 		:mMyThreadId(threadId), mMaxDebugIdx(0)
 	{
 		mAsyncOpSyncData = bs_shared_ptr_new<AsyncOpSyncData>();
 		mCommands = bs_new<BansheeEngine::Queue<QueuedCommand>>();
 
 		{
-			BS_LOCK_MUTEX(CommandQueueBreakpointMutex);
+			Lock lock(CommandQueueBreakpointMutex);
 
 			mCommandQueueIdx = MaxCommandQueueIdx++;
 		}
 	}
 #else
-	CommandQueueBase::CommandQueueBase(BS_THREAD_ID_TYPE threadId)
+	CommandQueueBase::CommandQueueBase(ThreadId threadId)
 		:mMyThreadId(threadId)
 	{
 		mAsyncOpSyncData = bs_shared_ptr_new<AsyncOpSyncData>();
@@ -164,7 +164,7 @@ namespace BansheeEngine
 	}
 
 #if BS_DEBUG_MODE
-	BS_STATIC_MUTEX_CLASS_INSTANCE(CommandQueueBreakpointMutex, CommandQueueBase);
+	Mutex CommandQueueBase::CommandQueueBreakpointMutex;
 
 	UINT32 CommandQueueBase::MaxCommandQueueIdx = 0;
 
@@ -186,7 +186,7 @@ namespace BansheeEngine
 
 	void CommandQueueBase::addBreakpoint(UINT32 queueIdx, UINT32 commandIdx)
 	{
-		BS_LOCK_MUTEX(CommandQueueBreakpointMutex);
+		Lock lock(CommandQueueBreakpointMutex);
 
 		SetBreakpoints.insert(QueueBreakpoint(queueIdx, commandIdx));
 	}

+ 6 - 6
Source/BansheeCore/Source/BsCoreApplication.cpp

@@ -238,12 +238,12 @@ namespace BansheeEngine
 			// thread, in which case sim thread needs to wait. Optimal solution would be to get an average 
 			// difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
 			{
-				BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
+				Lock lock(mFrameRenderingFinishedMutex);
 
 				while(!mIsFrameRenderingFinished)
 				{
 					TaskScheduler::instance().addWorker();
-					BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
+					mFrameRenderingFinishedCondition.wait(lock);
 					TaskScheduler::instance().removeWorker();
 				}
 
@@ -265,12 +265,12 @@ namespace BansheeEngine
 
 		// Wait until last core frame is finished before exiting
 		{
-			BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
+			Lock lock(mFrameRenderingFinishedMutex);
 
 			while (!mIsFrameRenderingFinished)
 			{
 				TaskScheduler::instance().addWorker();
-				BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
+				mFrameRenderingFinishedCondition.wait(lock);
 				TaskScheduler::instance().removeWorker();
 			}
 		}
@@ -304,10 +304,10 @@ namespace BansheeEngine
 
 	void CoreApplication::frameRenderingFinishedCallback()
 	{
-		BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
+		Lock lock(mFrameRenderingFinishedMutex);
 
 		mIsFrameRenderingFinished = true;
-		BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
+		mFrameRenderingFinishedCondition.notify_one();
 	}
 
 	void CoreApplication::startUpRenderer()

+ 6 - 6
Source/BansheeCore/Source/BsCoreObjectCore.cpp

@@ -5,8 +5,8 @@
 
 namespace BansheeEngine
 {
-	BS_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(mCoreGpuObjectLoadedCondition, CoreObjectCore)
-	BS_STATIC_MUTEX_CLASS_INSTANCE(mCoreGpuObjectLoadedMutex, CoreObjectCore)
+	Signal CoreObjectCore::mCoreGpuObjectLoadedCondition;
+	Mutex CoreObjectCore::mCoreGpuObjectLoadedMutex;
 
 	CoreObjectCore::CoreObjectCore()
 		:mFlags(0)
@@ -20,13 +20,13 @@ namespace BansheeEngine
 	void CoreObjectCore::initialize()
 	{
 		{
-			BS_LOCK_MUTEX(mCoreGpuObjectLoadedMutex);
+			Lock lock(mCoreGpuObjectLoadedMutex);
 			setIsInitialized(true);
 		}
 
 		setScheduledToBeInitialized(false);
 
-		BS_THREAD_NOTIFY_ALL(mCoreGpuObjectLoadedCondition);
+		mCoreGpuObjectLoadedCondition.notify_all();
 	}
 
 	void CoreObjectCore::synchronize()
@@ -38,13 +38,13 @@ namespace BansheeEngine
 				BS_EXCEPT(InternalErrorException, "You cannot call this method on the core thread. It will cause a deadlock!");
 #endif
 
-			BS_LOCK_MUTEX_NAMED(mCoreGpuObjectLoadedMutex, lock);
+			Lock lock(mCoreGpuObjectLoadedMutex);
 			while (!isInitialized())
 			{
 				if (!isScheduledToBeInitialized())
 					BS_EXCEPT(InternalErrorException, "Attempting to wait until initialization finishes but object is not scheduled to be initialized.");
 
-				BS_THREAD_WAIT(mCoreGpuObjectLoadedCondition, mCoreGpuObjectLoadedMutex, lock);
+				mCoreGpuObjectLoadedCondition.wait(lock);
 			}
 		}
 	}

+ 10 - 10
Source/BansheeCore/Source/BsCoreObjectManager.cpp

@@ -19,7 +19,7 @@ namespace BansheeEngine
 	CoreObjectManager::~CoreObjectManager()
 	{
 #if BS_DEBUG_MODE
-		BS_LOCK_MUTEX(mObjectsMutex);
+		Lock lock(mObjectsMutex);
 
 		if(mObjects.size() > 0)
 		{
@@ -37,7 +37,7 @@ namespace BansheeEngine
 	{
 		assert(object != nullptr);
 
-		BS_LOCK_MUTEX(mObjectsMutex);
+		Lock lock(mObjectsMutex);
 
 		mObjects[mNextAvailableID] = object;
 		mDirtyObjects[mNextAvailableID] = { object, -1 };
@@ -53,7 +53,7 @@ namespace BansheeEngine
 
 		// If dirty, we generate sync data before it is destroyed
 		{
-			BS_LOCK_MUTEX(mObjectsMutex);
+			Lock lock(mObjectsMutex);
 			bool isDirty = object->isCoreDirty() || (mDirtyObjects.find(internalId) != mDirtyObjects.end());
 
 			if (isDirty)
@@ -84,7 +84,7 @@ namespace BansheeEngine
 
 		// Clear dependencies from dependants
 		{
-			BS_LOCK_MUTEX(mObjectsMutex);
+			Lock lock(mObjectsMutex);
 
 			auto iterFind = mDependants.find(internalId);
 			if (iterFind != mDependants.end())
@@ -115,7 +115,7 @@ namespace BansheeEngine
 	{
 		UINT64 id = object->getInternalID();
 
-		BS_LOCK_MUTEX(mObjectsMutex);
+		Lock lock(mObjectsMutex);
 
 		mDirtyObjects[id] = { object, -1 };
 	}
@@ -137,7 +137,7 @@ namespace BansheeEngine
 			FrameVector<CoreObject*> toRemove;
 			FrameVector<CoreObject*> toAdd;
 
-			BS_LOCK_MUTEX(mObjectsMutex);
+			Lock lock(mObjectsMutex);
 
 			// Add dependencies and clear old dependencies from dependants
 			{
@@ -220,7 +220,7 @@ namespace BansheeEngine
 			FrameAlloc* allocator;
 		};
 
-		BS_LOCK_MUTEX(mObjectsMutex);
+		Lock lock(mObjectsMutex);
 
 		FrameAlloc* allocator = gCoreThread().getFrameAlloc();
 		Vector<IndividualCoreSyncData> syncData;
@@ -286,7 +286,7 @@ namespace BansheeEngine
 
 	void CoreObjectManager::syncDownload(FrameAlloc* allocator)
 	{
-		BS_LOCK_MUTEX(mObjectsMutex);
+		Lock lock(mObjectsMutex);
 
 		mCoreSyncData.push_back(CoreStoredSyncData());
 		CoreStoredSyncData& syncData = mCoreSyncData.back();
@@ -378,7 +378,7 @@ namespace BansheeEngine
 
 	void CoreObjectManager::syncUpload()
 	{
-		BS_LOCK_MUTEX(mObjectsMutex);
+		Lock lock(mObjectsMutex);
 
 		if (mCoreSyncData.size() == 0)
 			return;
@@ -403,7 +403,7 @@ namespace BansheeEngine
 
 	void CoreObjectManager::clearDirty()
 	{
-		BS_LOCK_MUTEX(mObjectsMutex);
+		Lock lock(mObjectsMutex);
 
 		FrameAlloc* allocator = gCoreThread().getFrameAlloc();
 		for (auto& objectData : mDirtyObjects)

+ 20 - 20
Source/BansheeCore/Source/BsCoreThread.cpp

@@ -40,7 +40,7 @@ namespace BansheeEngine
 		shutdownCoreThread();
 
 		{
-			BS_LOCK_MUTEX(mAccessorMutex);
+			Lock lock(mAccessorMutex);
 
 			for(auto& accessor : mAccessors)
 			{
@@ -70,10 +70,10 @@ namespace BansheeEngine
 		mCoreThread = ThreadPool::instance().run("Core", std::bind(&CoreThread::runCoreThread, this));
 		
 		// Need to wait to unsure thread ID is correctly set before continuing
-		BS_LOCK_MUTEX_NAMED(mThreadStartedMutex, lock)
+		Lock lock(mThreadStartedMutex);
 
-		while(!mCoreThreadStarted)
-			BS_THREAD_WAIT(mCoreThreadStartedCondition, mThreadStartedMutex, lock)
+		while (!mCoreThreadStarted)
+			mCoreThreadStartedCondition.wait(lock);
 #else
 		BS_EXCEPT(InternalErrorException, "Attempting to start a core thread but application isn't compiled with thread support.");
 #endif
@@ -86,21 +86,21 @@ namespace BansheeEngine
 		TaskScheduler::instance().removeWorker(); // One less worker because we are reserving one core for this thread
 
 		{
-			BS_LOCK_MUTEX(mThreadStartedMutex);
+			Lock lock(mThreadStartedMutex);
 
 			mCoreThreadStarted = true;
 			mCoreThreadId = BS_THREAD_CURRENT_ID;
 			mSyncedCoreAccessor = bs_new<CoreThreadAccessor<CommandQueueSync>>(BS_THREAD_CURRENT_ID);
 		}
 
-		BS_THREAD_NOTIFY_ONE(mCoreThreadStartedCondition);
+		mCoreThreadStartedCondition.notify_one();
 
 		while(true)
 		{
 			// Wait until we get some ready commands
 			Queue<QueuedCommand>* commands = nullptr;
 			{
-				BS_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
+				Lock lock(mCommandQueueMutex);
 
 				while(mCommandQueue->isEmpty())
 				{
@@ -112,7 +112,7 @@ namespace BansheeEngine
 					}
 
 					TaskScheduler::instance().addWorker(); // Do something else while we wait, otherwise this core will be unused
-					BS_THREAD_WAIT(mCommandReadyCondition, mCommandQueueMutex, lock);
+					mCommandReadyCondition.wait(lock);
 					TaskScheduler::instance().removeWorker();
 				}
 
@@ -130,12 +130,12 @@ namespace BansheeEngine
 #if !BS_FORCE_SINGLETHREADED_RENDERING
 
 		{
-			BS_LOCK_MUTEX(mCommandQueueMutex);
+			Lock lock(mCommandQueueMutex);
 			mCoreThreadShutdown = true;
 		}
 
 		// Wake all threads. They will quit after they see the shutdown flag
-		BS_THREAD_NOTIFY_ALL(mCommandReadyCondition);
+		mCommandReadyCondition.notify_all();
 
 		mCoreThreadId = BS_THREAD_CURRENT_ID;
 
@@ -152,7 +152,7 @@ namespace BansheeEngine
 			mAccessor.current->accessor = newAccessor;
 			mAccessor.current->isMain = BS_THREAD_CURRENT_ID == mSimThreadId;
 
-			BS_LOCK_MUTEX(mAccessorMutex);
+			Lock lock(mAccessorMutex);
 			mAccessors.push_back(mAccessor.current);
 		}
 
@@ -169,7 +169,7 @@ namespace BansheeEngine
 		Vector<AccessorContainer*> accessorCopies;
 
 		{
-			BS_LOCK_MUTEX(mAccessorMutex);
+			Lock lock(mAccessorMutex);
 
 			accessorCopies = mAccessors;
 		}
@@ -199,7 +199,7 @@ namespace BansheeEngine
 		AsyncOp op;
 		UINT32 commandId = -1;
 		{
-			BS_LOCK_MUTEX(mCommandQueueMutex);
+			Lock lock(mCommandQueueMutex);
 
 			if(blockUntilComplete)
 			{
@@ -210,7 +210,7 @@ namespace BansheeEngine
 				op = mCommandQueue->queueReturn(commandCallback);
 		}
 
-		BS_THREAD_NOTIFY_ALL(mCommandReadyCondition);
+		mCommandReadyCondition.notify_all();
 
 		if(blockUntilComplete)
 			blockUntilCommandCompleted(commandId);
@@ -224,7 +224,7 @@ namespace BansheeEngine
 
 		UINT32 commandId = -1;
 		{
-			BS_LOCK_MUTEX(mCommandQueueMutex);
+			Lock lock(mCommandQueueMutex);
 
 			if(blockUntilComplete)
 			{
@@ -235,7 +235,7 @@ namespace BansheeEngine
 				mCommandQueue->queue(commandCallback);
 		}
 
-		BS_THREAD_NOTIFY_ALL(mCommandReadyCondition);
+		mCommandReadyCondition.notify_all();
 
 		if(blockUntilComplete)
 			blockUntilCommandCompleted(commandId);
@@ -259,7 +259,7 @@ namespace BansheeEngine
 	void CoreThread::blockUntilCommandCompleted(UINT32 commandId)
 	{
 #if !BS_FORCE_SINGLETHREADED_RENDERING
-		BS_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
+		Lock lock(mCommandNotifyMutex);
 
 		while(true)
 		{
@@ -279,7 +279,7 @@ namespace BansheeEngine
 				break;
 			}
 
-			BS_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
+			mCommandCompleteCondition.wait(lock);
 		}
 #endif
 	}
@@ -287,12 +287,12 @@ namespace BansheeEngine
 	void CoreThread::commandCompletedNotify(UINT32 commandId)
 	{
 		{
-			BS_LOCK_MUTEX(mCommandNotifyMutex);
+			Lock lock(mCommandNotifyMutex);
 
 			mCommandsCompleted.push_back(commandId);
 		}
 
-		BS_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
+		mCommandCompleteCondition.notify_all();
 	}
 
 	CoreThread& gCoreThread()

+ 8 - 8
Source/BansheeCore/Source/BsOSInputHandler.cpp

@@ -44,7 +44,7 @@ namespace BansheeEngine
 		Queue<InputCommandType> inputCommands;
 
 		{
-			BS_LOCK_MUTEX(mOSInputMutex);
+			Lock lock(mOSInputMutex);
 			inputString = mInputString;
 			mInputString.clear();
 
@@ -179,14 +179,14 @@ namespace BansheeEngine
 
 	void OSInputHandler::charInput(UINT32 character)
 	{
-		BS_LOCK_MUTEX(mOSInputMutex);
+		Lock lock(mOSInputMutex);
 
 		mInputString += character;
 	}
 
 	void OSInputHandler::cursorMoved(const Vector2I& cursorPos, const OSPointerButtonStates& btnStates)
 	{
-		BS_LOCK_MUTEX(mOSInputMutex);
+		Lock lock(mOSInputMutex);
 
 		mCursorPosition = cursorPos;
 		mMouseMoveBtnState = btnStates;
@@ -195,7 +195,7 @@ namespace BansheeEngine
 	void OSInputHandler::cursorPressed(const Vector2I& cursorPos, 
 		OSMouseButton button, const OSPointerButtonStates& btnStates)
 	{
-		BS_LOCK_MUTEX(mOSInputMutex);
+		Lock lock(mOSInputMutex);
 
 		mButtonStates.push(ButtonStateChange());
 		ButtonStateChange& btnState = mButtonStates.back();
@@ -209,7 +209,7 @@ namespace BansheeEngine
 	void OSInputHandler::cursorReleased(const Vector2I& cursorPos, 
 		OSMouseButton button, const OSPointerButtonStates& btnStates)
 	{
-		BS_LOCK_MUTEX(mOSInputMutex);
+		Lock lock(mOSInputMutex);
 
 		mButtonStates.push(ButtonStateChange());
 		ButtonStateChange& btnState = mButtonStates.back();
@@ -222,7 +222,7 @@ namespace BansheeEngine
 
 	void OSInputHandler::cursorDoubleClick(const Vector2I& cursorPos, const OSPointerButtonStates& btnStates)
 	{
-		BS_LOCK_MUTEX(mOSInputMutex);
+		Lock lock(mOSInputMutex);
 
 		mDoubleClicks.push(DoubleClick());
 		DoubleClick& btnState = mDoubleClicks.back();
@@ -233,14 +233,14 @@ namespace BansheeEngine
 
 	void OSInputHandler::inputCommandEntered(InputCommandType commandType)
 	{
-		BS_LOCK_MUTEX(mOSInputMutex);
+		Lock lock(mOSInputMutex);
 
 		mInputCommands.push(commandType);
 	}
 
 	void OSInputHandler::mouseWheelScrolled(float scrollPos)
 	{
-		BS_LOCK_MUTEX(mOSInputMutex);
+		Lock lock(mOSInputMutex);
 
 		mMouseScroll = scrollPos;
 	}

+ 2 - 2
Source/BansheeCore/Source/BsPhysics.cpp

@@ -16,7 +16,7 @@ namespace BansheeEngine
 	{
 		assert(groupA < CollisionMapSize && groupB < CollisionMapSize);
 
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 		mCollisionMap[groupA][groupB] = enabled;
 	}
 
@@ -31,7 +31,7 @@ namespace BansheeEngine
 			Flag3 = 1 << 2
 		};
 
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 		return mCollisionMap[groupA][groupB];
 	}
 

+ 2 - 2
Source/BansheeCore/Source/BsProfilerCPU.cpp

@@ -277,7 +277,7 @@ namespace BansheeEngine
 	{
 		reset();
 
-		BS_LOCK_MUTEX(mThreadSync);
+		Lock lock(mThreadSync);
 
 		for(auto& threadInfo : mActiveThreads)
 			bs_delete<ThreadInfo, ProfilerAlloc>(threadInfo);
@@ -292,7 +292,7 @@ namespace BansheeEngine
 			thread = ThreadInfo::activeThread;
 
 			{
-				BS_LOCK_MUTEX(mThreadSync);
+				Lock lock(mThreadSync);
 
 				mActiveThreads.push_back(thread);
 			}

+ 3 - 3
Source/BansheeCore/Source/BsProfilerGPU.cpp

@@ -74,14 +74,14 @@ namespace BansheeEngine
 
 	UINT32 ProfilerGPU::getNumAvailableReports()
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		return (UINT32)mReadyReports.size();
 	}
 
 	GPUProfilerReport ProfilerGPU::getNextReport()
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		if (mReadyReports.empty())
 			BS_EXCEPT(InvalidStateException, "No reports are available.")
@@ -106,7 +106,7 @@ namespace BansheeEngine
 				mUnresolvedFrames.pop();
 
 				{
-					BS_LOCK_MUTEX(mMutex);
+					Lock lock(mMutex);
 					mReadyReports.push(report);
 				}
 			}

+ 2 - 2
Source/BansheeCore/Source/BsProfilingManager.cpp

@@ -38,7 +38,7 @@ namespace BansheeEngine
 	void ProfilingManager::_updateCore()
 	{
 #if BS_PROFILING_ENABLED
-		BS_LOCK_MUTEX(mSync);
+		Lock lock(mSync);
 		mSavedCoreReports[mNextCoreReportIdx].cpuReport = gProfilerCPU().generateReport();
 
 		gProfilerCPU().reset();
@@ -53,7 +53,7 @@ namespace BansheeEngine
 
 		if(thread == ProfiledThread::Core)
 		{
-			BS_LOCK_MUTEX(mSync);
+			Lock lock(mSync);
 
 			UINT32 reportIdx = mNextCoreReportIdx + (UINT32)((INT32)NUM_SAVED_FRAMES - ((INT32)idx + 1));
 			reportIdx = (reportIdx) % NUM_SAVED_FRAMES;

+ 9 - 9
Source/BansheeCore/Source/BsRenderStateManager.cpp

@@ -284,42 +284,42 @@ namespace BansheeEngine
 
 	void RenderStateCoreManager::notifySamplerStateCreated(const SAMPLER_STATE_DESC& desc, const SPtr<SamplerStateCore>& state) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		mCachedSamplerStates[desc] = state;
 	}
 
 	void RenderStateCoreManager::notifyBlendStateCreated(const BLEND_STATE_DESC& desc, const CachedBlendState& state) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		mCachedBlendStates[desc] = state;
 	}
 
 	void RenderStateCoreManager::notifyRasterizerStateCreated(const RASTERIZER_STATE_DESC& desc, const CachedRasterizerState& state) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		mCachedRasterizerStates[desc] = state;
 	}
 
 	void RenderStateCoreManager::notifyDepthStencilStateCreated(const DEPTH_STENCIL_STATE_DESC& desc, const CachedDepthStencilState& state) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		mCachedDepthStencilStates[desc] = state;
 	}
 
 	void RenderStateCoreManager::notifySamplerStateDestroyed(const SAMPLER_STATE_DESC& desc) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		mCachedSamplerStates.erase(desc);
 	}
 
 	SPtr<SamplerStateCore> RenderStateCoreManager::findCachedState(const SAMPLER_STATE_DESC& desc) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		auto iterFind = mCachedSamplerStates.find(desc);
 		if (iterFind != mCachedSamplerStates.end())
@@ -330,7 +330,7 @@ namespace BansheeEngine
 
 	SPtr<BlendStateCore> RenderStateCoreManager::findCachedState(const BLEND_STATE_DESC& desc, UINT32& id) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		auto iterFind = mCachedBlendStates.find(desc);
 		if (iterFind != mCachedBlendStates.end())
@@ -351,7 +351,7 @@ namespace BansheeEngine
 
 	SPtr<RasterizerStateCore> RenderStateCoreManager::findCachedState(const RASTERIZER_STATE_DESC& desc, UINT32& id) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		auto iterFind = mCachedRasterizerStates.find(desc);
 		if (iterFind != mCachedRasterizerStates.end())
@@ -372,7 +372,7 @@ namespace BansheeEngine
 
 	SPtr<DepthStencilStateCore> RenderStateCoreManager::findCachedState(const DEPTH_STENCIL_STATE_DESC& desc, UINT32& id) const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		auto iterFind = mCachedDepthStencilStates.find(desc);
 		if (iterFind != mCachedDepthStencilStates.end())

+ 14 - 14
Source/BansheeCore/Source/BsRenderWindowManager.cpp

@@ -26,7 +26,7 @@ namespace BansheeEngine
 		renderWindow->_setThisPtr(renderWindow);
 		
 		{
-			BS_LOCK_MUTEX(mWindowMutex);
+			Lock lock(mWindowMutex);
 
 			mWindows[renderWindow->mWindowId] = renderWindow.get();
 		}
@@ -39,7 +39,7 @@ namespace BansheeEngine
 	void RenderWindowManager::notifyWindowDestroyed(RenderWindow* window)
 	{
 		{
-			BS_LOCK_MUTEX(mWindowMutex);
+			Lock lock(mWindowMutex);
 
 			auto iterFind = std::find_if(begin(mMovedOrResizedWindows), end(mMovedOrResizedWindows), 
 				[&](const MoveOrResizeData& x) { return x.window == window; });
@@ -57,7 +57,7 @@ namespace BansheeEngine
 
 	void RenderWindowManager::notifyFocusReceived(RenderWindowCore* coreWindow)
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		RenderWindow* window = getNonCore(coreWindow);
 		mNewWindowInFocus = window;
@@ -65,14 +65,14 @@ namespace BansheeEngine
 
 	void RenderWindowManager::notifyFocusLost(RenderWindowCore* coreWindow)
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		mNewWindowInFocus = nullptr;
 	}
 
 	void RenderWindowManager::notifyMovedOrResized(RenderWindowCore* coreWindow)
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		RenderWindow* window = getNonCore(coreWindow);
 		if (window == nullptr)
@@ -105,7 +105,7 @@ namespace BansheeEngine
 
 	void RenderWindowManager::notifySyncDataDirty(RenderWindowCore* coreWindow)
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		RenderWindow* window = getNonCore(coreWindow);
 
@@ -115,7 +115,7 @@ namespace BansheeEngine
 
 	void RenderWindowManager::windowMouseLeft(RenderWindowCore* coreWindow)
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		RenderWindow* window = getNonCore(coreWindow);
 		auto iterFind = std::find(begin(mMouseLeftWindows), end(mMouseLeftWindows), window);
@@ -131,7 +131,7 @@ namespace BansheeEngine
 		Vector<RenderWindow*> mouseLeftWindows;
 
 		{
-			BS_LOCK_MUTEX(mWindowMutex);
+			Lock lock(mWindowMutex);
 			newWinInFocus = mNewWindowInFocus;
 
 			for (auto& moveResizeData : mMovedOrResizedWindows)
@@ -185,7 +185,7 @@ namespace BansheeEngine
 
 	Vector<RenderWindow*> RenderWindowManager::getRenderWindows() const
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		Vector<RenderWindow*> windows;
 		for (auto& windowPair : mWindows)
@@ -221,7 +221,7 @@ namespace BansheeEngine
 
 	void RenderWindowCoreManager::_update()
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		for (auto& dirtyPropertyWindow : mDirtyProperties)
 			dirtyPropertyWindow->syncProperties();
@@ -231,7 +231,7 @@ namespace BansheeEngine
 
 	void RenderWindowCoreManager::windowCreated(RenderWindowCore* window)
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		mCreatedWindows.push_back(window);
 	}
@@ -239,7 +239,7 @@ namespace BansheeEngine
 	void RenderWindowCoreManager::windowDestroyed(RenderWindowCore* window)
 	{
 		{
-			BS_LOCK_MUTEX(mWindowMutex);
+			Lock lock(mWindowMutex);
 
 			auto iterFind = std::find(begin(mCreatedWindows), end(mCreatedWindows), window);
 
@@ -253,14 +253,14 @@ namespace BansheeEngine
 
 	Vector<RenderWindowCore*> RenderWindowCoreManager::getRenderWindows() const
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		return mCreatedWindows;
 	}
 
 	void RenderWindowCoreManager::notifySyncDataDirty(RenderWindowCore* window)
 	{
-		BS_LOCK_MUTEX(mWindowMutex);
+		Lock lock(mWindowMutex);
 
 		mDirtyProperties.insert(window);
 	}

+ 6 - 6
Source/BansheeCore/Source/BsResourceHandle.cpp

@@ -9,8 +9,8 @@
 
 namespace BansheeEngine
 {
-	BS_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(mResourceCreatedCondition, ResourceHandleBase)
-	BS_STATIC_MUTEX_CLASS_INSTANCE(mResourceCreatedMutex, ResourceHandleBase)
+	Signal ResourceHandleBase::mResourceCreatedCondition;
+	Mutex ResourceHandleBase::mResourceCreatedMutex;
 
 	ResourceHandleBase::ResourceHandleBase()
 	{
@@ -39,10 +39,10 @@ namespace BansheeEngine
 
 		if (!mData->mIsCreated)
 		{
-			BS_LOCK_MUTEX_NAMED(mResourceCreatedMutex, lock);
+			Lock lock(mResourceCreatedMutex);
 			while (!mData->mIsCreated)
 			{
-				BS_THREAD_WAIT(mResourceCreatedCondition, mResourceCreatedMutex, lock);
+				mResourceCreatedCondition.wait(lock);
 			}
 
 			// Send out ResourceListener events right away, as whatever called this method
@@ -86,12 +86,12 @@ namespace BansheeEngine
 		
 			if(!mData->mIsCreated)
 			{
-				BS_LOCK_MUTEX(mResourceCreatedMutex);
+				Lock lock(mResourceCreatedMutex);
 				{
 					mData->mIsCreated = true; 
 				}
 				
-				BS_THREAD_NOTIFY_ALL(mResourceCreatedCondition);
+				mResourceCreatedCondition.notify_all();
 			}
 		}
 	}

+ 6 - 6
Source/BansheeCore/Source/BsResourceListenerManager.cpp

@@ -25,7 +25,7 @@ namespace BansheeEngine
 	void ResourceListenerManager::registerListener(IResourceListener* listener)
 	{
 #if BS_DEBUG_MODE
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 		mActiveListeners.insert(listener);
 #endif
 	}
@@ -34,7 +34,7 @@ namespace BansheeEngine
 	{
 #if BS_DEBUG_MODE
 		{
-			BS_LOCK_RECURSIVE_MUTEX(mMutex);
+			RecursiveLock lock(mMutex);
 			mActiveListeners.erase(listener);
 		}
 #endif
@@ -60,7 +60,7 @@ namespace BansheeEngine
 		mDirtyListeners.clear();
 
 		{
-			BS_LOCK_RECURSIVE_MUTEX(mMutex);
+			RecursiveLock lock(mMutex);
 
 			for (auto& entry : mLoadedResources)
 				sendResourceLoaded(entry.second);
@@ -75,7 +75,7 @@ namespace BansheeEngine
 
 	void ResourceListenerManager::notifyListeners(const String& resourceUUID)
 	{
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 
 		auto iterFindLoaded = mLoadedResources.find(resourceUUID);
 		if (iterFindLoaded != mLoadedResources.end())
@@ -96,14 +96,14 @@ namespace BansheeEngine
 
 	void ResourceListenerManager::onResourceLoaded(const HResource& resource)
 	{
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 
 		mLoadedResources[resource.getUUID()] = resource;
 	}
 
 	void ResourceListenerManager::onResourceModified(const HResource& resource)
 	{
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 
 		mModifiedResources[resource.getUUID()] = resource;
 	}

+ 22 - 22
Source/BansheeCore/Source/BsResources.cpp

@@ -27,7 +27,7 @@ namespace BansheeEngine
 		UnorderedMap<String, LoadedResourceData> loadedResourcesCopy;
 		
 		{
-			BS_LOCK_MUTEX(mLoadedResourceMutex)
+			Lock lock(mLoadedResourceMutex);
 			loadedResourcesCopy = mLoadedResources;
 		}
 
@@ -103,7 +103,7 @@ namespace BansheeEngine
 		bool loadInProgress = false;
 		{
 			// Check if resource is already being loaded on a worker thread
-			BS_LOCK_MUTEX(mInProgressResourcesMutex);
+			Lock inProgressLock(mInProgressResourcesMutex);
 			auto iterFind2 = mInProgressResources.find(UUID);
 			if (iterFind2 != mInProgressResources.end())
 			{
@@ -126,7 +126,7 @@ namespace BansheeEngine
 
 			if (!alreadyLoading)
 			{
-				BS_LOCK_MUTEX(mLoadedResourceMutex);
+				Lock loadedLock(mLoadedResourceMutex);
 				auto iterFind = mLoadedResources.find(UUID);
 				if (iterFind != mLoadedResources.end()) // Resource is already loaded
 				{
@@ -149,7 +149,7 @@ namespace BansheeEngine
 		if (!alreadyLoading)
 		{
 			// Check if the handle already exists
-			BS_LOCK_MUTEX(mLoadedResourceMutex);
+			Lock lock(mLoadedResourceMutex);
 			auto iterFind = mHandles.find(UUID);
 			if (iterFind != mHandles.end())
 				outputResource = iterFind->second.lock();
@@ -198,7 +198,7 @@ namespace BansheeEngine
 		if (!alreadyLoading)
 		{
 			{
-				BS_LOCK_MUTEX(mInProgressResourcesMutex);
+				Lock lock(mInProgressResourcesMutex);
 
 				ResourceLoadData* loadData = bs_new<ResourceLoadData>(outputResource.getWeak(), 0);
 				mInProgressResources[UUID] = loadData;
@@ -238,7 +238,7 @@ namespace BansheeEngine
 
 				// Keep dependencies alive until the parent is done loading
 				{
-					BS_LOCK_MUTEX(mInProgressResourcesMutex);
+					Lock lock(mInProgressResourcesMutex);
 
 					// At this point the resource is guaranteed to still be in-progress, so it's safe to update its dependency list
 					mInProgressResources[UUID]->dependencies = dependencies;
@@ -251,7 +251,7 @@ namespace BansheeEngine
 			if (!dependencies.empty())
 			{
 				{
-					BS_LOCK_MUTEX(mInProgressResourcesMutex);
+					Lock lock(mInProgressResourcesMutex);
 
 					ResourceLoadData* loadData = nullptr;
 
@@ -333,7 +333,7 @@ namespace BansheeEngine
 				// In case loading finished in the meantime we cannot be sure at what point ::loadComplete was triggered,
 				// so trigger it manually so that the dependency count is properly decremented in case this resource
 				// is a dependency.
-				BS_LOCK_MUTEX(mLoadedResourceMutex);
+				Lock lock(mLoadedResourceMutex);
 				auto iterFind = mLoadedResources.find(UUID);
 				if (iterFind != mLoadedResources.end())
 					loadComplete(outputResource);
@@ -370,7 +370,7 @@ namespace BansheeEngine
 		{
 			bool loadInProgress = false;
 
-			BS_LOCK_MUTEX(mInProgressResourcesMutex);
+			Lock inProgressLock(mInProgressResourcesMutex);
 			auto iterFind2 = mInProgressResources.find(UUID);
 			if (iterFind2 != mInProgressResources.end())
 				loadInProgress = true;
@@ -383,7 +383,7 @@ namespace BansheeEngine
 				resource.blockUntilLoaded();
 
 			{
-				BS_LOCK_MUTEX(mLoadedResourceMutex);
+				Lock loadedLock(mLoadedResourceMutex);
 				auto iterFind = mLoadedResources.find(UUID);
 				if (iterFind != mLoadedResources.end()) // Resource is already loaded
 				{
@@ -404,7 +404,7 @@ namespace BansheeEngine
 		Vector<HResource> resourcesToUnload;
 
 		{
-			BS_LOCK_MUTEX(mLoadedResourceMutex);
+			Lock lock(mLoadedResourceMutex);
 			for(auto iter = mLoadedResources.begin(); iter != mLoadedResources.end(); ++iter)
 			{
 				const LoadedResourceData& resData = iter->second;
@@ -433,7 +433,7 @@ namespace BansheeEngine
 		{
 			bool loadInProgress = false;
 			{
-				BS_LOCK_MUTEX(mInProgressResourcesMutex);
+				Lock lock(mInProgressResourcesMutex);
 				auto iterFind2 = mInProgressResources.find(uuid);
 				if (iterFind2 != mInProgressResources.end())
 					loadInProgress = true;
@@ -450,7 +450,7 @@ namespace BansheeEngine
 		resource.mData->mPtr->destroy();
 
 		{
-			BS_LOCK_MUTEX(mLoadedResourceMutex);
+			Lock lock(mLoadedResourceMutex);
 
 			auto iterFind = mLoadedResources.find(uuid);
 			if (iterFind != mLoadedResources.end())
@@ -482,7 +482,7 @@ namespace BansheeEngine
 		{
 			bool loadInProgress = false;
 			{
-				BS_LOCK_MUTEX(mInProgressResourcesMutex);
+				Lock lock(mInProgressResourcesMutex);
 				auto iterFind2 = mInProgressResources.find(resource.getUUID());
 				if (iterFind2 != mInProgressResources.end())
 					loadInProgress = true;
@@ -534,7 +534,7 @@ namespace BansheeEngine
 		handle.setHandleData(resource, uuid);
 
 		{
-			BS_LOCK_MUTEX(mLoadedResourceMutex);
+			Lock lock(mLoadedResourceMutex);
 			auto iterFind = mLoadedResources.find(uuid);
 			if (iterFind == mLoadedResources.end())
 			{
@@ -595,7 +595,7 @@ namespace BansheeEngine
 	{
 		if (checkInProgress)
 		{
-			BS_LOCK_MUTEX(mInProgressResourcesMutex);
+			Lock inProgressLock(mInProgressResourcesMutex);
 			auto iterFind2 = mInProgressResources.find(uuid);
 			if (iterFind2 != mInProgressResources.end())
 			{
@@ -603,7 +603,7 @@ namespace BansheeEngine
 			}
 
 			{
-				BS_LOCK_MUTEX(mLoadedResourceMutex);
+				Lock loadedLock(mLoadedResourceMutex);
 				auto iterFind = mLoadedResources.find(uuid);
 				if (iterFind != mLoadedResources.end())
 				{
@@ -621,7 +621,7 @@ namespace BansheeEngine
 		HResource newHandle(obj, uuid);
 
 		{
-			BS_LOCK_MUTEX(mLoadedResourceMutex);
+			Lock lock(mLoadedResourceMutex);
 
 			LoadedResourceData& resData = mLoadedResources[uuid];
 			resData.resource = newHandle.getWeak();
@@ -633,7 +633,7 @@ namespace BansheeEngine
 
 	HResource Resources::_getResourceHandle(const String& uuid)
 	{
-		BS_LOCK_MUTEX(mLoadedResourceMutex);
+		Lock lock(mLoadedResourceMutex);
 		auto iterFind3 = mHandles.find(uuid);
 		if (iterFind3 != mHandles.end()) // Not loaded, but handle does exist
 		{
@@ -681,7 +681,7 @@ namespace BansheeEngine
 		bool finishLoad = true;
 		Vector<ResourceLoadData*> dependantLoads;
 		{
-			BS_LOCK_MUTEX(mInProgressResourcesMutex);
+			Lock inProgresslock(mInProgressResourcesMutex);
 
 			auto iterFind = mInProgressResources.find(uuid);
 			if (iterFind != mInProgressResources.end())
@@ -706,7 +706,7 @@ namespace BansheeEngine
 				// by its dependencies.
 				if (myLoadData != nullptr && myLoadData->loadedData != nullptr)
 				{
-					BS_LOCK_MUTEX(mLoadedResourceMutex);
+					Lock loadedLock(mLoadedResourceMutex);
 
 					mLoadedResources[uuid] = myLoadData->resData;
 					resource.setHandleData(myLoadData->loadedData, uuid);
@@ -740,7 +740,7 @@ namespace BansheeEngine
 		SPtr<Resource> rawResource = loadFromDiskAndDeserialize(filePath);
 
 		{
-			BS_LOCK_MUTEX(mInProgressResourcesMutex);
+			Lock lock(mInProgressResourcesMutex);
 
 			// Check if all my dependencies are loaded
 			ResourceLoadData* myLoadData = mInProgressResources[resource.getUUID()];

+ 24 - 25
Source/BansheeCore/Source/Win32/BsWin32FolderMonitor.cpp

@@ -50,8 +50,8 @@ namespace BansheeEngine
 
 		WString mCachedOldFileName; // Used during rename notifications as they are handled in two steps
 
-		BS_MUTEX(mStatusMutex)
-		BS_THREAD_SYNCHRONISER(mStartStopEvent)
+		Mutex mStatusMutex;
+		Signal mStartStopEvent;
 	};
 
 	FolderMonitor::FolderWatchInfo::FolderWatchInfo(const Path& folderToMonitor, HANDLE dirHandle, bool monitorSubdirectories, DWORD monitorFlags)
@@ -74,19 +74,19 @@ namespace BansheeEngine
 			return; // Already monitoring
 
 		{
-			BS_LOCK_MUTEX_NAMED(mStatusMutex, lock);
+			Lock lock(mStatusMutex);
 
 			mState = MonitorState::Starting;
 			PostQueuedCompletionStatus(compPortHandle, sizeof(this), (ULONG_PTR)this, &mOverlapped);
 
 			while(mState != MonitorState::Monitoring)
-				BS_THREAD_WAIT(mStartStopEvent, mStatusMutex, lock);
+				mStartStopEvent.wait(lock);
 		}
 
 		if(mReadError != ERROR_SUCCESS)
 		{
 			{
-				BS_LOCK_MUTEX(mStatusMutex);
+				Lock lock(mStatusMutex);
 				mState = MonitorState::Inactive;
 			}
 
@@ -99,13 +99,13 @@ namespace BansheeEngine
 	{
 		if(mState != MonitorState::Inactive)
 		{
-			BS_LOCK_MUTEX_NAMED(mStatusMutex, lock);
+			Lock lock(mStatusMutex);
 
 			mState = MonitorState::Shutdown;
 			PostQueuedCompletionStatus(compPortHandle, sizeof(this), (ULONG_PTR)this, &mOverlapped);
 
 			while(mState != MonitorState::Inactive)
-				BS_THREAD_WAIT(mStartStopEvent, mStatusMutex, lock);
+				mStartStopEvent.wait(lock);
 		}
 
 		if(mDirHandle != INVALID_HANDLE_VALUE)
@@ -301,8 +301,8 @@ namespace BansheeEngine
 		Queue<FileAction*> mFileActions;
 		List<FileAction*> mActiveFileActions;
 
-		BS_MUTEX(mMainMutex);
-		BS_THREAD_TYPE* mWorkerThread;
+		Mutex mMainMutex;
+		Thread* mWorkerThread;
 	};
 
 	FolderMonitor::FolderMonitor()
@@ -386,8 +386,7 @@ namespace BansheeEngine
 
 		if(mPimpl->mWorkerThread == nullptr)
 		{
-			BS_THREAD_CREATE(t, (std::bind(&FolderMonitor::workerThreadMain, this)));
-			mPimpl->mWorkerThread = t;
+			mPimpl->mWorkerThread = bs_new<Thread>(std::bind(&FolderMonitor::workerThreadMain, this));
 
 			if(mPimpl->mWorkerThread == nullptr)
 			{
@@ -439,7 +438,7 @@ namespace BansheeEngine
 				// Even though we wait for a condition variable from the worker thread in stopMonitor,
 				// that doesn't mean the worker thread is done with the condition variable
 				// (which is stored inside watchInfo)
-				BS_LOCK_MUTEX(mPimpl->mMainMutex); 
+				Lock lock(mPimpl->mMainMutex);
 				bs_delete(watchInfo);
 			}
 		}
@@ -451,7 +450,7 @@ namespace BansheeEngine
 			PostQueuedCompletionStatus(mPimpl->mCompPortHandle, 0, 0, nullptr);
 
 			mPimpl->mWorkerThread->join();
-			BS_THREAD_DESTROY(mPimpl->mWorkerThread);
+			bs_delete(mPimpl->mWorkerThread);
 			mPimpl->mWorkerThread = nullptr;
 		}
 
@@ -482,7 +481,7 @@ namespace BansheeEngine
 				MonitorState state;
 
 				{
-					BS_LOCK_MUTEX(watchInfo->mStatusMutex);
+					Lock lock(watchInfo->mStatusMutex);
 					state = watchInfo->mState;
 				}
 
@@ -500,12 +499,12 @@ namespace BansheeEngine
 						watchInfo->mReadError = ERROR_SUCCESS;
 
 						{
-							BS_LOCK_MUTEX(watchInfo->mStatusMutex);
+							Lock lock(watchInfo->mStatusMutex);
 							watchInfo->mState = MonitorState::Monitoring;
 						}
 					}
 
-					BS_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
+					watchInfo->mStartStopEvent.notify_one();
 
 					break;
 				case MonitorState::Monitoring:
@@ -532,20 +531,20 @@ namespace BansheeEngine
 						watchInfo->mDirHandle = INVALID_HANDLE_VALUE;
 
 						{
-							BS_LOCK_MUTEX(watchInfo->mStatusMutex);
+							Lock lock(watchInfo->mStatusMutex);
 							watchInfo->mState = MonitorState::Shutdown2;
 						}
 					}
 					else
 					{
 						{
-							BS_LOCK_MUTEX(watchInfo->mStatusMutex);
+							Lock lock(watchInfo->mStatusMutex);
 							watchInfo->mState = MonitorState::Inactive;
 						}
 
 						{
-							BS_LOCK_MUTEX(mPimpl->mMainMutex); // Ensures that we don't delete "watchInfo" before this thread is done with mStartStopEvent
-							BS_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
+							Lock lock(mPimpl->mMainMutex); // Ensures that we don't delete "watchInfo" before this thread is done with mStartStopEvent
+							watchInfo->mStartStopEvent.notify_one();
 						}
 					}
 
@@ -560,13 +559,13 @@ namespace BansheeEngine
 					else
 					{
 						{
-							BS_LOCK_MUTEX(watchInfo->mStatusMutex);
+							Lock lock(watchInfo->mStatusMutex);
 							watchInfo->mState = MonitorState::Inactive;
 						}
 
 						{
-							BS_LOCK_MUTEX(mPimpl->mMainMutex); // Ensures that we don't delete "watchInfo" before this thread is done with mStartStopEvent
-							BS_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
+							Lock lock(mPimpl->mMainMutex); // Ensures that we don't delete "watchInfo" before this thread is done with mStartStopEvent
+							watchInfo->mStartStopEvent.notify_one();
 						}
 					}
 
@@ -613,7 +612,7 @@ namespace BansheeEngine
 		} while(notifyInfo.getNext());
 
 		{
-			BS_LOCK_MUTEX(mPimpl->mMainMutex);
+			Lock lock(mPimpl->mMainMutex);
 
 			for(auto& action : mActions)
 				mPimpl->mFileActions.push(action);
@@ -623,7 +622,7 @@ namespace BansheeEngine
 	void FolderMonitor::_update()
 	{
 		{
-			BS_LOCK_MUTEX(mPimpl->mMainMutex);
+			Lock lock(mPimpl->mMainMutex);
 
 			while (!mPimpl->mFileActions.empty())
 			{

+ 12 - 12
Source/BansheeCore/Source/Win32/BsWin32Platform.cpp

@@ -218,21 +218,21 @@ namespace BansheeEngine
 
 	void Platform::setCaptionNonClientAreas(const RenderWindowCore& window, const Vector<Rect2I>& nonClientAreas)
 	{
-		BS_LOCK_MUTEX(mData->mSync);
+		Lock lock(mData->mSync);
 
 		mData->mNonClientAreas[&window].moveAreas = nonClientAreas;
 	}
 
 	void Platform::setResizeNonClientAreas(const RenderWindowCore& window, const Vector<NonClientResizeArea>& nonClientAreas)
 	{
-		BS_LOCK_MUTEX(mData->mSync);
+		Lock lock(mData->mSync);
 
 		mData->mNonClientAreas[&window].resizeAreas = nonClientAreas;
 	}
 
 	void Platform::resetNonClientAreas(const RenderWindowCore& window)
 	{
-		BS_LOCK_MUTEX(mData->mSync);
+		Lock lock(mData->mSync);
 
 		auto iterFind = mData->mNonClientAreas.find(&window);
 
@@ -258,7 +258,7 @@ namespace BansheeEngine
 			mData->mDropTargets.dropTargetsPerWindow[window] = win32DropTarget;
 
 			{
-				BS_LOCK_MUTEX(mData->mSync);
+				Lock lock(mData->mSync);
 				mData->mDropTargets.dropTargetsToInitialize.push_back(win32DropTarget);
 			}
 		}
@@ -288,7 +288,7 @@ namespace BansheeEngine
 				mData->mDropTargets.dropTargetsPerWindow.erase(iterFind);
 
 				{
-					BS_LOCK_MUTEX(mData->mSync);
+					Lock lock(mData->mSync);
 					mData->mDropTargets.dropTargetsToDestroy.push_back(win32DropTarget);
 				}
 			}
@@ -309,7 +309,7 @@ namespace BansheeEngine
 
 	void Platform::_startUp()
 	{
-		BS_LOCK_MUTEX(mData->mSync);
+		Lock lock(mData->mSync);
 
 		if (timeBeginPeriod(1) == TIMERR_NOCANDO)
 		{
@@ -332,7 +332,7 @@ namespace BansheeEngine
 	void Platform::_coreUpdate()
 	{
 		{
-			BS_LOCK_MUTEX(mData->mSync);
+			Lock lock(mData->mSync);
 			if (mData->mRequiresStartUp)
 			{
 				OleInitialize(nullptr);
@@ -342,7 +342,7 @@ namespace BansheeEngine
 		}
 
 		{
-			BS_LOCK_MUTEX(mData->mSync);
+			Lock lock(mData->mSync);
 			for (auto& dropTargetToDestroy : mData->mDropTargets.dropTargetsToDestroy)
 			{
 				dropTargetToDestroy->unregisterWithOS();
@@ -353,7 +353,7 @@ namespace BansheeEngine
 		}
 
 		{
-			BS_LOCK_MUTEX(mData->mSync);
+			Lock lock(mData->mSync);
 			for (auto& dropTargetToInit : mData->mDropTargets.dropTargetsToInitialize)
 			{
 				dropTargetToInit->registerWithOS();
@@ -365,7 +365,7 @@ namespace BansheeEngine
 		_messagePump();
 
 		{
-			BS_LOCK_MUTEX(mData->mSync);
+			Lock lock(mData->mSync);
 			if (mData->mRequiresShutDown)
 			{
 				OleUninitialize();
@@ -376,7 +376,7 @@ namespace BansheeEngine
 
 	void Platform::_shutDown()
 	{
-		BS_LOCK_MUTEX(mData->mSync);
+		Lock lock(mData->mSync);
 
 		timeEndPeriod(1);
 		mData->mRequiresShutDown = true;
@@ -660,7 +660,7 @@ namespace BansheeEngine
 				// to trigger, while the mouse is still in the non-client area of the window.
 				mData->mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
 
-				BS_LOCK_MUTEX(mData->mSync);
+				Lock lock(mData->mSync);
 
 				if (!onMouseLeftWindow.empty())
 					onMouseLeftWindow(win);

+ 1 - 1
Source/BansheeD3D9RenderAPI/Include/BsD3D9Resource.h

@@ -42,7 +42,7 @@ namespace BansheeEngine
 		static void unlockDeviceAccess();
 
 	protected:
-		BS_STATIC_MUTEX(msDeviceAccessMutex)		
+		static Mutex msDeviceAccessMutex;
 	};
 
 	/** @} */

+ 1 - 1
Source/BansheeD3D9RenderAPI/Include/BsD3D9ResourceManager.h

@@ -65,7 +65,7 @@ namespace BansheeEngine
 	protected:
 		friend class D3D9Resource;
 		
-		BS_MUTEX(mResourcesMutex)
+		Mutex mResourcesMutex;
 		Vector<D3D9Resource*> mResources;
 		D3D9ResourceCreationPolicy mResourceCreationPolicy;
 		long mDeviceAccessLockCount;		

+ 1 - 1
Source/BansheeD3D9RenderAPI/Source/BsD3D9Resource.cpp

@@ -6,7 +6,7 @@
 
 namespace BansheeEngine
 {
-	BS_STATIC_MUTEX_INSTANCE(D3D9Resource::msDeviceAccessMutex)
+	Mutex D3D9Resource::msDeviceAccessMutex;
 
 	D3D9Resource::D3D9Resource()
 	{				

+ 6 - 6
Source/BansheeD3D9RenderAPI/Source/BsD3D9ResourceManager.cpp

@@ -29,7 +29,7 @@ namespace BansheeEngine
 
 	void D3D9ResourceManager::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
 	{				
-		BS_LOCK_MUTEX(mResourcesMutex)
+		Lock lock(mResourcesMutex);
 
 		for (auto& resource : mResources)
 			resource->notifyOnDeviceCreate(d3d9Device);			
@@ -37,7 +37,7 @@ namespace BansheeEngine
 
 	void D3D9ResourceManager::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
 	{
-		BS_LOCK_MUTEX(mResourcesMutex)
+		Lock lock(mResourcesMutex);
 
 		for (auto& resource : mResources)
 			resource->notifyOnDeviceDestroy(d3d9Device);
@@ -45,7 +45,7 @@ namespace BansheeEngine
 
 	void D3D9ResourceManager::notifyOnDeviceLost(IDirect3DDevice9* d3d9Device)
 	{
-		BS_LOCK_MUTEX(mResourcesMutex)
+		Lock lock(mResourcesMutex);
 
 		for (auto& resource : mResources)
 			resource->notifyOnDeviceLost(d3d9Device);
@@ -53,7 +53,7 @@ namespace BansheeEngine
 
 	void D3D9ResourceManager::notifyOnDeviceReset(IDirect3DDevice9* d3d9Device)
 	{		
-		BS_LOCK_MUTEX(mResourcesMutex)
+		Lock lock(mResourcesMutex);
 
 		for (auto& resource : mResources)
 			resource->notifyOnDeviceReset(d3d9Device);	
@@ -61,13 +61,13 @@ namespace BansheeEngine
 
 	void D3D9ResourceManager::_notifyResourceCreated(D3D9Resource* pResource)
 	{		
-		BS_LOCK_MUTEX(mResourcesMutex)		
+		Lock lock(mResourcesMutex);
 		mResources.push_back(pResource);
 	}
 	
 	void D3D9ResourceManager::_notifyResourceDestroyed(D3D9Resource* pResource)
 	{		
-		BS_LOCK_MUTEX(mResourcesMutex)
+		Lock lock(mResourcesMutex);
 
 		auto iter = mResources.begin();
 		while (iter != mResources.end())

+ 3 - 3
Source/BansheeEngine/Source/BsRendererMaterialManager.cpp

@@ -32,7 +32,7 @@ namespace BansheeEngine
 
 	void RendererMaterialManager::_registerMaterial(RendererMaterialMetaData* metaData, const Path& shaderPath)
 	{
-		Lock<> lock(getMutex());
+		Lock lock(getMutex());
 
 		Vector<RendererMaterialData>& materials = getMaterials();
 		materials.push_back({metaData, shaderPath});
@@ -54,7 +54,7 @@ namespace BansheeEngine
 
 	void RendererMaterialManager::initOnCore(const Vector<SPtr<ShaderCore>>& shaders)
 	{
-		Lock<> lock(getMutex());
+		Lock lock(getMutex());
 
 		Vector<RendererMaterialData>& materials = getMaterials();
 		for (UINT32 i = 0; i < materials.size(); i++)
@@ -63,7 +63,7 @@ namespace BansheeEngine
 
 	void RendererMaterialManager::destroyOnCore()
 	{
-		Lock<> lock(getMutex());
+		Lock lock(getMutex());
 
 		Vector<RendererMaterialData>& materials = getMaterials();
 		for (UINT32 i = 0; i < materials.size(); i++)

+ 2 - 2
Source/BansheeUtility/Include/BsAsyncOp.h

@@ -20,8 +20,8 @@ namespace BansheeEngine
 	class BS_UTILITY_EXPORT AsyncOpSyncData
 	{
 	public:
-		BS_MUTEX(mMutex)
-		BS_THREAD_SYNCHRONISER(mCondition)
+		Mutex mMutex;
+		Signal mCondition;
 	};
 
 	/**

+ 7 - 7
Source/BansheeUtility/Include/BsEvent.h

@@ -76,7 +76,7 @@ namespace BansheeEngine
 		 */
 		void disconnect(BaseConnectionData* conn)
 		{
-			BS_LOCK_RECURSIVE_MUTEX(mMutex);
+			RecursiveLock lock(mMutex);
 
 			conn->deactivate();
 			conn->handleLinks--;
@@ -88,7 +88,7 @@ namespace BansheeEngine
 		/** Disconnects all connections in the event. */
 		void clear()
 		{
-			BS_LOCK_RECURSIVE_MUTEX(mMutex);
+			RecursiveLock lock(mMutex);
 
 			BaseConnectionData* conn = mConnections;
 			while (conn != nullptr)
@@ -109,7 +109,7 @@ namespace BansheeEngine
 		 */
 		void freeHandle(BaseConnectionData* conn)
 		{
-			BS_LOCK_RECURSIVE_MUTEX(mMutex);
+			RecursiveLock lock(mMutex);
 
 			conn->handleLinks--;
 
@@ -144,7 +144,7 @@ namespace BansheeEngine
 		BaseConnectionData* mConnections;
 		BaseConnectionData* mFreeConnections;
 
-		BS_RECURSIVE_MUTEX(mMutex);
+		RecursiveMutex mMutex;
 	};
 
 	/** @} */
@@ -268,7 +268,7 @@ namespace BansheeEngine
 		/** Register a new callback that will get notified once the event is triggered. */
 		HEvent connect(std::function<RetType(Args...)> func)
 		{
-			BS_LOCK_RECURSIVE_MUTEX(mInternalData->mMutex);
+			RecursiveLock lock(mInternalData->mMutex);
 
 			ConnectionData* connData = nullptr;
 			if (mInternalData->mFreeConnections != nullptr)
@@ -304,7 +304,7 @@ namespace BansheeEngine
 			// deletes the event itself.
 			SPtr<EventInternalData> internalData = mInternalData;
 
-			BS_LOCK_RECURSIVE_MUTEX(internalData->mMutex);
+			RecursiveLock lock(internalData->mMutex);
 
 			// Hidden dependency: If any new connections are made during these callbacks they must be
 			// inserted at the start of the linked list so that we don't trigger them here.
@@ -334,7 +334,7 @@ namespace BansheeEngine
 		 */
 		bool empty() const
 		{
-			BS_LOCK_RECURSIVE_MUTEX(mInternalData->mMutex);
+			RecursiveLock lock(mInternalData->mMutex);
 
 			return mInternalData->mConnections == nullptr;
 		}

+ 2 - 2
Source/BansheeUtility/Include/BsFrameAlloc.h

@@ -121,7 +121,7 @@ namespace BansheeEngine
 		 * Changes the frame allocator owner thread. After the owner thread has changed only allocations from that thread 
 		 * can be made.
 		 */
-		void setOwnerThread(BS_THREAD_ID_TYPE thread);
+		void setOwnerThread(ThreadId thread);
 
 	private:
 		UINT32 mBlockSize;
@@ -132,7 +132,7 @@ namespace BansheeEngine
 		void* mLastFrame;
 
 #if BS_DEBUG_MODE
-		BS_THREAD_ID_TYPE mOwnerThread;
+		ThreadId mOwnerThread;
 #endif
 
 		/**

+ 1 - 1
Source/BansheeUtility/Include/BsLog.h

@@ -88,7 +88,7 @@ namespace BansheeEngine
 		Vector<LogEntry> mEntries;
 		Queue<LogEntry> mUnreadEntries;
 		UINT64 mHash;
-		BS_RECURSIVE_MUTEX(mMutex);
+		mutable RecursiveMutex mMutex;
 	};
 
 	/** @} */

+ 4 - 4
Source/BansheeUtility/Include/BsTaskScheduler.h

@@ -130,10 +130,10 @@ namespace BansheeEngine
 		UINT32 mNextTaskId;
 		bool mShutdown;
 
-		BS_MUTEX(mReadyMutex);
-		BS_MUTEX(mCompleteMutex);
-		BS_THREAD_SYNCHRONISER(mTaskReadyCond);
-		BS_THREAD_SYNCHRONISER(mTaskCompleteCond);
+		Mutex mReadyMutex;
+		Mutex mCompleteMutex;
+		Signal mTaskReadyCond;
+		Signal mTaskCompleteCond;
 	};
 
 	/** @} */

+ 19 - 42
Source/BansheeUtility/Include/BsThreadDefines.h

@@ -14,57 +14,34 @@
  *  @{
  */
 
-#define BS_AUTO_MUTEX mutable std::mutex BS_AUTO_MUTEX_NAME;
-#define BS_LOCK_AUTO_MUTEX std::unique_lock<std::mutex> bsAutoMutexLock(BS_AUTO_MUTEX_NAME);
-#define BS_MUTEX(name) mutable std::mutex name;
-#define BS_STATIC_MUTEX(name) static std::mutex name;
-#define BS_STATIC_MUTEX_INSTANCE(name) std::mutex name;
-#define BS_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName) std::mutex classTypeName::name;
-#define BS_LOCK_MUTEX(name) std::unique_lock<std::mutex> bsnameLock(name);
-#define BS_LOCK_MUTEX_NAMED(mutexName, lockName) std::unique_lock<std::mutex> lockName(mutexName);
-#define BS_LOCK_TYPE std::unique_lock<std::mutex>
-// like BS_AUTO_MUTEX but mutex held by pointer
-#define BS_AUTO_SHARED_MUTEX mutable std::mutex *BS_AUTO_MUTEX_NAME;
-#define BS_LOCK_AUTO_SHARED_MUTEX assert(BS_AUTO_MUTEX_NAME); std::lock_guard<std::mutex> bsAutoMutexLock(*BS_AUTO_MUTEX_NAME);
-#define BS_COPY_AUTO_SHARED_MUTEX(from) assert(!BS_AUTO_MUTEX_NAME); BS_AUTO_MUTEX_NAME = from;
-#define BS_SET_AUTO_SHARED_MUTEX_NULL BS_AUTO_MUTEX_NAME = 0;
-#define BS_MUTEX_CONDITIONAL(mutex) if (mutex)
-#define BS_THREAD_SYNCHRONISER(sync) std::condition_variable sync;
-#define BS_STATIC_THREAD_SYNCHRONISER(sync) static std::condition_variable sync;
-#define BS_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(sync, classTypeName) std::condition_variable classTypeName::sync;
-#define BS_THREAD_WAIT(sync, mutex, lock) sync.wait(lock);
-#define BS_THREAD_WAIT_FOR(sync, mutex, lock, ms) sync.wait_for(lock, std::chrono::milliseconds(ms));
-#define BS_THREAD_NOTIFY_ONE(sync) sync.notify_one(); 
-#define BS_THREAD_NOTIFY_ALL(sync) sync.notify_all(); 
-#define BS_THREAD_JOIN(thread) thread.join();
-// Recursive mutex
-#define BS_RECURSIVE_MUTEX(name) mutable std::recursive_mutex name
-#define BS_LOCK_RECURSIVE_MUTEX(name) std::unique_lock<std::recursive_mutex> cmnameLock(name);
-// Read-write mutex
-#define BS_RW_MUTEX(name) mutable std::mutex name
-#define BS_LOCK_RW_MUTEX_READ(name) std::unique_lock<std::mutex> cmnameLock(name)
-#define BS_LOCK_RW_MUTEX_WRITE(name) std::unique_lock<std::mutex> cmnameLock(name)
-// Thread objects and related functions
-#define BS_THREAD_TYPE std::thread
-#define BS_THREAD_CREATE(name, worker) std::thread* name = new (BansheeEngine::MemoryAllocator<BansheeEngine::GenAlloc>::allocate(sizeof(std::thread))) std::thread(worker);
-#define BS_THREAD_DESTROY(name) BansheeEngine::bs_delete<std::thread, BansheeEngine::GenAlloc>(name);
+/** Returns the number of logical CPU cores. */
 #define BS_THREAD_HARDWARE_CONCURRENCY std::thread::hardware_concurrency()
+
+/** Returns the ThreadId of the current thread. */
 #define BS_THREAD_CURRENT_ID std::this_thread::get_id()
-#define BS_THREAD_ID_TYPE std::thread::id
-#define BS_DEFER_LOCK std::defer_lock
-#define BS_THREAD_WORKER_INHERIT
-// Utility
+
+/** Causes the current thread to sleep for the provided amount of milliseconds. */
 #define BS_THREAD_SLEEP(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms));
 
+/** Wrapper for the C++ std::mutex. */
 using Mutex = std::mutex;
+
+/** Wrapper for the C++ std::recursive_mutex. */
 using RecursiveMutex = std::recursive_mutex;
+
+/** Wrapper for the C++ std::condition_variable. */
 using Signal = std::condition_variable;
+
+/** Wrapper for the C++ std::thread. */
 using Thread = std::thread;
 
-template <typename T = Mutex>
-using Lock = std::unique_lock<T>;
+/** Wrapper for the C++ std::thread::id. */
+using ThreadId = std::thread::id;
+
+/** Wrapper for the C++ std::unique_lock<std::mutex>. */
+using Lock = std::unique_lock<Mutex>;
 
-template <typename T = RecursiveMutex>
-using RecursiveLock = std::unique_lock<T>;
+/** Wrapper for the C++ std::unique_lock<std::recursive_mutex>. */
+using RecursiveLock = std::unique_lock<RecursiveMutex>;
 
 /** @} */

+ 6 - 6
Source/BansheeUtility/Include/BsThreadPool.h

@@ -100,11 +100,11 @@ namespace BansheeEngine
 
 		time_t mIdleTime;
 
-		BS_THREAD_TYPE* mThread;
-		BS_MUTEX(mMutex);
-		BS_THREAD_SYNCHRONISER(mStartedCond);
-		BS_THREAD_SYNCHRONISER(mReadyCond);
-		BS_THREAD_SYNCHRONISER(mWorkerEndedCond);
+		Thread* mThread;
+		mutable Mutex mMutex;
+		Signal mStartedCond;
+		Signal mReadyCond;
+		Signal mWorkerEndedCond;
 	};
 
 	/**
@@ -212,7 +212,7 @@ namespace BansheeEngine
 		UINT32 mAge;
 		
 		std::atomic_uint mUniqueId;
-		BS_MUTEX(mMutex);
+		mutable Mutex mMutex;
 	};
 
 	/** @} */

+ 4 - 4
Source/BansheeUtility/Source/BsAsyncOp.cpp

@@ -16,7 +16,7 @@ namespace BansheeEngine
 		mData->mIsCompleted.store(true, std::memory_order_release);
 
 		if (mSyncData != nullptr)
-			BS_THREAD_NOTIFY_ALL(mSyncData->mCondition);
+			mSyncData->mCondition.notify_all();
 	}
 
 	void AsyncOp::_completeOperation() 
@@ -24,7 +24,7 @@ namespace BansheeEngine
 		mData->mIsCompleted.store(true, std::memory_order_release);
 
 		if (mSyncData != nullptr)
-			BS_THREAD_NOTIFY_ALL(mSyncData->mCondition);
+			mSyncData->mCondition.notify_all();
 	}
 
 	void AsyncOp::blockUntilComplete() const
@@ -35,8 +35,8 @@ namespace BansheeEngine
 			return;
 		}
 
-		BS_LOCK_MUTEX_NAMED(mSyncData->mMutex, lock);
+		Lock lock(mSyncData->mMutex);
 		while (!hasCompleted())
-			BS_THREAD_WAIT(mSyncData->mCondition, mSyncData->mMutex, lock);
+			mSyncData->mCondition.wait(lock);
 	}
 }

+ 1 - 1
Source/BansheeUtility/Source/BsFrameAlloc.cpp

@@ -284,7 +284,7 @@ namespace BansheeEngine
 		bs_free_aligned(block);
 	}
 
-	void FrameAlloc::setOwnerThread(BS_THREAD_ID_TYPE thread)
+	void FrameAlloc::setOwnerThread(ThreadId thread)
 	{
 #if BS_DEBUG_MODE
 		mOwnerThread = thread;

+ 6 - 6
Source/BansheeUtility/Source/BsLog.cpp

@@ -21,14 +21,14 @@ namespace BansheeEngine
 
 	void Log::logMsg(const String& message, UINT32 channel)
 	{
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 
 		mUnreadEntries.push(LogEntry(message, channel));
 	}
 
 	void Log::clear()
 	{
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 
 		mEntries.clear();
 
@@ -40,7 +40,7 @@ namespace BansheeEngine
 
 	void Log::clear(UINT32 channel)
 	{
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 
 		Vector<LogEntry> newEntries;
 		for(auto& entry : mEntries)
@@ -71,7 +71,7 @@ namespace BansheeEngine
 
 	bool Log::getUnreadEntry(LogEntry& entry)
 	{
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 
 		if (mUnreadEntries.empty())
 			return false;
@@ -95,7 +95,7 @@ namespace BansheeEngine
 
 	Vector<LogEntry> Log::getEntries() const
 	{
-		BS_LOCK_RECURSIVE_MUTEX(mMutex);
+		RecursiveLock lock(mMutex);
 
 		return mEntries;
 	}
@@ -104,7 +104,7 @@ namespace BansheeEngine
 	{
 		Vector<LogEntry> entries;
 		{
-			BS_LOCK_RECURSIVE_MUTEX(mMutex);
+			RecursiveLock lock(mMutex);
 
 			for (auto& entry : mEntries)
 				entries.push_back(entry);

+ 16 - 16
Source/BansheeUtility/Source/BsTaskScheduler.cpp

@@ -50,7 +50,7 @@ namespace BansheeEngine
 	{
 		// Wait until all tasks complete
 		{
-			BS_LOCK_MUTEX_NAMED(mReadyMutex, activeTaskLock);
+			Lock activeTaskLock(mReadyMutex);
 
 			while (mActiveTasks.size() > 0)
 			{
@@ -64,19 +64,19 @@ namespace BansheeEngine
 
 		// Start shutdown of the main queue worker and wait until it exits
 		{
-			BS_LOCK_MUTEX(mReadyMutex);
+			Lock lock(mReadyMutex);
 
 			mShutdown = true;
 		}
 
-		BS_THREAD_NOTIFY_ONE(mTaskReadyCond);
+		mTaskReadyCond.notify_one();
 
 		mTaskSchedulerThread.blockUntilComplete();
 	}
 
 	void TaskScheduler::addTask(const SPtr<Task>& task)
 	{
-		BS_LOCK_MUTEX(mReadyMutex);
+		Lock lock(mReadyMutex);
 
 		task->mParent = this;
 		task->mTaskId = mNextTaskId++;
@@ -84,22 +84,22 @@ namespace BansheeEngine
 		mTaskQueue.insert(task);
 
 		// Wake main scheduler thread
-		BS_THREAD_NOTIFY_ONE(mTaskReadyCond);
+		mTaskReadyCond.notify_one();
 	}
 
 	void TaskScheduler::addWorker()
 	{
-		BS_LOCK_MUTEX(mReadyMutex);
+		Lock lock(mReadyMutex);
 
 		mMaxActiveTasks++;
 
 		// A spot freed up, queue new tasks on main scheduler thread if they exist
-		BS_THREAD_NOTIFY_ONE(mTaskReadyCond);
+		mTaskReadyCond.notify_one();
 	}
 
 	void TaskScheduler::removeWorker()
 	{
-		BS_LOCK_MUTEX(mReadyMutex);
+		Lock lock(mReadyMutex);
 
 		if(mMaxActiveTasks > 0)
 			mMaxActiveTasks--;
@@ -109,10 +109,10 @@ namespace BansheeEngine
 	{
 		while(true)
 		{
-			BS_LOCK_MUTEX_NAMED(mReadyMutex, lock);
+			Lock lock(mReadyMutex);
 
 			while((mTaskQueue.size() == 0 || (UINT32)mActiveTasks.size() >= mMaxActiveTasks) && !mShutdown)
-				BS_THREAD_WAIT(mTaskReadyCond, mReadyMutex, lock);
+				mTaskReadyCond.wait(lock);
 
 			if(mShutdown)
 				break;
@@ -141,7 +141,7 @@ namespace BansheeEngine
 		task->mTaskWorker();
 
 		{
-			BS_LOCK_MUTEX(mReadyMutex);
+			Lock lock(mReadyMutex);
 
 			auto findIter = std::find(mActiveTasks.begin(), mActiveTasks.end(), task);
 			if (findIter != mActiveTasks.end())
@@ -149,14 +149,14 @@ namespace BansheeEngine
 		}
 
 		{
-			BS_LOCK_MUTEX(mCompleteMutex);
+			Lock lock(mCompleteMutex);
 			task->mState.store(2);
 
-			BS_THREAD_NOTIFY_ALL(mTaskCompleteCond);
+			mTaskCompleteCond.notify_all();
 		}
 
 		// Possibly this task was someones dependency, so wake the main scheduler thread
-		BS_THREAD_NOTIFY_ONE(mTaskReadyCond);
+		mTaskReadyCond.notify_one();
 	}
 
 	void TaskScheduler::waitUntilComplete(const Task* task)
@@ -165,12 +165,12 @@ namespace BansheeEngine
 			return;
 
 		{
-			BS_LOCK_MUTEX_NAMED(mCompleteMutex, lock);
+			Lock lock(mCompleteMutex);
 			
 			while(!task->isComplete())
 			{
 				addWorker();
-				BS_THREAD_WAIT(mTaskCompleteCond, mCompleteMutex, lock);
+				mTaskCompleteCond.wait(lock);
 				removeWorker();
 			}
 		}

+ 30 - 31
Source/BansheeUtility/Source/BsThreadPool.cpp

@@ -28,7 +28,7 @@ namespace BansheeEngine
 		PooledThread* parentThread = nullptr;
 
 		{
-			BS_LOCK_MUTEX(mPool->mMutex);
+			Lock lock(mPool->mMutex);
 
 			for (auto& thread : mPool->mThreads)
 			{
@@ -42,12 +42,12 @@ namespace BansheeEngine
 
 		if (parentThread != nullptr)
 		{
-			BS_LOCK_MUTEX_NAMED(parentThread->mMutex, lock);
+			Lock lock(parentThread->mMutex);
 
 			if (parentThread->mId == mThreadId) // Check again in case it changed
 			{
 				while (!parentThread->mIdle)
-					BS_THREAD_WAIT(parentThread->mWorkerEndedCond, parentThread->mMutex, lock);
+					parentThread->mWorkerEndedCond.wait(lock);
 			}
 		}
 	}
@@ -62,19 +62,18 @@ namespace BansheeEngine
 
 	void PooledThread::initialize()
 	{
-		BS_THREAD_CREATE(t, std::bind(&PooledThread::run, this));
-		mThread = t;
+		mThread = bs_new<Thread>(std::bind(&PooledThread::run, this));
 
-		BS_LOCK_MUTEX_NAMED(mMutex, lock);
+		Lock lock(mMutex);
 
 		while(!mThreadStarted)
-			BS_THREAD_WAIT(mStartedCond, mMutex, lock);
+			mStartedCond.wait(lock);
 	}
 
 	void PooledThread::start(std::function<void()> workerMethod, UINT32 id)
 	{
 		{
-			BS_LOCK_MUTEX(mMutex);
+			Lock lock(mMutex);
 
 			mWorkerMethod = workerMethod;
 			mIdle = false;
@@ -83,7 +82,7 @@ namespace BansheeEngine
 			mId = id;
 		}
 
-		BS_THREAD_NOTIFY_ONE(mReadyCond);
+		mReadyCond.notify_one();
 	}
 
 	void PooledThread::run()
@@ -91,11 +90,11 @@ namespace BansheeEngine
 		onThreadStarted(mName);
 
 		{
-			BS_LOCK_MUTEX(mMutex);
+			Lock lock(mMutex);
 			mThreadStarted = true;
 		}
 
-		BS_THREAD_NOTIFY_ONE(mStartedCond);
+		mStartedCond.notify_one();
 
 		while(true)
 		{
@@ -103,10 +102,10 @@ namespace BansheeEngine
 
 			{
 				{
-					BS_LOCK_MUTEX_NAMED(mMutex, lock);
+					Lock lock(mMutex);
 
 					while (!mThreadReady)
-						BS_THREAD_WAIT(mReadyCond, mMutex, lock);
+						mReadyCond.wait(lock);
 
 					worker = mWorkerMethod;
 				}
@@ -133,14 +132,14 @@ namespace BansheeEngine
 #endif
 
 			{
-				BS_LOCK_MUTEX(mMutex);
+				Lock lock(mMutex);
 
 				mIdle = true;
 				mIdleTime = std::time(nullptr);
 				mThreadReady = false;
 				mWorkerMethod = nullptr; // Make sure to clear as it could have bound shared pointers and similar
 
-				BS_THREAD_NOTIFY_ONE(mWorkerEndedCond);
+				mWorkerEndedCond.notify_one();
 			}
 		}
 	}
@@ -150,34 +149,34 @@ namespace BansheeEngine
 		blockUntilComplete();
 
 		{
-			BS_LOCK_MUTEX(mMutex);
+			Lock lock(mMutex);
 			mWorkerMethod = nullptr;
 			mThreadReady = true;
 		}
 
-		BS_THREAD_NOTIFY_ONE(mReadyCond);
-		BS_THREAD_JOIN((*mThread));
-		BS_THREAD_DESTROY(mThread);
+		mReadyCond.notify_one();
+		mThread->join();
+		bs_delete(mThread);
 	}
 
 	void PooledThread::blockUntilComplete()
 	{
-		BS_LOCK_MUTEX_NAMED(mMutex, lock);
+		Lock lock(mMutex);
 
 		while (!mIdle)
-			BS_THREAD_WAIT(mWorkerEndedCond, mMutex, lock);
+			mWorkerEndedCond.wait(lock);
 	}
 
 	bool PooledThread::isIdle()
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		return mIdle;
 	}
 
 	time_t PooledThread::idleTime()
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		return (time(nullptr) - mIdleTime);
 	}
@@ -189,7 +188,7 @@ namespace BansheeEngine
 
 	UINT32 PooledThread::getId() const
 	{ 
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		return mId; 
 	}
@@ -215,7 +214,7 @@ namespace BansheeEngine
 
 	void ThreadPool::stopAll()
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 		for(auto& thread : mThreads)
 		{
 			destroyThread(thread);
@@ -226,7 +225,7 @@ namespace BansheeEngine
 
 	void ThreadPool::clearUnused()
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 		mAge = 0;
 
 		if(mThreads.size() <= mDefaultCapacity)
@@ -280,7 +279,7 @@ namespace BansheeEngine
 	{
 		UINT32 age = 0;
 		{
-			BS_LOCK_MUTEX(mMutex);
+			Lock lock(mMutex);
 			age = ++mAge;
 		}
 
@@ -288,7 +287,7 @@ namespace BansheeEngine
 			clearUnused();
 
 		PooledThread* newThread = nullptr;
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		for(auto& thread : mThreads)
 		{
@@ -315,7 +314,7 @@ namespace BansheeEngine
 	{
 		UINT32 numAvailable = 0;
 
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 		for(auto& thread : mThreads)
 		{
 			if(thread->isIdle())
@@ -329,7 +328,7 @@ namespace BansheeEngine
 	{
 		UINT32 numActive = 0;
 
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 		for(auto& thread : mThreads)
 		{
 			if(!thread->isIdle())
@@ -341,7 +340,7 @@ namespace BansheeEngine
 
 	UINT32 ThreadPool::getNumAllocated() const
 	{
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 
 		return (UINT32)mThreads.size();
 	}

+ 2 - 2
Source/BansheeUtility/Source/Win32/BsWin32CrashHandler.cpp

@@ -456,7 +456,7 @@ namespace BansheeEngine
 		const String& file, UINT32 line) const
 	{
 		// Win32 debug methods are not thread safe
-		Lock<>(m->mutex);
+		Lock(m->mutex);
 
 		String stackTrace = getStackTrace();
 
@@ -490,7 +490,7 @@ namespace BansheeEngine
 		EXCEPTION_POINTERS* exceptionData = (EXCEPTION_POINTERS*)exceptionDataPtr;
 
 		// Win32 debug methods are not thread safe
-		Lock<>(m->mutex);
+		Lock(m->mutex);
 
 		win32_initPSAPI();
 		win32_loadSymbols();

+ 5 - 5
Source/BansheeUtility/Source/Win32/BsWin32Window.cpp

@@ -215,7 +215,7 @@ namespace BansheeEngine
 			FrameVector<HWND> windowsToDisable;
 			FrameVector<HWND> windowsToBringToFront;
 			{
-				BS_LOCK_MUTEX(sWindowsMutex);
+				Lock lock(sWindowsMutex);
 
 				if (m->isModal)
 				{
@@ -268,7 +268,7 @@ namespace BansheeEngine
 			{
 				FrameVector<HWND> windowsToEnable;
 				{
-					BS_LOCK_MUTEX(sWindowsMutex);
+					Lock lock(sWindowsMutex);
 
 					// Hidden dependency: All windows must be re-enabled before a window is destroyed, otherwise the incorrect
 					// window in the z order will be activated.
@@ -312,7 +312,7 @@ namespace BansheeEngine
 		}
 
 		{
-			BS_LOCK_MUTEX(sWindowsMutex);
+			Lock lock(sWindowsMutex);
 
 			auto iterFind = std::find(sAllWindows.begin(), sAllWindows.end(), this);
 			sAllWindows.erase(iterFind);
@@ -460,7 +460,7 @@ namespace BansheeEngine
 		Vector<HWND> windowsToEnable;
 
 		{
-			BS_LOCK_MUTEX(sWindowsMutex);
+			Lock lock(sWindowsMutex);
 			for (auto& window : sAllWindows)
 				windowsToEnable.push_back(window->m->hWnd);
 		}
@@ -475,7 +475,7 @@ namespace BansheeEngine
 		HWND bringToFrontHwnd = 0;
 
 		{
-			BS_LOCK_MUTEX(sWindowsMutex)
+			Lock lock(sWindowsMutex);
 
 			if (!sModalWindowStack.empty())
 			{

+ 1 - 1
Source/SBansheeEngine/Include/BsScriptObjectManager.h

@@ -69,7 +69,7 @@ namespace BansheeEngine
 
 		Vector<ScriptObjectBase*> mFinalizedObjects[2];
 		UINT32 mFinalizedQueueIdx;
-		BS_MUTEX(mMutex);
+		Mutex mMutex;
 	};
 
 	/** @} */

+ 2 - 2
Source/SBansheeEngine/Source/BsScriptObjectManager.cpp

@@ -81,7 +81,7 @@ namespace BansheeEngine
 	{
 		assert(instance != nullptr);
 
-		BS_LOCK_MUTEX(mMutex);
+		Lock lock(mMutex);
 		mFinalizedObjects[mFinalizedQueueIdx].push_back(instance);
 	}
 
@@ -94,7 +94,7 @@ namespace BansheeEngine
 	{
 		UINT32 readQueueIdx = 0;
 		{
-			BS_LOCK_MUTEX(mMutex);
+			Lock lock(mMutex);
 			readQueueIdx = mFinalizedQueueIdx;
 			mFinalizedQueueIdx = (mFinalizedQueueIdx + 1) % 2;
 		}