diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2019-01-26 20:33:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-26 20:33:18 +0100 |
commit | d355469c1f2b13c4c8f8672891c5ddd103191af2 (patch) | |
tree | e38ae8770663ab2471dee0aff9b67d6b46b44ef0 /drivers/unix/thread_posix.cpp | |
parent | ed1b9c1f159b349a0b86b81a1b1bc7a0b0158d31 (diff) | |
parent | 6de04cf04c2ce80f403700184b150d9fe764d5e1 (diff) | |
download | redot-engine-d355469c1f2b13c4c8f8672891c5ddd103191af2.tar.gz |
Merge pull request #25300 from neikeq/improve-thread-id
Improve custom thread numbering for POSIX
Diffstat (limited to 'drivers/unix/thread_posix.cpp')
-rw-r--r-- | drivers/unix/thread_posix.cpp | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/drivers/unix/thread_posix.cpp b/drivers/unix/thread_posix.cpp index a81292d4a2..ef3f5fb49c 100644 --- a/drivers/unix/thread_posix.cpp +++ b/drivers/unix/thread_posix.cpp @@ -40,9 +40,13 @@ #include "core/os/memory.h" #include "core/safe_refcount.h" +static void _thread_id_key_destr_callback(void *p_value) { + memdelete(static_cast<Thread::ID *>(p_value)); +} + static pthread_key_t _create_thread_id_key() { pthread_key_t key; - pthread_key_create(&key, NULL); + pthread_key_create(&key, &_thread_id_key_destr_callback); return key; } @@ -63,7 +67,7 @@ void *ThreadPosix::thread_callback(void *userdata) { ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata); t->id = atomic_increment(&next_thread_id); - pthread_setspecific(thread_id_key, (void *)t->id); + pthread_setspecific(thread_id_key, (void *)memnew(ID(t->id))); ScriptServer::thread_enter(); //scripts may need to attach a stack @@ -89,7 +93,14 @@ Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_ } Thread::ID ThreadPosix::get_thread_id_func_posix() { - return (ID)pthread_getspecific(thread_id_key); + void *value = pthread_getspecific(thread_id_key); + + if (value) + return *static_cast<ID *>(value); + + ID new_id = atomic_increment(&next_thread_id); + pthread_setspecific(thread_id_key, (void *)memnew(ID(new_id))); + return new_id; } void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) { |