Skip to content

unitycoder/ZeroAllocJobScheduler

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZeroAllocJobScheduler

Maintenance Nuget License C#

A high-performance alloc-free C# job scheduler.
Schedules and executes jobs on a set of worker threads with automatic pooling of internal handles.

Code sample

public class HeavyCalculation : IJob
{
  public void Execute()
  {
    Thread.Sleep(50);  // Simulate heavy work
    Console.WriteLine("Done");
  }
}

// Automatically chooses threads based on your processor count
// Creates a global singleton instance
var scheduler = new JobScheduler("MyThreads");

// You need to pool/create jobs by yourself
var firstJob = new HeavyCalculation();  

var firstHandle = firstJob.Schedule(false); // Schedules job locally; false = user must manually wait for Complete() and and manually Return() the handle to the pool
scheduler.Flush();                          // Dispatches all scheduled jobs to the worker threads                      

firstHandle.Complete();                     // Blocks the thread until the job is complete
firstHandle.Return();                       // Returns job to pool

// Dispose at program exit
scheduler.Dispose();                

Fire-and-forget sample

var scheduler = new JobScheduler("MyThreads"); 

var firstJob = new HeavyCalculation();    
var firstHandle = firstJob.Schedule(true);  // Schedules job locally; true = user can't wait for Complete(), and it will automatically Return() on completion
scheduler.Flush();                          // Dispatches all scheduled jobs to the worker threads   

scheduler.Dispose();                

Syntactic sugar

IJob.Schedule(IList<IJob> jobs, IList<JobHandle> handles);   // Schedules a bunch of jobs at once, and adds their handles to the passed-in list, which is cleared

JobHandle.Complete(JobHandle[] handles);                     // Waits for all JobHandles to finish, and blocks the main thread until they complete
JobHandle.Complete(IList<JobHandle> handles);

JobHandle.Return(JobHandle[] handles);                       // Returns all handles to the pool
JobHandle.Return(IList<JobHandle> handles);

About

A high-performance alloc free c# Jobscheduler.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%