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:
+42
-29
@@ -45,6 +45,7 @@ using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
using System.Threading;
|
||||
using System.Xml.XPath;
|
||||
|
||||
namespace StgSharp
|
||||
@@ -62,39 +63,51 @@ namespace StgSharp
|
||||
|
||||
// HLSF test arena
|
||||
Console.WriteLine(
|
||||
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Test Arena ===\n");
|
||||
HybridLayerSegregatedFitAllocator hlsfAllocator = new HybridLayerSegregatedFitAllocator(1024 * 1024);
|
||||
Stack<HybridLayerSegregatedFitAllocationHandle> handles = [];
|
||||
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Multi-threaded Test Arena ===\n");
|
||||
HybridLayerSegregatedFitAllocator hlsfAllocator = new HybridLayerSegregatedFitAllocator(1024 * 1024 * 4);
|
||||
|
||||
hlsfAllocator.DisplayEntryLayout(false);
|
||||
for (int c = 0; c < 1; c++)
|
||||
int threadCount = 4;
|
||||
int blocksPerThread = 1024;
|
||||
int blockSize = 1024;
|
||||
|
||||
Thread[] threads = new Thread[threadCount];
|
||||
for (int t = 0; t < threadCount; t++)
|
||||
{
|
||||
int i = 0;
|
||||
for (; i < 1024 * 1024 / 1024; i++)
|
||||
{
|
||||
Console.WriteLine($"{c} cycles: Allocating the {i}th block.");
|
||||
handles.Push(hlsfAllocator.Alloc(1024));
|
||||
hlsfAllocator.DisplayEntryLayout(true);
|
||||
threads[t] = new Thread(() => {
|
||||
int threadIndex = Environment.CurrentManagedThreadId;
|
||||
Stack<HybridLayerSegregatedFitAllocationHandle> handles = new();
|
||||
|
||||
Console.ReadKey();
|
||||
}
|
||||
Console.WriteLine($"{i} allocs called, {handles.Count} handles collected.");
|
||||
Console.ReadKey();
|
||||
while (handles.TryPop(out HybridLayerSegregatedFitAllocationHandle h))
|
||||
{
|
||||
i--;
|
||||
hlsfAllocator.Free(h);
|
||||
Console.WriteLine($"{c} cycles: The {i}th handle is freed");
|
||||
}
|
||||
if (handles.Count != 0)
|
||||
{
|
||||
Console.WriteLine($"[警告] 第{c}轮后Stack未清空,剩余{handles.Count}个handle未释放!");
|
||||
} else
|
||||
{
|
||||
Console.WriteLine("All handles freed.");
|
||||
}
|
||||
Console.ReadKey();
|
||||
// Allocate blocks
|
||||
for (int i = 0; i < blocksPerThread; i++)
|
||||
{
|
||||
Console.WriteLine($"Thread {threadIndex}: Allocating block {i}.");
|
||||
handles.Push(hlsfAllocator.Alloc((uint)blockSize));
|
||||
}
|
||||
|
||||
// Free blocks
|
||||
while (handles.TryPop(out HybridLayerSegregatedFitAllocationHandle handle))
|
||||
{
|
||||
Console.WriteLine($"Thread {threadIndex}: Freeing a block.");
|
||||
hlsfAllocator.Free(handle);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Thread {threadIndex}: All blocks allocated and freed.");
|
||||
});
|
||||
}
|
||||
|
||||
// Start all threads
|
||||
foreach (Thread thread in threads)
|
||||
{
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
// Wait for all threads to complete
|
||||
foreach (Thread thread in threads)
|
||||
{
|
||||
thread.Join();
|
||||
}
|
||||
|
||||
Console.WriteLine("All threads completed successfully.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user