WIP Make static collections thread-safe using ThreadStatic and ThreadLocal

Refactored various static and instance collections to use [ThreadStatic], ThreadLocal, or local variables to prevent concurrent modification issues during parallel updates. This affects status effect targets, affliction lists, damage modifiers, and cached data in Character, CharacterHealth, Limb, Explosion, Hull, Submarine, and ToolBox classes. Also replaced Dictionary caches with ConcurrentDictionary where appropriate for thread safety.
This commit is contained in:
Eero
2025-12-28 14:14:53 +08:00
parent c5fa49405f
commit 45312af297
7 changed files with 135 additions and 75 deletions
@@ -7,6 +7,7 @@ using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Barotrauma
{
@@ -648,7 +649,11 @@ namespace Barotrauma
}
}
private static readonly Dictionary<Structure, float> damagedStructures = new Dictionary<Structure, float>();
// ThreadLocal for thread-safe structure damage tracking
private static readonly ThreadLocal<Dictionary<Structure, float>> damagedStructuresLocal =
new ThreadLocal<Dictionary<Structure, float>>(() => new Dictionary<Structure, float>());
private static Dictionary<Structure, float> damagedStructures => damagedStructuresLocal.Value;
/// <summary>
/// Returns a dictionary where the keys are the structures that took damage and the values are the amount of damage taken
/// </summary>