[3.2] Improve thread IDs to avoid collisions with threads not created by the Godot API.

This commit is contained in:
bruvzg 2021-03-07 10:09:02 +02:00
parent b993febf28
commit ce11f7fa3e
No known key found for this signature in database
GPG key ID: 009E1BFE42239B95
2 changed files with 17 additions and 16 deletions

View file

@ -41,9 +41,13 @@ void (*Thread::set_priority_func)(Thread::Priority) = nullptr;
void (*Thread::init_func)() = nullptr; void (*Thread::init_func)() = nullptr;
void (*Thread::term_func)() = nullptr; void (*Thread::term_func)() = nullptr;
Thread::ID Thread::main_thread_id = 1; uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) {
SafeNumeric<Thread::ID> Thread::last_thread_id{ 1 }; static std::hash<std::thread::id> hasher;
thread_local Thread::ID Thread::caller_id = 1; return hasher(p_t);
}
Thread::ID Thread::main_thread_id = _thread_id_hash(std::this_thread::get_id());
thread_local Thread::ID Thread::caller_id = _thread_id_hash(std::this_thread::get_id());
void Thread::_set_platform_funcs( void Thread::_set_platform_funcs(
Error (*p_set_name_func)(const String &), Error (*p_set_name_func)(const String &),
@ -57,7 +61,7 @@ void Thread::_set_platform_funcs(
} }
void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) { void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) {
Thread::caller_id = p_self->id; Thread::caller_id = _thread_id_hash(p_self->thread.get_id());
if (set_priority_func) { if (set_priority_func) {
set_priority_func(p_settings.priority); set_priority_func(p_settings.priority);
} }
@ -73,7 +77,7 @@ void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_cal
} }
void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) { void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
if (id != 0) { if (id != _thread_id_hash(std::thread::id())) {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
WARN_PRINT("A Thread object has been re-started without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread."); WARN_PRINT("A Thread object has been re-started without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread.");
#endif #endif
@ -81,22 +85,22 @@ void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_
std::thread empty_thread; std::thread empty_thread;
thread.swap(empty_thread); thread.swap(empty_thread);
} }
id = last_thread_id.increment();
std::thread new_thread(&Thread::callback, this, p_settings, p_callback, p_user); std::thread new_thread(&Thread::callback, this, p_settings, p_callback, p_user);
thread.swap(new_thread); thread.swap(new_thread);
id = _thread_id_hash(thread.get_id());
} }
bool Thread::is_started() const { bool Thread::is_started() const {
return id != 0; return id != _thread_id_hash(std::thread::id());
} }
void Thread::wait_to_finish() { void Thread::wait_to_finish() {
if (id != 0) { if (id != _thread_id_hash(std::thread::id())) {
ERR_FAIL_COND_MSG(id == get_caller_id(), "A Thread can't wait for itself to finish."); ERR_FAIL_COND_MSG(id == get_caller_id(), "A Thread can't wait for itself to finish.");
thread.join(); thread.join();
std::thread empty_thread; std::thread empty_thread;
thread.swap(empty_thread); thread.swap(empty_thread);
id = 0; id = _thread_id_hash(std::thread::id());
} }
} }
@ -108,11 +112,8 @@ Error Thread::set_name(const String &p_name) {
return ERR_UNAVAILABLE; return ERR_UNAVAILABLE;
} }
Thread::Thread() :
id(0) {}
Thread::~Thread() { Thread::~Thread() {
if (id != 0) { if (id != _thread_id_hash(std::thread::id())) {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
WARN_PRINT("A Thread object has been destroyed without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread."); WARN_PRINT("A Thread object has been destroyed without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread.");
#endif #endif

View file

@ -62,9 +62,10 @@ private:
friend class Main; friend class Main;
static ID main_thread_id; static ID main_thread_id;
static SafeNumeric<ID> last_thread_id;
ID id; static uint64_t _thread_id_hash(const std::thread::id &p_t);
ID id = _thread_id_hash(std::thread::id());
static thread_local ID caller_id; static thread_local ID caller_id;
std::thread thread; std::thread thread;
@ -97,7 +98,6 @@ public:
///< waits until thread is finished, and deallocates it. ///< waits until thread is finished, and deallocates it.
void wait_to_finish(); void wait_to_finish();
Thread();
~Thread(); ~Thread();
#else #else
_FORCE_INLINE_ ID get_id() const { return 0; } _FORCE_INLINE_ ID get_id() const { return 0; }