WIP Make collections thread-safe and add safe iteration

Replaced static lists and dictionaries with thread-safe ConcurrentDictionary or ThreadLocal collections for various item components and systems. Updated all relevant code to use snapshots (ToArray, ToList) for safe iteration, and added helper methods for marking and clearing changed connections. These changes improve thread safety and prevent potential concurrency issues in multi-threaded scenarios.
This commit is contained in:
Eero
2025-12-28 04:59:56 +08:00
parent 90962b2328
commit 46595b1399
25 changed files with 519 additions and 245 deletions
@@ -1,17 +1,19 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Xml.Linq;
namespace Barotrauma.Items.Components
{
partial class Ladder : ItemComponent
{
public static List<Ladder> List { get; } = new List<Ladder>();
private static readonly ConcurrentDictionary<Ladder, byte> _ladderDict = new ConcurrentDictionary<Ladder, byte>();
public static IEnumerable<Ladder> List => _ladderDict.Keys;
public Ladder(Item item, ContentXElement element)
: base(item, element)
{
InitProjSpecific(element);
List.Add(this);
_ladderDict.TryAdd(this, 0);
}
partial void InitProjSpecific(ContentXElement element);
@@ -28,7 +30,7 @@ namespace Barotrauma.Items.Components
{
base.RemoveComponentSpecific();
RemoveProjSpecific();
List.Remove(this);
_ladderDict.TryRemove(this, out _);
}
partial void RemoveProjSpecific();