A simple job system for Odin.
The design is inspired by fiber-based job systems, most notably the one used at Naughty Dog. (see Parallelizing the Naughty Dog Engine).
BUT This scheduler doesn't use fibers! The queued jobs are just executed directly on the waiting thread. From an API perspective, this is basically the same as fibers. It might require more stack space in your worker threads, but there is no need to allocate stacks for fibers.
Also there is no chance of ending up on another thread after using wait
, so you can use TLS
and OS-provided synchronization primitives like Mutexes and Semaphores.
- dispatching and waiting for jobs to finish
- nested jobs
- utilities for batch processing of slices/arrays
- full control over the thread processing loop
- the jobs are queued on a linked list (FILO queue)
- individual jobs are allocated with
context.temp_allocator
(or manually) - all jobs have to finish the frame you start them (and before calling
free_all(context.temp_allocator
)!
main :: proc() {
jobs.initialize()
g: jobs.Group
jobs.dispatch(.Medium, jobs.make_job(&g, hello_job))
jobs.wait(&g)
jobs.shutdown()
}
hello_job :: proc(_: rawptr) {
fmt.println("Hello from thread", jobs.current_thread_index())
}
See the examples directory for all examples.
All examples:
- hello - a very basic introduction to jobs
- simple - simple overview with most of the features
- boids - boids simulation with Raylib
- improve the examples (boids are especially wonky)
- use atomic linked list instead of spinlocks
All contributions are welcome!