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:
@@ -456,7 +456,8 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
item.SendSignal(conditionSignal, "condition_out");
|
||||
|
||||
foreach (var component in item.Components)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (var component in item.Components.ToArray())
|
||||
{
|
||||
if (component is IDeteriorateUnderStress deteriorateUnderStress)
|
||||
{
|
||||
@@ -713,7 +714,8 @@ namespace Barotrauma.Items.Components
|
||||
#endif
|
||||
|
||||
if (LastActiveTime > Timing.TotalTime) { return true; }
|
||||
foreach (ItemComponent ic in item.Components)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (ItemComponent ic in item.Components.ToArray())
|
||||
{
|
||||
if (ic is Fabricator || ic is Deconstructor)
|
||||
{
|
||||
@@ -761,7 +763,8 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
private float GetDeteriorationDelayMultiplier()
|
||||
{
|
||||
foreach (ItemComponent ic in item.Components)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (ItemComponent ic in item.Components.ToArray())
|
||||
{
|
||||
if (ic is Engine engine)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user