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:
@@ -222,13 +222,14 @@ namespace Barotrauma.Items.Components
|
||||
public void SetRecipientsDirty()
|
||||
{
|
||||
recipientsDirty = true;
|
||||
if (IsPower) { Powered.ChangedConnections.Add(this); }
|
||||
if (IsPower) { Powered.MarkConnectionChanged(this); }
|
||||
}
|
||||
|
||||
private void RefreshRecipients()
|
||||
{
|
||||
recipients.Clear();
|
||||
foreach (var wire in wires)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (var wire in wires.ToArray())
|
||||
{
|
||||
Connection recipient = wire.OtherConnection(this);
|
||||
if (recipient != null) { recipients.Add(recipient); }
|
||||
@@ -267,8 +268,8 @@ namespace Barotrauma.Items.Components
|
||||
//Check if both connections belong to a larger grid
|
||||
if (prevOtherConnection.recipients.Count > 1 && recipients.Count > 1)
|
||||
{
|
||||
Powered.ChangedConnections.Add(prevOtherConnection);
|
||||
Powered.ChangedConnections.Add(this);
|
||||
Powered.MarkConnectionChanged(prevOtherConnection);
|
||||
Powered.MarkConnectionChanged(this);
|
||||
}
|
||||
else if (recipients.Count > 1)
|
||||
{
|
||||
@@ -284,7 +285,7 @@ namespace Barotrauma.Items.Components
|
||||
else if (Grid.Connections.Count == 2)
|
||||
{
|
||||
//Delete the grid as these were the only 2 devices
|
||||
Powered.Grids.Remove(Grid.ID);
|
||||
Powered.Grids.TryRemove(Grid.ID, out _);
|
||||
Grid = null;
|
||||
prevOtherConnection.Grid = null;
|
||||
}
|
||||
@@ -325,8 +326,8 @@ namespace Barotrauma.Items.Components
|
||||
else
|
||||
{
|
||||
//Flag change so that proper grids can be formed
|
||||
Powered.ChangedConnections.Add(this);
|
||||
Powered.ChangedConnections.Add(otherConnection);
|
||||
Powered.MarkConnectionChanged(this);
|
||||
Powered.MarkConnectionChanged(otherConnection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,7 +340,8 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
LastSentSignal = signal;
|
||||
enumeratingWires = true;
|
||||
foreach (var wire in wires)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (var wire in wires.ToArray())
|
||||
{
|
||||
Connection recipient = wire.OtherConnection(this);
|
||||
if (recipient == null) { continue; }
|
||||
@@ -354,14 +356,14 @@ namespace Barotrauma.Items.Components
|
||||
GameMain.LuaCs.Hook.Call("signalReceived." + recipient.item.Prefab.Identifier, signal, recipient);
|
||||
}
|
||||
|
||||
foreach (CircuitBoxConnection connection in CircuitBoxConnections)
|
||||
foreach (CircuitBoxConnection connection in CircuitBoxConnections.ToArray())
|
||||
{
|
||||
connection.ReceiveSignal(signal);
|
||||
GameMain.LuaCs.Hook.Call("signalReceived", signal, connection.Connection);
|
||||
GameMain.LuaCs.Hook.Call("signalReceived." + connection.Connection.Item.Prefab.Identifier, signal, connection);
|
||||
}
|
||||
enumeratingWires = false;
|
||||
foreach (var removedWire in removedWires)
|
||||
foreach (var removedWire in removedWires.ToArray())
|
||||
{
|
||||
wires.Remove(removedWire);
|
||||
}
|
||||
@@ -372,14 +374,16 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
conn.LastReceivedSignal = signal;
|
||||
|
||||
foreach (ItemComponent ic in conn.item.Components)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (ItemComponent ic in conn.item.Components.ToArray())
|
||||
{
|
||||
ic.ReceiveSignal(signal, conn);
|
||||
}
|
||||
|
||||
if (conn.Effects == null || signal.value == "0") { return; }
|
||||
|
||||
foreach (StatusEffect effect in conn.Effects)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (StatusEffect effect in conn.Effects.ToArray())
|
||||
{
|
||||
conn.Item.ApplyStatusEffect(effect, ActionType.OnUse, (float)Timing.Step);
|
||||
}
|
||||
@@ -389,13 +393,15 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (IsPower && Grid != null)
|
||||
{
|
||||
Powered.ChangedConnections.Add(this);
|
||||
foreach (Connection c in recipients)
|
||||
Powered.MarkConnectionChanged(this);
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (Connection c in recipients.ToArray())
|
||||
{
|
||||
Powered.ChangedConnections.Add(c);
|
||||
Powered.MarkConnectionChanged(c);
|
||||
}
|
||||
}
|
||||
foreach (var wire in wires)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (var wire in wires.ToArray())
|
||||
{
|
||||
wire.RemoveConnection(this);
|
||||
recipientsDirty = true;
|
||||
@@ -403,7 +409,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
if (enumeratingWires)
|
||||
{
|
||||
foreach (var wire in wires)
|
||||
foreach (var wire in wires.ToArray())
|
||||
{
|
||||
removedWires.Add(wire);
|
||||
}
|
||||
@@ -446,7 +452,8 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
XElement newElement = new XElement(IsOutput ? "output" : "input", new XAttribute("name", Name));
|
||||
|
||||
foreach (var wire in wires.OrderBy(w => w.Item.ID))
|
||||
// Use ToArray() snapshot before OrderBy for thread-safe iteration
|
||||
foreach (var wire in wires.ToArray().OrderBy(w => w.Item.ID))
|
||||
{
|
||||
newElement.Add(new XElement("link",
|
||||
new XAttribute("w", wire.Item.ID.ToString()),
|
||||
|
||||
+13
-7
@@ -148,14 +148,16 @@ namespace Barotrauma.Items.Components
|
||||
Vector2 wireNodeOffset = item.Submarine == null ? Vector2.Zero : item.Submarine.HiddenSubPosition + amount;
|
||||
foreach (Connection c in Connections)
|
||||
{
|
||||
foreach (Wire wire in c.Wires)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (Wire wire in c.Wires.ToArray())
|
||||
{
|
||||
if (wire == null) { continue; }
|
||||
TryMoveWire(wire);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var wire in DisconnectedWires)
|
||||
// Use ToList() snapshot for thread-safe iteration
|
||||
foreach (var wire in DisconnectedWires.ToList())
|
||||
{
|
||||
TryMoveWire(wire);
|
||||
}
|
||||
@@ -387,7 +389,7 @@ namespace Barotrauma.Items.Components
|
||||
}
|
||||
foreach (var connection in Connections)
|
||||
{
|
||||
Powered.ChangedConnections.Remove(connection);
|
||||
Powered.UnmarkConnectionChanged(connection);
|
||||
connection.Recipients.Clear();
|
||||
}
|
||||
Connections.Clear();
|
||||
@@ -412,15 +414,19 @@ namespace Barotrauma.Items.Components
|
||||
msg.WriteByte((byte)Connections.Count);
|
||||
foreach (Connection connection in Connections)
|
||||
{
|
||||
msg.WriteVariableUInt32((uint)connection.Wires.Count);
|
||||
foreach (Wire wire in connection.Wires)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
var wiresSnapshot = connection.Wires.ToArray();
|
||||
msg.WriteVariableUInt32((uint)wiresSnapshot.Length);
|
||||
foreach (Wire wire in wiresSnapshot)
|
||||
{
|
||||
msg.WriteUInt16(wire?.Item == null ? (ushort)0 : wire.Item.ID);
|
||||
}
|
||||
}
|
||||
|
||||
msg.WriteUInt16((ushort)DisconnectedWires.Count);
|
||||
foreach (Wire disconnectedWire in DisconnectedWires)
|
||||
// Use ToList() snapshot for thread-safe iteration
|
||||
var disconnectedSnapshot = DisconnectedWires.ToList();
|
||||
msg.WriteUInt16((ushort)disconnectedSnapshot.Count);
|
||||
foreach (Wire disconnectedWire in disconnectedSnapshot)
|
||||
{
|
||||
msg.WriteUInt16(disconnectedWire.Item.ID);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
@@ -31,7 +32,8 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
private float thirdInverseMax = 0, loadEqnConstant = 0;
|
||||
|
||||
private static readonly Dictionary<string, string> connectionPairs = new Dictionary<string, string>
|
||||
// Thread-safe immutable dictionary for connection pairs (read-only after initialization)
|
||||
private static readonly ImmutableDictionary<string, string> connectionPairs = new Dictionary<string, string>
|
||||
{
|
||||
{ "power_in", "power_out"},
|
||||
{ "signal_in", "signal_out" },
|
||||
@@ -40,7 +42,7 @@ namespace Barotrauma.Items.Components
|
||||
{ "signal_in3", "signal_out3" },
|
||||
{ "signal_in4", "signal_out4" },
|
||||
{ "signal_in5", "signal_out5" }
|
||||
};
|
||||
}.ToImmutableDictionary();
|
||||
|
||||
protected override PowerPriority Priority { get { return PowerPriority.Relay; } }
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Barotrauma.Networking;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
@@ -10,7 +11,8 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
partial class WifiComponent : ItemComponent, IServerSerializable, IClientSerializable
|
||||
{
|
||||
private static readonly List<WifiComponent> list = new List<WifiComponent>();
|
||||
private static readonly ConcurrentDictionary<WifiComponent, byte> _wifiDict = new ConcurrentDictionary<WifiComponent, byte>();
|
||||
private static IEnumerable<WifiComponent> AllWifiComponents => _wifiDict.Keys;
|
||||
|
||||
const int ChannelMemorySize = 10;
|
||||
|
||||
@@ -111,7 +113,7 @@ namespace Barotrauma.Items.Components
|
||||
public WifiComponent(Item item, ContentXElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
list.Add(this);
|
||||
_wifiDict.TryAdd(this, 0);
|
||||
IsActive = true;
|
||||
}
|
||||
|
||||
@@ -156,7 +158,7 @@ namespace Barotrauma.Items.Components
|
||||
/// </summary>
|
||||
public IEnumerable<WifiComponent> GetReceiversInRange()
|
||||
{
|
||||
return list.Where(w => w != this && w.CanReceive(this));
|
||||
return AllWifiComponents.Where(w => w != this && w.CanReceive(this));
|
||||
}
|
||||
|
||||
public bool CanReceive(WifiComponent sender)
|
||||
@@ -185,7 +187,7 @@ namespace Barotrauma.Items.Components
|
||||
/// </summary>
|
||||
public IEnumerable<WifiComponent> GetTransmittersInRange()
|
||||
{
|
||||
return list.Where(w => w != this && w.CanTransmit(this));
|
||||
return AllWifiComponents.Where(w => w != this && w.CanTransmit(this));
|
||||
}
|
||||
|
||||
public bool CanTransmit(WifiComponent sender)
|
||||
@@ -275,7 +277,8 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
if (signal.source != null)
|
||||
{
|
||||
foreach (Connection receiver in wifiComp.item.LastSentSignalRecipients)
|
||||
// Use ToList() snapshot for thread-safe iteration
|
||||
foreach (Connection receiver in wifiComp.item.LastSentSignalRecipients.ToList())
|
||||
{
|
||||
if (!signal.source.LastSentSignalRecipients.Contains(receiver))
|
||||
{
|
||||
@@ -366,7 +369,7 @@ namespace Barotrauma.Items.Components
|
||||
protected override void RemoveComponentSpecific()
|
||||
{
|
||||
base.RemoveComponentSpecific();
|
||||
list.Remove(this);
|
||||
_wifiDict.TryRemove(this, out _);
|
||||
}
|
||||
|
||||
public override XElement Save(XElement parentElement)
|
||||
|
||||
@@ -250,7 +250,8 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (connections[0] != null && connections[1] != null)
|
||||
{
|
||||
foreach (ItemComponent ic in item.Components)
|
||||
// Use ToArray() snapshot for thread-safe iteration
|
||||
foreach (ItemComponent ic in item.Components.ToArray())
|
||||
{
|
||||
if (ic == this) { continue; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user