From eb0ca31ac17dfecfcdf78a9e98e1bd5cca76b0c9 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Sun, 27 Dec 2020 16:49:46 +0100 Subject: [PATCH] Add an `OS.get_thread_caller_id()` method This can be used to print thread IDs in logs. This can make it easier to debug multi-threaded applications. Co-authored-by: Khaos (cherry picked from commit 35b046ddf7eab341256cced9d1c77e168c52e256) --- core/bind/core_bind.cpp | 5 +++++ core/bind/core_bind.h | 1 + doc/classes/OS.xml | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 67dbcff2527..c0387ec59f5 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -647,6 +647,10 @@ int _OS::get_power_percent_left() { return OS::get_singleton()->get_power_percent_left(); } +Thread::ID _OS::get_thread_caller_id() const { + return Thread::get_caller_id(); +}; + bool _OS::has_feature(const String &p_feature) const { return OS::get_singleton()->has_feature(p_feature); @@ -1416,6 +1420,7 @@ void _OS::_bind_methods() { ClassDB::bind_method(D_METHOD("alert", "text", "title"), &_OS::alert, DEFVAL("Alert!")); ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &_OS::set_thread_name); + ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &_OS::get_thread_caller_id); ClassDB::bind_method(D_METHOD("set_use_vsync", "enable"), &_OS::set_use_vsync); ClassDB::bind_method(D_METHOD("is_vsync_enabled"), &_OS::is_vsync_enabled); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index c37fa9497fb..6f1bae02d57 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -360,6 +360,7 @@ public: bool is_ok_left_and_cancel_right() const; Error set_thread_name(const String &p_name); + Thread::ID get_thread_caller_id() const; void set_use_vsync(bool p_enable); bool is_vsync_enabled() const; diff --git a/doc/classes/OS.xml b/doc/classes/OS.xml index f2eba23f93a..74c22cbc8c2 100644 --- a/doc/classes/OS.xml +++ b/doc/classes/OS.xml @@ -486,6 +486,14 @@ [b]Note:[/b] This method is implemented on Windows. + + + + + Returns the ID of the current thread. This can be used in logs to ease debugging of multi-threaded applications. + [b]Note:[/b] Thread IDs are not deterministic and may be reused across application restarts. + +