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
@@ -49,16 +49,16 @@ namespace StgSharp.HighPerformance.Memory
private bool AssertBackwardBoundary(ulong current, Entry* target, int level)
{
int size = (level * 2) + 6;
ulong align = current >> size;
return align == target->Position >> size && align == (current + target->Size) >> size;
ulong align = (ulong)levelSizeArray[level + 1];
ulong targetOffset = target->Position - (ulong)m_Buffer;
return current / align == targetOffset / align;
}
private bool AssertForwardBoundary(ulong current, Entry* target, int level)
{
int size = (level * 2) + 6;
ulong align = current >> size;
return align == target->Position >> size && align == (current - target->Size) >> size;
ulong align = (ulong)levelSizeArray[level + 1];
ulong targetOffset = target->Position + target->Size - (ulong)m_Buffer;
return current / align == targetOffset / align;
}
/// <summary>
@@ -209,6 +209,10 @@ namespace StgSharp.HighPerformance.Memory
return;
}
AcquireEntrySpinLock(entry);
entry->LockBuffer = 0;
entry->State = EntryState.Empty;
entry->PreviousNear = EmptyHandle;
entry->NextNear = EmptyHandle;
_entries.Free((nuint)entry);
}
@@ -308,15 +312,13 @@ namespace StgSharp.HighPerformance.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetEntryState(Entry* entry)
{
return entry != null ? Volatile.Read(ref entry->State) : 0;
return Volatile.Read(ref entry->State);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void SetEntryState(Entry* entry, int newState)
{
if (entry != null) {
Volatile.Write(ref entry->State, newState);
}
Volatile.Write(ref entry->State, newState);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -343,7 +345,6 @@ namespace StgSharp.HighPerformance.Memory
return GetEntryState(entry) == EntryState.Merging;
}
// Entry 前驱锁操作(控制PreviousNear字段)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryAcquireEntryPrevLock(Entry* entry)
{
@@ -352,7 +353,9 @@ namespace StgSharp.HighPerformance.Memory
}
int thread = Environment.CurrentManagedThreadId;
return Interlocked.CompareExchange(ref entry->PrevLock, thread, 0) == 0;
return
Interlocked.CompareExchange(ref entry->PrevLock, thread, 0) == 0 ||
Volatile.Read(ref entry->PrevLock) == thread;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -392,7 +395,9 @@ namespace StgSharp.HighPerformance.Memory
}
int thread = Environment.CurrentManagedThreadId;
return Interlocked.CompareExchange(ref entry->NextLock, thread, 0) == 0;
return
Interlocked.CompareExchange(ref entry->NextLock, thread, 0) == 0 ||
Volatile.Read(ref entry->NextLock) == thread;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -432,17 +437,23 @@ namespace StgSharp.HighPerformance.Memory
return true;
}
if (TryAcquireEntryPrevLock(entry))
int thread = Environment.CurrentManagedThreadId;
// 尝试同时获取前后锁
if (Interlocked.CompareExchange(ref entry->PrevLock, thread, 0) == 0 ||
Volatile.Read(ref entry->PrevLock) == thread)
{
if (TryAcquireEntryNextLock(entry))
if (Interlocked.CompareExchange(ref entry->NextLock, thread, 0) == 0 ||
Volatile.Read(ref entry->NextLock) == thread)
{
return true;
} else
{
ReleaseEntryPrevLock(entry); // 回滚
return false;
// 回滚前锁
Volatile.Write(ref entry->PrevLock, 0);
}
}
return false;
}
@@ -464,8 +475,22 @@ namespace StgSharp.HighPerformance.Memory
return;
}
AcquireEntryPrevLock(entry);
AcquireEntryNextLock(entry);
int thread = Environment.CurrentManagedThreadId;
// 循环直到成功获取两个锁
while (true)
{
if ((Interlocked.CompareExchange(ref entry->PrevLock, thread, 0) == 0 ||
Volatile.Read(ref entry->PrevLock) == thread) &&
(Interlocked.CompareExchange(ref entry->NextLock, thread, 0) == 0 ||
Volatile.Read(ref entry->NextLock) == thread)) {
return;
}
// 回滚前锁
Volatile.Write(ref entry->PrevLock, 0);
Thread.Sleep(0);
}
}
#endregion