HLSF single thread pass, dead lock on multi thread test

Signed-off-by: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com>
This commit is contained in:
Youmu Liu
2025-08-22 22:10:05 +08:00
parent fb9398e8be
commit 539300b3fa
172 changed files with 2660 additions and 1862 deletions
@@ -324,6 +324,7 @@ namespace StgSharp.HighPerformance.Memory
return false;
}
if (previous != null) {
previous->NextLevel = n;
}
@@ -381,7 +382,9 @@ namespace StgSharp.HighPerformance.Memory
}
int thread = Environment.CurrentManagedThreadId;
return Interlocked.CompareExchange(ref node->PrevLock, thread, 0) == 0;
return
Interlocked.CompareExchange(ref node->PrevLock, thread, 0) == 0 ||
Volatile.Read(ref node->PrevLock) == thread;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -422,7 +425,9 @@ namespace StgSharp.HighPerformance.Memory
}
int thread = Environment.CurrentManagedThreadId;
return Interlocked.CompareExchange(ref node->NextLock, thread, 0) == 0;
return
Interlocked.CompareExchange(ref node->NextLock, thread, 0) == 0 ||
Volatile.Read(ref node->NextLock) == thread;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -461,20 +466,59 @@ namespace StgSharp.HighPerformance.Memory
return true;
}
if (TryAcquireBucketNodePrevLock(node))
int thread = Environment.CurrentManagedThreadId;
int retryCount = 0;
while (retryCount < 1000)
{
if (TryAcquireBucketNodeNextLock(node))
if (Interlocked.CompareExchange(ref node->PrevLock, thread, 0) == 0 ||
Volatile.Read(ref node->PrevLock) == thread)
{
return true;
} else
{
ReleaseBucketNodePrevLock(node); // 回滚
return false;
if (Interlocked.CompareExchange(ref node->NextLock, thread, 0) == 0 ||
Volatile.Read(ref node->NextLock) == thread)
{
return true;
} else
{
Volatile.Write(ref node->PrevLock, 0);
}
}
retryCount++;
Thread.Sleep(0); // 让出 CPU
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AcquireBucketNodeBothLocks(BucketNode* node)
{
if (node == null) {
return;
}
int thread = Environment.CurrentManagedThreadId;
while (true)
{
if (Interlocked.CompareExchange(ref node->PrevLock, thread, 0) == 0 ||
Volatile.Read(ref node->PrevLock) == thread)
{
if (Interlocked.CompareExchange(ref node->NextLock, thread, 0) == 0 ||
Volatile.Read(ref node->NextLock) == thread)
{
return;
} else
{
Volatile.Write(ref node->PrevLock, 0);
}
}
Thread.Sleep(0); // 让出 CPU
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ReleaseBucketNodeBothLocks(BucketNode* node)
{
@@ -486,17 +530,6 @@ namespace StgSharp.HighPerformance.Memory
ReleaseBucketNodeNextLock(node);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AcquireBucketNodeBothLocks(BucketNode* node)
{
if (node == null) {
return;
}
AcquireBucketNodePrevLock(node);
AcquireBucketNodeNextLock(node);
}
#endregion
}
}