Bläddra i källkod

Gotten rid of thread checks for synced deferred contexts

Marko Pintera 12 år sedan
förälder
incheckning
1098318b19

+ 57 - 1
CamelotCore/Include/CmCommandQueue.h

@@ -14,6 +14,11 @@ namespace CamelotFramework
 		CommandQueueNoSync() {}
 		virtual ~CommandQueueNoSync() {}
 
+		bool isValidThread(CM_THREAD_ID_TYPE ownerThread) const
+		{
+			return CM_THREAD_CURRENT_ID == ownerThread;
+		}
+
 		void lock() 
 		{
 		};
@@ -32,6 +37,11 @@ namespace CamelotFramework
 		{ }
 		virtual ~CommandQueueSync() {}
 
+		bool isValidThread(CM_THREAD_ID_TYPE ownerThread) const
+		{
+			return true;
+		}
+
 		void lock() 
 		{
 			mLock.lock();
@@ -178,6 +188,9 @@ namespace CamelotFramework
 		 */
 		bool isEmpty();
 
+	protected:
+		void throwInvalidThreadException(const String& message) const;
+
 	private:
 		std::queue<QueuedCommand>* mCommands;
 
@@ -237,13 +250,28 @@ namespace CamelotFramework
 			:CommandQueueBase(threadId, allowAllThreads)
 		{ }
 
-		~CommandQueue() {}
+		~CommandQueue() 
+		{
+#if CM_DEBUG_MODE
+#if CM_THREAD_SUPPORT != 0
+			if(!isValidThread(getThreadId()))
+				throwInvalidThreadException("Command queue accessed outside of its creation thread.");
+#endif
+#endif
+		}
 
 		/**
 		 * @copydoc CommandQueueBase::queueReturn
 		 */
 		AsyncOp queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
 		{
+#if CM_DEBUG_MODE
+#if CM_THREAD_SUPPORT != 0
+			if(!isValidThread(getThreadId()))
+				throwInvalidThreadException("Command queue accessed outside of its creation thread.");
+#endif
+#endif
+
 			lock();
 			AsyncOp asyncOp = CommandQueueBase::queueReturn(commandCallback, _notifyWhenComplete, _callbackId);
 			unlock();
@@ -256,6 +284,13 @@ namespace CamelotFramework
 		 */
 		void queue(boost::function<void()> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
 		{
+#if CM_DEBUG_MODE
+#if CM_THREAD_SUPPORT != 0
+			if(!isValidThread(getThreadId()))
+				throwInvalidThreadException("Command queue accessed outside of its creation thread.");
+#endif
+#endif
+
 			lock();
 			CommandQueueBase::queue(commandCallback, _notifyWhenComplete, _callbackId);
 			unlock();
@@ -266,6 +301,13 @@ namespace CamelotFramework
 		 */
 		std::queue<QueuedCommand>* flush()
 		{
+#if CM_DEBUG_MODE
+#if CM_THREAD_SUPPORT != 0
+			if(!isValidThread(getThreadId()))
+				throwInvalidThreadException("Command queue accessed outside of its creation thread.");
+#endif
+#endif
+
 			lock();
 			std::queue<QueuedCommand>* commands = CommandQueueBase::flush();
 			unlock();
@@ -278,6 +320,13 @@ namespace CamelotFramework
 		 */
 		void cancelAll()
 		{
+#if CM_DEBUG_MODE
+#if CM_THREAD_SUPPORT != 0
+			if(!isValidThread(getThreadId()))
+				throwInvalidThreadException("Command queue accessed outside of its creation thread.");
+#endif
+#endif
+
 			lock();
 			CommandQueueBase::cancelAll();
 			unlock();
@@ -288,6 +337,13 @@ namespace CamelotFramework
 		 */
 		bool isEmpty()
 		{
+#if CM_DEBUG_MODE
+#if CM_THREAD_SUPPORT != 0
+			if(!isValidThread(getThreadId()))
+				throwInvalidThreadException("Command queue accessed outside of its creation thread.");
+#endif
+#endif
+
 			lock();
 			bool empty = CommandQueueBase::isEmpty();
 			unlock();

+ 1 - 1
CamelotCore/Include/CmRenderSystem.h

@@ -436,7 +436,7 @@ namespace CamelotFramework
 		CM_THREAD_TYPE* mRenderThread;
 #endif
 
-		CommandQueue<CommandQueueNoSync>* mCommandQueue;
+		CommandQueue<CommandQueueSync>* mCommandQueue;
 
 		UINT32 mMaxCommandNotifyId; // ID that will be assigned to the next command with a notifier callback
 		vector<UINT32>::type mCommandsCompleted; // Completed commands that have notifier callbacks set up

+ 5 - 27
CamelotCore/Source/CmCommandQueue.cpp

@@ -28,30 +28,12 @@ namespace CamelotFramework
 
 	CommandQueueBase::~CommandQueueBase()
 	{
-#if CM_DEBUG_MODE
-#if CM_THREAD_SUPPORT != 0
-		if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
-		{
-			CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
-		}
-#endif
-#endif
-
 		if(mCommands != nullptr)
 			CM_DELETE(mCommands, queue<QueuedCommand>, PoolAlloc);
 	}
 
 	AsyncOp CommandQueueBase::queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
 	{
-//#if CM_DEBUG_MODE
-//#if CM_THREAD_SUPPORT != 0
-//		if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
-//		{
-//			CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
-//		}
-//#endif
-//#endif
-
 #if CM_DEBUG_MODE
 		breakIfNeeded(mCommandQueueIdx, mMaxDebugIdx);
 
@@ -72,15 +54,6 @@ namespace CamelotFramework
 
 	void CommandQueueBase::queue(boost::function<void()> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
 	{
-//#if CM_DEBUG_MODE
-//#if CM_THREAD_SUPPORT != 0
-//		if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
-//		{
-//			CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
-//		}
-//#endif
-//#endif
-
 #if CM_DEBUG_MODE
 		breakIfNeeded(mCommandQueueIdx, mMaxDebugIdx);
 
@@ -167,6 +140,11 @@ namespace CamelotFramework
 		return true;
 	}
 
+	void CommandQueueBase::throwInvalidThreadException(const String& message) const
+	{
+		CM_EXCEPT(InternalErrorException, message);
+	}
+
 #if CM_DEBUG_MODE
 	CM_STATIC_MUTEX_CLASS_INSTANCE(CommandQueueBreakpointMutex, CommandQueueBase);
 

+ 2 - 2
CamelotCore/Source/CmRenderSystem.cpp

@@ -80,7 +80,7 @@ namespace CamelotFramework {
 
 		if(mCommandQueue != nullptr)
 		{
-			CM_DELETE(mCommandQueue, CommandQueue<CommandQueueNoSync>, GenAlloc);
+			CM_DELETE(mCommandQueue, CommandQueue<CommandQueueSync>, GenAlloc);
 			mCommandQueue = nullptr;
 		}
 
@@ -91,7 +91,7 @@ namespace CamelotFramework {
 	RenderWindowPtr RenderSystem::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
 	{
 		mRenderThreadId = CM_THREAD_CURRENT_ID;
-		mCommandQueue = CM_NEW(CommandQueue<CommandQueueNoSync>, GenAlloc) CommandQueue<CommandQueueNoSync>(CM_THREAD_CURRENT_ID, true);
+		mCommandQueue = CM_NEW(CommandQueue<CommandQueueSync>, GenAlloc) CommandQueue<CommandQueueSync>(CM_THREAD_CURRENT_ID, true);
 		mPrimaryWindowDesc = primaryWindowDesc;
 
 		initRenderThread();

+ 0 - 1
TODO.txt

@@ -9,7 +9,6 @@ When I'm canceling command queue commands I might be canceling important user co
    - input lag?
 
 <<<<<<<Resource update/read>>>>>>>>
-get rid of thread checks for synced contexts (currently theyre commented out)
 RenderSystem does syncing external to CommandQueue
  - That probably won't be needed once I have syncable CommandQueue