Merge pull request #25300 from neikeq/improve-thread-id
Improve custom thread numbering for POSIX
This commit is contained in:
commit
d355469c1f
|
@ -40,9 +40,13 @@
|
||||||
#include "core/os/memory.h"
|
#include "core/os/memory.h"
|
||||||
#include "core/safe_refcount.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() {
|
static pthread_key_t _create_thread_id_key() {
|
||||||
pthread_key_t key;
|
pthread_key_t key;
|
||||||
pthread_key_create(&key, NULL);
|
pthread_key_create(&key, &_thread_id_key_destr_callback);
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +67,7 @@ void *ThreadPosix::thread_callback(void *userdata) {
|
||||||
|
|
||||||
ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata);
|
ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata);
|
||||||
t->id = atomic_increment(&next_thread_id);
|
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
|
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() {
|
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) {
|
void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) {
|
||||||
|
|
||||||
|
|
|
@ -34,9 +34,13 @@
|
||||||
#include "core/safe_refcount.h"
|
#include "core/safe_refcount.h"
|
||||||
#include "core/script_language.h"
|
#include "core/script_language.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() {
|
static pthread_key_t _create_thread_id_key() {
|
||||||
pthread_key_t key;
|
pthread_key_t key;
|
||||||
pthread_key_create(&key, NULL);
|
pthread_key_create(&key, &_thread_id_key_destr_callback);
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +63,7 @@ void *ThreadAndroid::thread_callback(void *userdata) {
|
||||||
setup_thread();
|
setup_thread();
|
||||||
ScriptServer::thread_enter(); //scripts may need to attach a stack
|
ScriptServer::thread_enter(); //scripts may need to attach a stack
|
||||||
t->id = atomic_increment(&next_thread_id);
|
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)));
|
||||||
t->callback(t->user);
|
t->callback(t->user);
|
||||||
ScriptServer::thread_exit();
|
ScriptServer::thread_exit();
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -80,7 +84,14 @@ Thread *ThreadAndroid::create_func_jandroid(ThreadCreateCallback p_callback, voi
|
||||||
|
|
||||||
Thread::ID ThreadAndroid::get_thread_id_func_jandroid() {
|
Thread::ID ThreadAndroid::get_thread_id_func_jandroid() {
|
||||||
|
|
||||||
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 ThreadAndroid::wait_to_finish_func_jandroid(Thread *p_thread) {
|
void ThreadAndroid::wait_to_finish_func_jandroid(Thread *p_thread) {
|
||||||
|
|
Loading…
Reference in New Issue