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:
@@ -1153,6 +1153,7 @@ namespace Barotrauma
|
||||
EventManager?.EndRound();
|
||||
StatusEffect.StopAll();
|
||||
AfflictionPrefab.ClearAllEffects();
|
||||
PhysicsBodyQueue.Clear();
|
||||
IsRunning = false;
|
||||
|
||||
#if CLIENT
|
||||
|
||||
@@ -14,6 +14,12 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -73,6 +73,11 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
public PhysicsBody PhysicsBody { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Flag to prevent multiple queued refresh requests.
|
||||
/// </summary>
|
||||
private volatile bool physicsBodyRefreshQueued;
|
||||
|
||||
private float radius;
|
||||
[Editable, Serialize(0.0f, IsPropertySaveable.Yes)]
|
||||
public float Radius
|
||||
@@ -83,7 +88,7 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (radius == value) { return; }
|
||||
radius = value;
|
||||
if (PhysicsBody != null) { RefreshPhysicsBodySize(); }
|
||||
if (PhysicsBody != null) { QueuePhysicsBodyRefresh(); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +102,7 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (width == value) { return; }
|
||||
width = value;
|
||||
if (PhysicsBody != null) { RefreshPhysicsBodySize(); }
|
||||
if (PhysicsBody != null) { QueuePhysicsBodyRefresh(); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,10 +116,28 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (height == value) { return; }
|
||||
height = value;
|
||||
if (PhysicsBody != null) { RefreshPhysicsBodySize(); }
|
||||
if (PhysicsBody != null) { QueuePhysicsBodyRefresh(); }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queue the physics body refresh to be executed on the main thread.
|
||||
/// This is necessary because physics body operations are not thread-safe.
|
||||
/// </summary>
|
||||
private void QueuePhysicsBodyRefresh()
|
||||
{
|
||||
if (physicsBodyRefreshQueued) { return; }
|
||||
physicsBodyRefreshQueued = true;
|
||||
PhysicsBodyQueue.EnqueueCreation(() =>
|
||||
{
|
||||
if (!item.Removed)
|
||||
{
|
||||
RefreshPhysicsBodySize();
|
||||
}
|
||||
physicsBodyRefreshQueued = false;
|
||||
});
|
||||
}
|
||||
|
||||
private float currentRadius, currentWidth, currentHeight;
|
||||
|
||||
private Vector2 bodyOffset;
|
||||
|
||||
@@ -308,6 +308,12 @@ namespace Barotrauma.MapCreatures.Behavior
|
||||
private BallastFloraBranch? root;
|
||||
private readonly List<Body> bodies = new List<Body>();
|
||||
|
||||
/// <summary>
|
||||
/// Branches that need physics bodies created on the main thread.
|
||||
/// </summary>
|
||||
private readonly List<BallastFloraBranch> pendingBodyCreations = new List<BallastFloraBranch>();
|
||||
private readonly object pendingBodyCreationsLock = new object();
|
||||
|
||||
private bool isDead;
|
||||
|
||||
public readonly BallastFloraStateMachine StateMachine;
|
||||
@@ -347,7 +353,8 @@ namespace Barotrauma.MapCreatures.Behavior
|
||||
}
|
||||
}
|
||||
UpdateConnections(branch);
|
||||
CreateBody(branch);
|
||||
// OnMapLoaded runs on the main thread, so we can create bodies immediately
|
||||
CreateBody(branch, immediate: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -998,10 +1005,52 @@ namespace Barotrauma.MapCreatures.Behavior
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a body for a branch which works as the hitbox for flamer
|
||||
/// Queue a physics body creation for a branch.
|
||||
/// The actual body will be created on the main thread to ensure thread safety.
|
||||
/// </summary>
|
||||
/// <param name="branch"></param>
|
||||
private void CreateBody(BallastFloraBranch branch)
|
||||
/// <param name="branch">The branch to create a body for</param>
|
||||
/// <param name="immediate">If true, create the body immediately (only safe when called from main thread)</param>
|
||||
private void CreateBody(BallastFloraBranch branch, bool immediate = false)
|
||||
{
|
||||
if (immediate)
|
||||
{
|
||||
CreateBodyImmediate(branch);
|
||||
return;
|
||||
}
|
||||
|
||||
lock (pendingBodyCreationsLock)
|
||||
{
|
||||
pendingBodyCreations.Add(branch);
|
||||
}
|
||||
PhysicsBodyQueue.EnqueueCreation(() => ProcessPendingBodyCreations());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process all pending body creations on the main thread.
|
||||
/// This ensures Farseer Physics operations are thread-safe.
|
||||
/// </summary>
|
||||
private void ProcessPendingBodyCreations()
|
||||
{
|
||||
List<BallastFloraBranch> branchesToProcess;
|
||||
lock (pendingBodyCreationsLock)
|
||||
{
|
||||
if (pendingBodyCreations.Count == 0) { return; }
|
||||
branchesToProcess = new List<BallastFloraBranch>(pendingBodyCreations);
|
||||
pendingBodyCreations.Clear();
|
||||
}
|
||||
|
||||
foreach (var branch in branchesToProcess)
|
||||
{
|
||||
if (branch.Removed) { continue; }
|
||||
CreateBodyImmediate(branch);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Actually create the physics body for a branch.
|
||||
/// Must be called on the main thread.
|
||||
/// </summary>
|
||||
private void CreateBodyImmediate(BallastFloraBranch branch)
|
||||
{
|
||||
Rectangle rect = branch.Rect;
|
||||
Vector2 pos = Parent.Position + Offset + branch.Position;
|
||||
|
||||
@@ -686,6 +686,10 @@ namespace Barotrauma
|
||||
}
|
||||
);
|
||||
|
||||
// Process any physics body creation operations queued during Hull/Structure updates.
|
||||
// BallastFlora growth (from Hull.Update) may queue physics body creations.
|
||||
PhysicsBodyQueue.ProcessPendingCreations();
|
||||
|
||||
#if CLIENT
|
||||
// Hull Cheats need to be executed after Hull update
|
||||
Hull.UpdateCheats(deltaTime, cam);
|
||||
@@ -727,6 +731,10 @@ namespace Barotrauma
|
||||
throw new InvalidOperationException($"Error while updating item {lastUpdatedItem?.Name ?? "null"}", innerException: e);
|
||||
}
|
||||
|
||||
// Process any physics body creation operations that were queued during the parallel update.
|
||||
// This must be done on the main thread because Farseer Physics is not thread-safe.
|
||||
PhysicsBodyQueue.ProcessPendingCreations();
|
||||
|
||||
UpdateAllProjSpecific(scaledDeltaTime);
|
||||
Spawner?.Update();
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
/// <summary>
|
||||
/// Thread-safe queue for deferring physics body creation operations to the main thread.
|
||||
/// This is necessary because Farseer Physics' DynamicTree is not thread-safe,
|
||||
/// and physics bodies cannot be safely created during parallel item updates.
|
||||
/// </summary>
|
||||
static class PhysicsBodyQueue
|
||||
{
|
||||
private static readonly object _lock = new object();
|
||||
private static readonly Queue<Action> _pendingCreations = new Queue<Action>();
|
||||
|
||||
/// <summary>
|
||||
/// Enqueues a physics body creation action to be executed on the main thread.
|
||||
/// This method is thread-safe and can be called from parallel update loops.
|
||||
/// </summary>
|
||||
/// <param name="createAction">The action that creates the physics body</param>
|
||||
public static void EnqueueCreation(Action createAction)
|
||||
{
|
||||
if (createAction == null) { return; }
|
||||
lock (_lock)
|
||||
{
|
||||
_pendingCreations.Enqueue(createAction);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of pending physics body creation operations.
|
||||
/// </summary>
|
||||
public static int PendingCount
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _pendingCreations.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes all pending physics body creation operations.
|
||||
/// Must be called on the main thread, outside of any parallel loops.
|
||||
/// </summary>
|
||||
public static void ProcessPendingCreations()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Action action;
|
||||
lock (_lock)
|
||||
{
|
||||
if (_pendingCreations.Count == 0) { break; }
|
||||
action = _pendingCreations.Dequeue();
|
||||
}
|
||||
try
|
||||
{
|
||||
action?.Invoke();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.ThrowError($"Error processing deferred physics body creation: {e.Message}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears all pending physics body creation operations.
|
||||
/// Should be called when ending a round or cleaning up.
|
||||
/// </summary>
|
||||
public static void Clear()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_pendingCreations.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user