Skip to content

Commit

Permalink
Merge pull request godotengine#102726 from mihe/jolt/thread-safe-errors
Browse files Browse the repository at this point in the history
Skip `Object::to_string` when Jolt Physics is on separate thread
  • Loading branch information
akien-mga committed Feb 11, 2025
2 parents 0636a14 + 1006fa9 commit 9ac02cc
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,7 @@
</member>
<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.
[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 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].
Expand Down
1 change: 1 addition & 0 deletions modules/jolt_physics/jolt_physics_server_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ class JoltPhysicsServer3D final : public PhysicsServer3D {

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; }

void free_space(JoltSpace3D *p_space);
Expand Down
9 changes: 8 additions & 1 deletion modules/jolt_physics/objects/jolt_object_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "jolt_object_3d.h"

#include "../jolt_physics_server_3d.h"
#include "../jolt_project_settings.h"
#include "../spaces/jolt_layers.h"
#include "../spaces/jolt_space_3d.h"
Expand Down Expand Up @@ -137,6 +138,12 @@ bool JoltObject3D::can_interact_with(const JoltObject3D &p_other) 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();
return instance != nullptr ? instance->to_string() : "<unknown>";
return instance != nullptr ? instance->to_string() : fallback_name;
}
5 changes: 0 additions & 5 deletions modules/jolt_physics/objects/jolt_soft_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,3 @@ bool JoltSoftBody3D::is_vertex_pinned(int p_index) const {

return pinned_vertices.has(physics_index);
}

String JoltSoftBody3D::to_string() const {
Object *instance = get_instance();
return instance != nullptr ? instance->to_string() : "<unknown>";
}
2 changes: 0 additions & 2 deletions modules/jolt_physics/objects/jolt_soft_body_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ class JoltSoftBody3D final : public JoltObject3D {
void unpin_all_vertices();

bool is_vertex_pinned(int p_index) const;

String to_string() const;
};

#endif // JOLT_SOFT_BODY_3D_H

0 comments on commit 9ac02cc

Please sign in to comment.