1
0
mirror of https://github.com/godotengine/godot.git synced 2026-01-06 19:41:11 +00:00

Fix thread IDs.

On Linux, thread IDs were not properly assigned with the current approach.
The line:
`std::thread new_thread(&Thread::callback, _thread_id_hash(thread.get_id()), p_settings, p_callback, p_user);`
does not work because the thread ID is not assigned until the thread starts.

This PR changes the behavior to use manually generated thread IDs. Additionally, if a thread is (or may have been created) outside Godot, the method `Thread::attach_external_thread` was added.
This commit is contained in:
Juan Linietsky
2023-04-22 15:34:16 +02:00
parent 24cb43a874
commit a37c30dfc9
7 changed files with 47 additions and 42 deletions

View File

@@ -471,6 +471,8 @@ void Main::print_help(const char *p_binary) {
// The order is the same as in `Main::setup()`, only core and some editor types
// are initialized here. This also combines `Main::setup2()` initialization.
Error Main::test_setup() {
Thread::make_main_thread();
OS::get_singleton()->initialize();
engine = memnew(Engine);
@@ -680,6 +682,8 @@ int Main::test_entrypoint(int argc, char *argv[], bool &tests_need_run) {
*/
Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_phase) {
Thread::make_main_thread();
OS::get_singleton()->initialize();
engine = memnew(Engine);
@@ -1876,6 +1880,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
engine->startup_benchmark_end_measure(); // core
Thread::release_main_thread(); // If setup2() is called from another thread, that one will become main thread, so preventively release this one.
if (p_second_phase) {
return setup2();
}
@@ -1941,7 +1947,9 @@ error:
return exit_code;
}
Error Main::setup2(Thread::ID p_main_tid_override) {
Error Main::setup2() {
Thread::make_main_thread(); // Make whatever thread call this the main thread.
// Print engine name and version
print_line(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE));
@@ -1962,10 +1970,6 @@ Error Main::setup2(Thread::ID p_main_tid_override) {
initialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
if (p_main_tid_override) {
Thread::main_thread_id = p_main_tid_override;
}
#ifdef TOOLS_ENABLED
if (editor || project_manager || cmdline_tool) {
EditorPaths::create();

View File

@@ -60,7 +60,7 @@ public:
static int test_entrypoint(int argc, char *argv[], bool &tests_need_run);
static Error setup(const char *execpath, int argc, char *argv[], bool p_second_phase = true);
static Error setup2(Thread::ID p_main_tid_override = 0);
static Error setup2(); // The thread calling setup2() will effectively become the main thread.
static String get_rendering_driver_name();
#ifdef TESTS_ENABLED
static Error test_setup();