diff --git a/Source.Engine/SpatialPartition.cs b/Source.Engine/SpatialPartition.cs index 7e72b9c48..462663ada 100644 --- a/Source.Engine/SpatialPartition.cs +++ b/Source.Engine/SpatialPartition.cs @@ -747,7 +747,7 @@ public sealed class VoxelTree SpatialPartitionImpl? _owner; readonly List _availableVisitBits = new(2048); ushort _nextVisitBit; - readonly ReaderWriterLockSlim _lock = new(); + readonly ReaderWriterLockSlim _lock = new(LockRecursionPolicy.SupportsRecursion); [ThreadStatic] static PartitionVisits? t_visits; @@ -803,8 +803,8 @@ public void Shutdown() { public void InsertIntoTree(ushort hPartition, in Vector3 mins, in Vector3 maxs) { Debug.Assert(hPartition != PARTITION_INVALID_HANDLE); - bool wasReading = t_visits != null; - if (wasReading) _lock.ExitReadLock(); + int readCount = _lock.RecursiveReadCount; + for (int i = 0; i < readCount; i++) _lock.ExitReadLock(); _lock.EnterWriteLock(); try { @@ -834,7 +834,7 @@ public void InsertIntoTree(ushort hPartition, in Vector3 mins, in Vector3 maxs) } finally { _lock.ExitWriteLock(); - if (wasReading) _lock.EnterReadLock(); + for (int i = 0; i < readCount; i++) _lock.EnterReadLock(); } } @@ -844,8 +844,8 @@ public void RemoveFromTree(ushort hPartition) { int nLevel = Unsafe.Add(ref Unsafe.As, sbyte>(ref info.Level), _treeId); if (nLevel < 0) return; - bool wasReading = t_visits != null; - if (wasReading) _lock.ExitReadLock(); + int readCount = _lock.RecursiveReadCount; + for (int i = 0; i < readCount; i++) _lock.ExitReadLock(); _lock.EnterWriteLock(); try { @@ -855,7 +855,7 @@ public void RemoveFromTree(ushort hPartition) { } finally { _lock.ExitWriteLock(); - if (wasReading) _lock.EnterReadLock(); + for (int i = 0; i < readCount; i++) _lock.EnterReadLock(); } }