CBT2.0 Make Hull and Level methods thread-safe using ThreadLocal
Replaced instance fields with ThreadLocal collections in Hull.GetConnectedHulls and Level.GetCells to ensure thread safety during parallel updates. Methods now return copies of the collections to prevent concurrent modification issues.
This commit is contained in:
@@ -1214,15 +1214,22 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
private readonly HashSet<Hull> adjacentHulls = new HashSet<Hull>();
|
||||
/// <summary>
|
||||
/// Used in <see cref="GetConnectedHulls"/> - ThreadLocal for thread safety during parallel updates
|
||||
/// </summary>
|
||||
private static readonly ThreadLocal<HashSet<Hull>> adjacentHullsLocal =
|
||||
new ThreadLocal<HashSet<Hull>>(() => new HashSet<Hull>());
|
||||
|
||||
public IEnumerable<Hull> GetConnectedHulls(bool includingThis, int? searchDepth = null, bool ignoreClosedGaps = false)
|
||||
{
|
||||
var adjacentHulls = adjacentHullsLocal.Value;
|
||||
adjacentHulls.Clear();
|
||||
int startStep = 0;
|
||||
searchDepth ??= 100;
|
||||
GetAdjacentHulls(adjacentHulls, ref startStep, searchDepth.Value, ignoreClosedGaps);
|
||||
if (!includingThis) { adjacentHulls.Remove(this); }
|
||||
return adjacentHulls;
|
||||
// Return a copy to prevent concurrent modification if the caller enumerates while another thread calls this method
|
||||
return adjacentHulls.ToHashSet();
|
||||
}
|
||||
|
||||
private void GetAdjacentHulls(HashSet<Hull> connectedHulls, ref int step, int searchDepth, bool ignoreClosedGaps = false)
|
||||
|
||||
Reference in New Issue
Block a user