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); } public static class IJobExtensions { public static void Run(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 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 ref T job, int totalIterations, ref readonly JobExecutionContext ctx) where T : struct, IJobParallel { job.Execute(0, totalIterations, in ctx); } }