Browse Source

WorkerThreadPool: Fix thread message queue not restored after overridden in a task

Also, simplifies the thread override teardown in MessageQueue.
Pedro J. Estébanez 1 year ago
parent
commit
21c03d1956

+ 1 - 0
core/io/resource_loader.cpp

@@ -395,6 +395,7 @@ void ResourceLoader::_thread_load_function(void *p_userdata) {
 
 	if (load_nesting == 0) {
 		if (mq_override) {
+			MessageQueue::set_thread_singleton_override(nullptr);
 			memdelete(mq_override);
 		}
 		memdelete(load_paths_stack);

+ 1 - 5
core/object/message_queue.cpp

@@ -481,10 +481,7 @@ CallQueue::~CallQueue() {
 	if (!allocator_is_custom) {
 		memdelete(allocator);
 	}
-	// This is done here to avoid a circular dependency between the safety checks and the thread singleton pointer.
-	if (this == MessageQueue::thread_singleton) {
-		MessageQueue::thread_singleton = nullptr;
-	}
+	DEV_ASSERT(!is_current_thread_override);
 }
 
 //////////////////////
@@ -493,7 +490,6 @@ CallQueue *MessageQueue::main_singleton = nullptr;
 thread_local CallQueue *MessageQueue::thread_singleton = nullptr;
 
 void MessageQueue::set_thread_singleton_override(CallQueue *p_thread_singleton) {
-	DEV_ASSERT(p_thread_singleton); // To unset the thread singleton, don't call this with nullptr, but just memfree() it.
 #ifdef DEV_ENABLED
 	if (thread_singleton) {
 		thread_singleton->is_current_thread_override = false;

+ 3 - 0
core/object/worker_thread_pool.cpp

@@ -53,7 +53,9 @@ void WorkerThreadPool::_process_task(Task *p_task) {
 	int pool_thread_index = thread_ids[Thread::get_caller_id()];
 	ThreadData &curr_thread = threads[pool_thread_index];
 	Task *prev_task = nullptr; // In case this is recursively called.
+
 	bool safe_for_nodes_backup = is_current_thread_safe_for_nodes();
+	CallQueue *call_queue_backup = MessageQueue::get_singleton() != MessageQueue::get_main_singleton() ? MessageQueue::get_singleton() : nullptr;
 
 	{
 		// Tasks must start with this unset. They are free to set-and-forget otherwise.
@@ -169,6 +171,7 @@ void WorkerThreadPool::_process_task(Task *p_task) {
 	}
 
 	set_current_thread_safe_for_nodes(safe_for_nodes_backup);
+	MessageQueue::set_thread_singleton_override(call_queue_backup);
 #endif
 }