Skip to content

Commit da4f933

Browse files
authored
Merge pull request godotengine#101072 from hpvb/thread-id-optimization
Optimize `Thread::get_caller_id()`
2 parents 02e4605 + 873eb21 commit da4f933

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

core/os/thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
#include "core/object/script_language.h"
3939

4040
SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
41+
thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();
4142

42-
thread_local Thread::ID Thread::caller_id = Thread::UNASSIGNED_ID;
4343
#endif
4444

4545
Thread::PlatformFunctions Thread::platform_functions;

core/os/thread.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,22 @@ class Thread {
112112
static PlatformFunctions platform_functions;
113113

114114
ID id = UNASSIGNED_ID;
115+
115116
static SafeNumeric<uint64_t> id_counter;
116117
static thread_local ID caller_id;
117118
THREADING_NAMESPACE::thread thread;
118119

119120
static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
120121

121122
static void make_main_thread() { caller_id = MAIN_ID; }
122-
static void release_main_thread() { caller_id = UNASSIGNED_ID; }
123+
static void release_main_thread() { caller_id = id_counter.increment(); }
123124

124125
public:
125126
static void _set_platform_functions(const PlatformFunctions &p_functions);
126127

127128
_FORCE_INLINE_ ID get_id() const { return id; }
128129
// get the ID of the caller thread
129130
_FORCE_INLINE_ static ID get_caller_id() {
130-
if (unlikely(caller_id == UNASSIGNED_ID)) {
131-
caller_id = id_counter.increment();
132-
}
133131
return caller_id;
134132
}
135133
// get the ID of the main thread

0 commit comments

Comments
 (0)