use dotnet thread pool for keen parallel cringe
All checks were successful
Build / Build Launcher (push) Successful in 1m23s
All checks were successful
Build / Build Launcher (push) Successful in 1m23s
This commit is contained in:
60
CringeLauncher/Utils/ThreadPoolScheduler.cs
Normal file
60
CringeLauncher/Utils/ThreadPoolScheduler.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using HarmonyLib;
|
||||
using Havok;
|
||||
using ParallelTasks;
|
||||
using CringeTask = ParallelTasks.Task;
|
||||
|
||||
namespace CringeLauncher.Utils;
|
||||
|
||||
public class ThreadPoolScheduler : IWorkScheduler
|
||||
{
|
||||
public void Schedule(CringeTask item)
|
||||
{
|
||||
ThreadPool.UnsafeQueueUserWorkItem(new ThreadPoolWorkItemTask(item), false);
|
||||
}
|
||||
|
||||
public bool WaitForTasksToFinish(TimeSpan waitTimeout) => true;
|
||||
|
||||
public void ScheduleOnEachWorker(Action action)
|
||||
{
|
||||
}
|
||||
|
||||
public int ReadAndClearExecutionTime() => 0;
|
||||
|
||||
public void SuspendThreads(TimeSpan waitTimeout)
|
||||
{
|
||||
}
|
||||
|
||||
public void ResumeThreads()
|
||||
{
|
||||
}
|
||||
|
||||
public int ThreadCount { get; } = ThreadPool.ThreadCount;
|
||||
}
|
||||
|
||||
[HarmonyPatch]
|
||||
internal class ThreadPoolWorkItemTask(CringeTask task) : IThreadPoolWorkItem
|
||||
{
|
||||
public void Execute()
|
||||
{
|
||||
if (HkBaseSystem.IsInitialized && !HkBaseSystem.IsThreadInitialized)
|
||||
HkBaseSystem.InitThread(Thread.CurrentThread.Name);
|
||||
|
||||
task.Execute();
|
||||
}
|
||||
|
||||
private static MethodBase TargetMethod()
|
||||
{
|
||||
var type = Type.GetType("System.Threading.PortableThreadPool+WorkerThread, System.Private.CoreLib", true);
|
||||
return AccessTools.DeclaredMethod(type, "WorkerThreadStart");
|
||||
}
|
||||
|
||||
private static void Postfix()
|
||||
{
|
||||
if (!HkBaseSystem.IsInitialized || !HkBaseSystem.IsThreadInitialized) return;
|
||||
|
||||
HkBaseSystem.QuitThread();
|
||||
Debug.WriteLine($"Hk Shutdown for {Thread.CurrentThread.Name}");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user