1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-09 12:50:35 +00:00

Enhance portability of threading

This commit is contained in:
Pedro J. Estébanez
2022-08-24 11:33:52 +02:00
parent 1371a97acf
commit 958ecf55fe
4 changed files with 29 additions and 34 deletions

View File

@@ -36,10 +36,7 @@
#include "core/object/script_language.h" #include "core/object/script_language.h"
#include "core/templates/safe_refcount.h" #include "core/templates/safe_refcount.h"
Error (*Thread::set_name_func)(const String &) = nullptr; Thread::PlatformFunctions Thread::platform_functions;
void (*Thread::set_priority_func)(Thread::Priority) = nullptr;
void (*Thread::init_func)() = nullptr;
void (*Thread::term_func)() = nullptr;
uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) { uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) {
static std::hash<std::thread::id> hasher; static std::hash<std::thread::id> hasher;
@@ -49,30 +46,27 @@ uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) {
Thread::ID Thread::main_thread_id = _thread_id_hash(std::this_thread::get_id()); Thread::ID Thread::main_thread_id = _thread_id_hash(std::this_thread::get_id());
thread_local Thread::ID Thread::caller_id = 0; thread_local Thread::ID Thread::caller_id = 0;
void Thread::_set_platform_funcs( void Thread::_set_platform_functions(const PlatformFunctions &p_functions) {
Error (*p_set_name_func)(const String &), platform_functions = p_functions;
void (*p_set_priority_func)(Thread::Priority),
void (*p_init_func)(),
void (*p_term_func)()) {
Thread::set_name_func = p_set_name_func;
Thread::set_priority_func = p_set_priority_func;
Thread::init_func = p_init_func;
Thread::term_func = p_term_func;
} }
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 = _thread_id_hash(p_self->thread.get_id()); Thread::caller_id = _thread_id_hash(p_self->thread.get_id());
if (set_priority_func) { if (platform_functions.set_priority) {
set_priority_func(p_settings.priority); platform_functions.set_priority(p_settings.priority);
} }
if (init_func) { if (platform_functions.init) {
init_func(); platform_functions.init();
} }
ScriptServer::thread_enter(); //scripts may need to attach a stack ScriptServer::thread_enter(); // Scripts may need to attach a stack.
if (platform_functions.wrapper) {
platform_functions.wrapper(p_callback, p_userdata);
} else {
p_callback(p_userdata); p_callback(p_userdata);
}
ScriptServer::thread_exit(); ScriptServer::thread_exit();
if (term_func) { if (platform_functions.term) {
term_func(); platform_functions.term();
} }
} }
@@ -105,8 +99,8 @@ void Thread::wait_to_finish() {
} }
Error Thread::set_name(const String &p_name) { Error Thread::set_name(const String &p_name) {
if (set_name_func) { if (platform_functions.set_name) {
return set_name_func(p_name); return platform_functions.set_name(p_name);
} }
return ERR_UNAVAILABLE; return ERR_UNAVAILABLE;

View File

@@ -63,6 +63,14 @@ public:
Settings() { priority = PRIORITY_NORMAL; } Settings() { priority = PRIORITY_NORMAL; }
}; };
struct PlatformFunctions {
Error (*set_name)(const String &) = nullptr;
void (*set_priority)(Thread::Priority) = nullptr;
void (*init)() = nullptr;
void (*wrapper)(Thread::Callback, void *) = nullptr;
void (*term)() = nullptr;
};
private: private:
friend class Main; friend class Main;
@@ -76,17 +84,10 @@ private:
static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
static Error (*set_name_func)(const String &); static PlatformFunctions platform_functions;
static void (*set_priority_func)(Thread::Priority);
static void (*init_func)();
static void (*term_func)();
public: public:
static void _set_platform_funcs( static void _set_platform_functions(const PlatformFunctions &p_functions);
Error (*p_set_name_func)(const String &),
void (*p_set_priority_func)(Thread::Priority),
void (*p_init_func)() = nullptr,
void (*p_term_func)() = nullptr);
_FORCE_INLINE_ ID get_id() const { return id; } _FORCE_INLINE_ ID get_id() const { return id; }
// get the ID of the caller thread // get the ID of the caller thread

View File

@@ -70,7 +70,7 @@ static Error set_name(const String &p_name) {
} }
void init_thread_posix() { void init_thread_posix() {
Thread::_set_platform_funcs(&set_name, nullptr); Thread::_set_platform_functions({ .set_name = set_name });
} }
#endif // UNIX_ENABLED || PTHREAD_ENABLED #endif // UNIX_ENABLED || PTHREAD_ENABLED

View File

@@ -63,7 +63,7 @@ static void term_thread() {
void init_thread_jandroid(JavaVM *p_jvm, JNIEnv *p_env) { void init_thread_jandroid(JavaVM *p_jvm, JNIEnv *p_env) {
java_vm = p_jvm; java_vm = p_jvm;
env = p_env; env = p_env;
Thread::_set_platform_funcs(nullptr, nullptr, &init_thread, &term_thread); Thread::_set_platform_functions({ .init = init_thread, .term = &term_thread });
} }
void setup_android_thread() { void setup_android_thread() {