Files
Misaki.HighPerformance/Misaki.HighPerformance.Jobs/WorkerThread.cs
Misaki 3923682b5e Enhance JobScheduler and related classes
Added XML documentation comments to the `JobScheduler` class and its methods.
Added a new method `GetJobStatus` in the `JobScheduler` class for job status retrieval.
Added a new `CollectionHandle` struct for collection management.
Added a new test class `TestUnsafeSparseSet` with unit tests for `UnsafeSparseSet`.

Changed the `WorkerThread` class to improve job retrieval logic with a new `FindJob` method.
Changed the `DynamicArena` class by removing commented-out code to streamline memory management.

Removed commented-out code in the `WorkerThread` class for improved readability.
Removed the `ArenaAllocator` struct from `AllocationManager` to clean up unused code.
Removed the `ParallelWriter` struct from `UnsafeSparseSet`, indicating a shift in handling sparse sets.
2025-09-10 13:17:17 +09:00

114 lines
3.2 KiB
C#

using System.Collections.Concurrent;
namespace Misaki.HighPerformance.Jobs;
internal class WorkerThread : IDisposable
{
private readonly int _index;
private readonly Thread _thread;
private readonly ConcurrentQueue<JobHandle> _localQueue;
private readonly JobScheduler _scheduler;
private readonly Random _random;
internal ConcurrentQueue<JobHandle> LocalQueue => _localQueue;
public WorkerThread(int index, JobScheduler scheduler)
{
_index = index;
_localQueue = new();
_scheduler = scheduler;
_random = new Random(index * 9973 + Environment.TickCount);
_thread = new Thread(WorkLoop)
{
IsBackground = true,
Name = $"WorkerThread-{index}"
};
}
public void Start() => _thread.Start();
private JobHandle FindJob()
{
var handle = JobHandle.Invalid;
if (_localQueue.TryDequeue(out handle)
|| _scheduler.TryStealJob(-1, out handle))
{
return handle;
}
while (true)
{
var randomIndex = _random.Next(0, _scheduler.WorkerCount);
if (_scheduler.TryStealJob(randomIndex, out handle))
{
return handle;
}
}
}
private unsafe void WorkLoop()
{
while (!_scheduler.IsCancellationRequested)
{
var spinner = new SpinWait();
for (var i = 0; i < 25; i++)
{
spinner.SpinOnce(-1);
if (_scheduler.HasWork())
{
// Instead of goto, we still need to go through the WaitForWork to claim a release.
// This causes lock and lots of branches inside the SemaphoreSlim, which lost 0.03ms.
// goto DoWork;
break;
}
}
try
{
_scheduler.WaitForWork();
}
catch (OperationCanceledException)
{
continue;
}
//var handle = JobHandle.Invalid;
//// Always try the local thread and main thread queue first.
//if (!_localQueue.TryDequeue(out handle)
// && !_scheduler.TryStealJob(-1, out handle))
//{
// var randomIndex = _random.Next(0, _scheduler.WorkerCount);
// if (_scheduler.TryStealJob(randomIndex, out var tempHandle))
// {
// handle = tempHandle;
// }
//}
//DoWork:
var handle = FindJob();
ref var jobInfo = ref _scheduler.GetJobInfoReference(handle, out var exist);
if (exist)
{
Interlocked.CompareExchange(ref jobInfo.status, JobStatus.Running, JobStatus.Scheduled);
var executeDelegate = jobInfo.executeDelegate;
if (executeDelegate == null
|| executeDelegate(jobInfo.pJobData, ref jobInfo.jobRanges, ref jobInfo.remainingBatches, _index))
{
_scheduler.MarkJobComplete(handle);
}
}
}
}
public void Dispose()
{
_thread.Join();
_localQueue.Clear();
}
}