Improve thread safety and performance in core systems

Refactors event, entity, and physics management to use thread-safe and lock-free data structures (Immutable collections, ConcurrentQueue, ConcurrentDictionary, Channel) for improved concurrency and performance. Replaces O(n) queue lookups with O(1) set/dictionary checks, ensures atomic updates for shared state, and optimizes queue draining and deferred action processing. Updates related code to use new APIs and patterns, and adds documentation for thread safety and workflow.
This commit is contained in:
Eero
2025-12-29 16:47:10 +08:00
parent e167a34f32
commit 7b8275100d
17 changed files with 368 additions and 191 deletions
@@ -2555,7 +2555,11 @@ namespace Barotrauma
/// </summary>
public bool IsActive = true;
public bool IsInRemoveQueue;
/// <summary>
/// Thread-safe flag indicating whether this item is queued for removal.
/// Uses volatile to ensure memory visibility across threads.
/// </summary>
public volatile bool IsInRemoveQueue;
public override void Update(float deltaTime, Camera cam)
{
@@ -4903,12 +4907,11 @@ namespace Barotrauma
StaticFixtures.Clear();
}
foreach (Item it in ItemList)
// Optimized: Remove() returns false if not found, no need for Contains() check
// Using _itemDictionary.Values directly avoids property access overhead
foreach (Item it in _itemDictionary.Values)
{
if (it.linkedTo.Contains(this))
{
it.linkedTo.Remove(this);
}
it.linkedTo.Remove(this);
}
RemoveProjSpecific();