Browse Source

Merge pull request #102726 from mihe/jolt/thread-safe-errors

Skip `Object::to_string` when Jolt Physics is on separate thread
Rémi Verschelde 6 months ago
parent
commit
9ac02ccbcb

+ 1 - 0
doc/classes/ProjectSettings.xml

@@ -2332,6 +2332,7 @@
 		</member>
 		</member>
 		<member name="physics/3d/run_on_separate_thread" type="bool" setter="" getter="" default="false">
 		<member name="physics/3d/run_on_separate_thread" type="bool" setter="" getter="" default="false">
 			If [code]true[/code], the 3D physics server runs on a separate thread, making better use of multi-core CPUs. If [code]false[/code], the 3D physics server runs on the main thread. Running the physics server on a separate thread can increase performance, but restricts API access to only physics process.
 			If [code]true[/code], the 3D physics server runs on a separate thread, making better use of multi-core CPUs. If [code]false[/code], the 3D physics server runs on the main thread. Running the physics server on a separate thread can increase performance, but restricts API access to only physics process.
+			[b]Note:[/b] When [member physics/3d/physics_engine] is set to [code]Jolt Physics[/code], enabling this setting will prevent the 3D physics server from being able to provide any context when reporting errors and warnings, and will instead always refer to nodes as [code]&lt;unknown&gt;[/code].
 		</member>
 		</member>
 		<member name="physics/3d/sleep_threshold_angular" type="float" setter="" getter="" default="0.139626">
 		<member name="physics/3d/sleep_threshold_angular" type="float" setter="" getter="" default="0.139626">
 			Threshold angular velocity under which a 3D physics body will be considered inactive. See [constant PhysicsServer3D.SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD].
 			Threshold angular velocity under which a 3D physics body will be considered inactive. See [constant PhysicsServer3D.SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD].

+ 1 - 0
modules/jolt_physics/jolt_physics_server_3d.h

@@ -421,6 +421,7 @@ public:
 
 
 	virtual int get_process_info(PhysicsServer3D::ProcessInfo p_process_info) override;
 	virtual int get_process_info(PhysicsServer3D::ProcessInfo p_process_info) override;
 
 
+	bool is_on_separate_thread() const { return on_separate_thread; }
 	bool is_active() const { return active; }
 	bool is_active() const { return active; }
 
 
 	void free_space(JoltSpace3D *p_space);
 	void free_space(JoltSpace3D *p_space);

+ 8 - 1
modules/jolt_physics/objects/jolt_object_3d.cpp

@@ -30,6 +30,7 @@
 
 
 #include "jolt_object_3d.h"
 #include "jolt_object_3d.h"
 
 
+#include "../jolt_physics_server_3d.h"
 #include "../jolt_project_settings.h"
 #include "../jolt_project_settings.h"
 #include "../spaces/jolt_layers.h"
 #include "../spaces/jolt_layers.h"
 #include "../spaces/jolt_space_3d.h"
 #include "../spaces/jolt_space_3d.h"
@@ -137,6 +138,12 @@ bool JoltObject3D::can_interact_with(const JoltObject3D &p_other) const {
 }
 }
 
 
 String JoltObject3D::to_string() const {
 String JoltObject3D::to_string() const {
+	static const String fallback_name = "<unknown>";
+
+	if (JoltPhysicsServer3D::get_singleton()->is_on_separate_thread()) {
+		return fallback_name; // Calling `Object::to_string` is not thread-safe.
+	}
+
 	Object *instance = get_instance();
 	Object *instance = get_instance();
-	return instance != nullptr ? instance->to_string() : "<unknown>";
+	return instance != nullptr ? instance->to_string() : fallback_name;
 }
 }

+ 0 - 5
modules/jolt_physics/objects/jolt_soft_body_3d.cpp

@@ -727,8 +727,3 @@ bool JoltSoftBody3D::is_vertex_pinned(int p_index) const {
 
 
 	return pinned_vertices.has(physics_index);
 	return pinned_vertices.has(physics_index);
 }
 }
-
-String JoltSoftBody3D::to_string() const {
-	Object *instance = get_instance();
-	return instance != nullptr ? instance->to_string() : "<unknown>";
-}

+ 0 - 2
modules/jolt_physics/objects/jolt_soft_body_3d.h

@@ -167,8 +167,6 @@ public:
 	void unpin_all_vertices();
 	void unpin_all_vertices();
 
 
 	bool is_vertex_pinned(int p_index) const;
 	bool is_vertex_pinned(int p_index) const;
-
-	String to_string() const;
 };
 };
 
 
 #endif // JOLT_SOFT_BODY_3D_H
 #endif // JOLT_SOFT_BODY_3D_H