Put thread pool to individual type define as thread resource manager.

This commit is contained in:
Youmu Liu
2025-10-22 20:35:05 +08:00
parent 3788db4cd0
commit 3707e5bd74
4 changed files with 93 additions and 20 deletions
@@ -28,6 +28,7 @@
using StgSharp.Collections;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -46,34 +47,63 @@ namespace StgSharp.Mathematics.Numeric
{
SpareWrap = CapacityFixedStackBuilder.Create(wrapSpan);
SingleThreadAllocation = new CapacityFixedBag<MatrixParallelWrap>(wrapSpan.Length);
SpareThreads = threadCount;
SpareThreadCount = threadCount;
}
internal MatrixParallelPool(int threadCount, CapacityFixedStack<MatrixParallelWrap> wrapSpan)
{
SpareWrap = wrapSpan;
SingleThreadAllocation = new CapacityFixedBag<MatrixParallelWrap>(wrapSpan.Count);
SpareThreads = threadCount;
SpareThreadCount = threadCount;
}
public int SpareThreads { get; set; }
public int SpareThreadCount { get; set; }
internal CapacityFixedBag<MatrixParallelWrap> SingleThreadAllocation { get; set; }
internal CapacityFixedStack<MatrixParallelWrap> SpareWrap { get; set; }
public bool TryRequestLeaderHandle(out MatrixParallelWrap.Handle handle) { }
public void ReturnIndividualHandle(ref MatrixParallelWrap.Handle handle)
{
MatrixParallelWrap wrap = handle.Wrap;
handle.Role = 0;
if (wrap.IsAllAvailable)
{
SingleThreadAllocation.RemoveAt(wrap.Magic);
SpareWrap.Push(wrap);
SpareThreadCount += wrap.WrapTaskCapacity;
}
}
public bool TryRequestWrap(out MatrixParallelWrap? wrap)
public bool TryRentIndividualHandle(out MatrixParallelWrap.Handle handle)
{
foreach (MatrixParallelWrap item in SingleThreadAllocation)
{
if (item.TryGetLeaderHandle(out handle!)) {
return true;
}
}
if (TryRequestWrap(out MatrixParallelWrap wrap))
{
wrap!.Magic = SingleThreadAllocation.Count;
SingleThreadAllocation.Add(wrap);
if (wrap.TryGetLeaderHandle(out handle!)) {
return true;
}
}
handle = null!;
return false;
}
public bool TryRequestWrap([NotNull]out MatrixParallelWrap? wrap)
{
if (SpareWrap.TryPop(out wrap))
{
SpareThreads -= wrap.WrapTaskCapacity;
SpareThreadCount -= wrap.WrapTaskCapacity;
return true;
} else
{
return false;
}
wrap = null!;
return false;
}
}