Added: - Added `JobExecutor.cs` for job execution management. - Added `JobInfo.cs` to hold job execution information. - Added `TestJobSystem.cs` for unit tests of the job system. - Added `TestJobs.cs` for additional job implementation tests. - Added `WorkerThread.cs` to manage worker threads for jobs. Changed: - Changed `AssemblyInfo.cs.cs` to include a global using directive for `unsafe JobExecuteFunc`. - Changed `IJob.cs` to include an overload of the `Execute` method with a `threadIndex` parameter. - Changed `JobHandle.cs` to include an `IsValid` property and updated internal structure. - Changed `JobScheduler.cs` to improve job scheduling and management. - Changed `JobsUtility.cs` to enhance job management functions. - Changed `MemoryBlock.cs` to reference the heap from which memory was allocated. - Changed `ParallelNoiseBenchmark.cs` to include benchmarks for the job system. - Changed `Program.cs` to execute benchmarks instead of previous test code. Removed: - Removed `.gitignore` entries for default ignored files. - Removed `JobBase.cs` to shift from structs to classes for jobs. - Removed `JobExtensions.cs` indicating a change in job scheduling. - Removed `JobStruct.cs` indicating a change in job structure. - Removed `encodings.xml`, `indexLayout.xml`, and `vcs.xml` files to simplify project configuration. - Removed fields from `JobData.cs` to simplify the job data structure. - Removed `TestJobSystem.csproj` entries related to old project structure.
95 lines
2.6 KiB
C#
95 lines
2.6 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 unsafe void WorkLoop()
|
|
{
|
|
while (!_scheduler.IsCancellationRequested)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var spinner = new SpinWait();
|
|
for (var i = 0; i < 25; i++)
|
|
{
|
|
spinner.SpinOnce(-1);
|
|
|
|
if (_scheduler.HasWork())
|
|
{
|
|
goto FoundWork;
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
_scheduler.WaitForWork();
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
FoundWork:
|
|
;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_thread.Join();
|
|
_localQueue.Clear();
|
|
}
|
|
} |