From d337ed1c64de89ed410cfe208ea162e4cd4ae503 Mon Sep 17 00:00:00 2001 From: myaaaaaaaaa <103326468+myaaaaaaaaa@users.noreply.github.com> Date: Wed, 22 Feb 2023 16:49:48 -0500 Subject: [PATCH] Fix data races in startup/teardown --- core/io/ip.cpp | 8 ++++---- core/os/thread.cpp | 6 +++--- core/os/thread.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/io/ip.cpp b/core/io/ip.cpp index 28b70371200..65728f34f6d 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -75,7 +75,7 @@ struct _IP_ResolverPrivate { Semaphore sem; Thread thread; - bool thread_abort = false; + SafeFlag thread_abort; void resolve_queues() { for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) { @@ -111,7 +111,7 @@ struct _IP_ResolverPrivate { static void _thread_function(void *self) { _IP_ResolverPrivate *ipr = static_cast<_IP_ResolverPrivate *>(self); - while (!ipr->thread_abort) { + while (!ipr->thread_abort.is_set()) { ipr->sem.wait(); ipr->resolve_queues(); } @@ -343,12 +343,12 @@ IP::IP() { singleton = this; resolver = memnew(_IP_ResolverPrivate); - resolver->thread_abort = false; + resolver->thread_abort.clear(); resolver->thread.start(_IP_ResolverPrivate::_thread_function, resolver); } IP::~IP() { - resolver->thread_abort = true; + resolver->thread_abort.set(); resolver->sem.post(); resolver->thread.wait_to_finish(); diff --git a/core/os/thread.cpp b/core/os/thread.cpp index 9d16392b2a5..92865576f3c 100644 --- a/core/os/thread.cpp +++ b/core/os/thread.cpp @@ -50,8 +50,8 @@ void Thread::_set_platform_functions(const PlatformFunctions &p_functions) { platform_functions = p_functions; } -void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) { - Thread::caller_id = _thread_id_hash(p_self->thread.get_id()); +void Thread::callback(ID p_caller_id, const Settings &p_settings, Callback p_callback, void *p_userdata) { + Thread::caller_id = p_caller_id; if (platform_functions.set_priority) { platform_functions.set_priority(p_settings.priority); } @@ -79,7 +79,7 @@ void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_ std::thread empty_thread; thread.swap(empty_thread); } - std::thread new_thread(&Thread::callback, this, p_settings, p_callback, p_user); + std::thread new_thread(&Thread::callback, _thread_id_hash(thread.get_id()), p_settings, p_callback, p_user); thread.swap(new_thread); id = _thread_id_hash(thread.get_id()); } diff --git a/core/os/thread.h b/core/os/thread.h index 3d4c48a760b..6eb21fba65b 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -82,7 +82,7 @@ private: static thread_local ID caller_id; std::thread thread; - static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); + static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); static PlatformFunctions platform_functions;