using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Jobs;
///
/// Represents a job that performs a single unit of work.
///
public interface IJob
{
///
/// Executes the job logic.
///
/// The context of the job execution, providing access to thread-specific information and job scheduling capabilities.
void Execute(ref readonly JobExecutionContext ctx);
}
///
/// Represents a job that performs the same operation for a set of items, executed in parallel.
///
public interface IJobParallelFor
{
///
/// Executes the job for a single item at the given index.
///
/// The index of the item to process.
/// The context of the job execution, providing access to thread-specific information and job scheduling capabilities.
void Execute(int loopIndex, ref readonly JobExecutionContext ctx);
}
///
/// Represents a job that performs the same operation for a set of items, executed in parallel.
///
public interface IJobParallel
{
///
/// Executes an operation over a specified range, optionally associating the execution with a particular thread index.
///
/// The zero-based index at which to begin the operation.
/// The zero-based index at which to end the operation.
/// The context of the job execution, providing access to thread-specific information and job scheduling capabilities.
void Execute(int startIndex, int endIndex, ref readonly JobExecutionContext ctx);
}
internal unsafe struct CombinedDependenciesJob : IJob
{
public JobHandle* dependencies;
public int dependencyCount;
public readonly void Execute(ref readonly JobExecutionContext ctx)
{
var span = new Span(dependencies, dependencyCount);
ctx.JobScheduler.WaitAll(span);
NativeMemory.Free(dependencies);
}
}
public static class IJobExtensions
{
public static void Run(this T job, ref readonly JobExecutionContext ctx)
where T : IJob
{
job.Execute(in ctx);
}
public static void RunRef(this ref T job, ref readonly JobExecutionContext ctx)
where T : struct, IJob
{
job.Execute(in ctx);
}
}
public static class IJobParallelForExtensions
{
public static void Run(this T job, int totalIterations, ref readonly JobExecutionContext ctx)
where T : IJobParallelFor
{
for (var i = 0; i < totalIterations; i++)
{
job.Execute(i, in ctx);
}
}
public static void RunRef(this ref T job, int totalIterations, ref readonly JobExecutionContext ctx)
where T : struct, IJobParallelFor
{
for (var i = 0; i < totalIterations; i++)
{
job.Execute(i, in ctx);
}
}
}
public static class IJobParallelExtensions
{
public static void Run(this T job, int totalIterations, ref readonly JobExecutionContext ctx)
where T : IJobParallel
{
job.Execute(0, totalIterations, in ctx);
}
public static void RunRef(this ref T job, int totalIterations, ref readonly JobExecutionContext ctx)
where T : struct, IJobParallel
{
job.Execute(0, totalIterations, in ctx);
}
}