Skip to content

Commit

Permalink
Ignore thread models when compiling with NO_THREAD
Browse files Browse the repository at this point in the history
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 f3c6ac1)
  • Loading branch information
Faless authored and akien-mga committed Sep 24, 2020
1 parent bf9090c commit 6dffc1e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions servers/physics_2d/physics_2d_server_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
4 changes: 4 additions & 0 deletions servers/physics_2d/physics_2d_server_wrap_mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,17 @@ class Physics2DServerWrapMT : public Physics2DServer {
template <class T>
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);
else if (tm == 1) // single safe
return memnew(Physics2DServerWrapMT(memnew(T), false));
else // multi threaded
return memnew(Physics2DServerWrapMT(memnew(T), true));
#endif
}

#undef ServerNameWrapMT
Expand Down

0 comments on commit 6dffc1e

Please sign in to comment.