Unstable Add thread-safe queue for deferred physics body creation
Introduces PhysicsBodyQueue to safely defer physics body creation to the main thread, addressing thread-safety issues with Farseer Physics during parallel updates. Updates LevelResource, TriggerComponent, BallastFloraBehavior, and MapEntity to use the queue for all physics body creation and refresh operations, ensuring they are processed outside of parallel loops. Also adds cleanup of the queue at round end.
This commit is contained in:
+21
-2
@@ -13,6 +13,12 @@ namespace Barotrauma.Items.Components
|
||||
private Holdable holdable;
|
||||
|
||||
private float deattachTimer;
|
||||
|
||||
/// <summary>
|
||||
/// Flag to prevent multiple queued creation requests.
|
||||
/// Uses volatile to ensure visibility across threads.
|
||||
/// </summary>
|
||||
private volatile bool triggerBodyCreationQueued;
|
||||
|
||||
[Serialize(1.0f, IsPropertySaveable.No, description: "How long it takes to deattach the item from the level walls (in seconds).")]
|
||||
public float DeattachDuration
|
||||
@@ -109,9 +115,22 @@ namespace Barotrauma.Items.Components
|
||||
}
|
||||
else
|
||||
{
|
||||
if (trigger == null)
|
||||
if (trigger == null && !triggerBodyCreationQueued)
|
||||
{
|
||||
CreateTriggerBody();
|
||||
// Queue the physics body creation to be processed on the main thread.
|
||||
// This is necessary because physics body creation is not thread-safe
|
||||
// and Update() may be called from a parallel loop.
|
||||
triggerBodyCreationQueued = true;
|
||||
PhysicsBodyQueue.EnqueueCreation(() =>
|
||||
{
|
||||
// Double-check that trigger hasn't been created yet
|
||||
// (in case this was called multiple times before queue processing)
|
||||
if (trigger == null && !item.Removed)
|
||||
{
|
||||
CreateTriggerBody();
|
||||
}
|
||||
triggerBodyCreationQueued = false;
|
||||
});
|
||||
}
|
||||
if (trigger != null && Vector2.DistanceSquared(item.SimPosition, trigger.SimPosition) > 0.01f)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user