Skip to content

Commit d09ef71

Browse files
committed
Fix recursive read lock in spatial partition enumeration
1 parent 231c916 commit d09ef71

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

‎Source.Engine/SpatialPartition.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ public sealed class VoxelTree
747747
SpatialPartitionImpl? _owner;
748748
readonly List<ushort> _availableVisitBits = new(2048);
749749
ushort _nextVisitBit;
750-
readonly ReaderWriterLockSlim _lock = new();
750+
readonly ReaderWriterLockSlim _lock = new(LockRecursionPolicy.SupportsRecursion);
751751

752752
[ThreadStatic] static PartitionVisits? t_visits;
753753

@@ -803,8 +803,8 @@ public void Shutdown() {
803803
public void InsertIntoTree(ushort hPartition, in Vector3 mins, in Vector3 maxs) {
804804
Debug.Assert(hPartition != PARTITION_INVALID_HANDLE);
805805

806-
bool wasReading = t_visits != null;
807-
if (wasReading) _lock.ExitReadLock();
806+
int readCount = _lock.RecursiveReadCount;
807+
for (int i = 0; i < readCount; i++) _lock.ExitReadLock();
808808

809809
_lock.EnterWriteLock();
810810
try {
@@ -834,7 +834,7 @@ public void InsertIntoTree(ushort hPartition, in Vector3 mins, in Vector3 maxs)
834834
}
835835
finally {
836836
_lock.ExitWriteLock();
837-
if (wasReading) _lock.EnterReadLock();
837+
for (int i = 0; i < readCount; i++) _lock.EnterReadLock();
838838
}
839839
}
840840

@@ -844,8 +844,8 @@ public void RemoveFromTree(ushort hPartition) {
844844
int nLevel = Unsafe.Add(ref Unsafe.As<TreeArray<sbyte>, sbyte>(ref info.Level), _treeId);
845845
if (nLevel < 0) return;
846846

847-
bool wasReading = t_visits != null;
848-
if (wasReading) _lock.ExitReadLock();
847+
int readCount = _lock.RecursiveReadCount;
848+
for (int i = 0; i < readCount; i++) _lock.ExitReadLock();
849849

850850
_lock.EnterWriteLock();
851851
try {
@@ -855,7 +855,7 @@ public void RemoveFromTree(ushort hPartition) {
855855
}
856856
finally {
857857
_lock.ExitWriteLock();
858-
if (wasReading) _lock.EnterReadLock();
858+
for (int i = 0; i < readCount; i++) _lock.EnterReadLock();
859859
}
860860
}
861861

0 commit comments

Comments
 (0)