From 6dffc1ef46bcfba4e904a5e5850a3fffb5e3b54c Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Wed, 23 Sep 2020 11:24:01 +0200 Subject: [PATCH] Ignore thread models when compiling with NO_THREAD The thread model option for physics (2D) and rendering (single-unsafe, single-safe, multithread), was causing crashes/locks when set as multithreaded and exported for a platform that does not support threads (namely HTML5). This commit ensures that when threads support is not available, that option is ignored, and the equivalent of "single-unsafe" is always used instead. (cherry picked from commit f3c6ac1d7183a9aea2809ee283a5cd899caf9f3d) --- main/main.cpp | 4 ++++ servers/physics_2d/physics_2d_server_sw.cpp | 4 ++++ servers/physics_2d/physics_2d_server_wrap_mt.h | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/main/main.cpp b/main/main.cpp index 551b5ccf5d9..c6bac052d2d 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1118,9 +1118,13 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } if (rtm >= 0 && rtm < 3) { +#ifdef NO_THREADS + rtm = OS::RENDER_THREAD_UNSAFE; // No threads available on this platform. +#else if (editor) { rtm = OS::RENDER_THREAD_SAFE; } +#endif OS::get_singleton()->_render_thread_mode = OS::RenderThreadMode(rtm); } diff --git a/servers/physics_2d/physics_2d_server_sw.cpp b/servers/physics_2d/physics_2d_server_sw.cpp index be8e1a7a33d..2a7d887fd44 100644 --- a/servers/physics_2d/physics_2d_server_sw.cpp +++ b/servers/physics_2d/physics_2d_server_sw.cpp @@ -1455,7 +1455,11 @@ Physics2DServerSW::Physics2DServerSW() { island_count = 0; active_objects = 0; collision_pairs = 0; +#ifdef NO_THREADS + using_threads = false; +#else using_threads = int(ProjectSettings::get_singleton()->get("physics/2d/thread_model")) == 2; +#endif flushing_queries = false; }; diff --git a/servers/physics_2d/physics_2d_server_wrap_mt.h b/servers/physics_2d/physics_2d_server_wrap_mt.h index eec0a3933f6..38ca6099bc4 100644 --- a/servers/physics_2d/physics_2d_server_wrap_mt.h +++ b/servers/physics_2d/physics_2d_server_wrap_mt.h @@ -326,6 +326,9 @@ public: template static Physics2DServer *init_server() { +#ifdef NO_THREADS + return memnew(T); // Always single unsafe when no threads are available. +#else int tm = GLOBAL_DEF("physics/2d/thread_model", 1); if (tm == 0) // single unsafe return memnew(T); @@ -333,6 +336,7 @@ public: return memnew(Physics2DServerWrapMT(memnew(T), false)); else // multi threaded return memnew(Physics2DServerWrapMT(memnew(T), true)); +#endif } #undef ServerNameWrapMT