Engine → Core → job_system.h
jobs::job_system
| Type | Class |
| Namespace | conjure::core::jobs |
| Include File Path | engine/include/conjure/core/jobs/job_system.h |
| Source File Path | engine/src/core/jobs/job_system.cpp |
| API ↗ | Jobs and threading |
Syntax
Constructors
| Access | Signature | Description |
|---|---|---|
| public | job_system(); |
Important
Additional constructor overloads have been deleted.
Destructors
| Access | Signature | Description |
|---|---|---|
| public | ~job_system(); |
Variables
| Access | Name | Type | Default | Description |
|---|---|---|---|---|
| private | mState | std::unique_ptr<state> | {} |
Functions
Public
current
current[[nodiscard]] static job_system *current() noexcept;The system whose worker the calling thread is, or nullptr on every other thread.
is_running
is_running[[nodiscard]] bool is_running() const noexcept;parallel_for
parallel_forvoid parallel_for(
usize count,
usize grain,
const std::function<void(usize begin, usize end)> &body);Splits [0, count) into ranges of at most grain elements (a grain of 0 counts as 1), runs them across the workers and the caller, and returns when every range has finished. Runs the whole range inline when there are no workers or count <= grain. The body is referenced, not copied, and the dispatch does not allocate once the block pool has warmed up. Nesting from inside a job or another parallel_for is fine.
Per-index variant with grain = max(1, count / (workerCount * 4)).
schedule
schedule[[nodiscard]] job_handle schedule(
std::function<void()> job,
job_priority priority = job_priority::NORMAL);Queues one job and returns its handle. An empty job asserts and yields an invalid handle. When the system is not running, has no workers or the queue is full, the job executes on the caller before this returns.
[[nodiscard]] job_handle schedule(
std::span<const std::function<void()>> jobs,
job_priority priority = job_priority::NORMAL);Queues a batch and returns one handle that completes when every job in it has run. Empty entries are skipped (asserting); an empty batch yields an invalid handle.
void schedule(
std::span<const std::function<void()>> jobs,
job_fence &fence,
job_priority priority = job_priority::NORMAL);Queues a batch that signals a caller-owned fence instead of returning a handle: the handle's shared counter is the one heap allocation of schedule(), so this overload allocates nothing once the block pool has warmed up, which per-frame dispatch (the ECS scheduler's stages) relies on. The batch is added to whatever the fence already counts; wait(fence) returns when all of it has run. The fence must stay alive and in place until then. Empty entries are skipped (asserting); an empty batch leaves the fence unchanged. Inline execution follows the same rules as the handle overload.
start
start[[nodiscard]] result<void> start(
const job_system_desc &desc);Creates desc.workerCount named worker threads and returns once every one of them is running and waiting for work. The thread names are prepared on the calling thread, so a worker allocates nothing from the moment start() returns: an allocation gate opened right after start() is never charged a worker's start-up. Fails with INVALID_STATE when already running and with INVALID_ARGUMENT for a queue capacity of zero or above MAX_QUEUED_JOBS, a worker count above MAX_WORKER_COUNT or a null/empty thread name prefix. Statistics are reset. Resource exhaustion is not one of the reported failures: if the operating system refuses to create a worker thread, the std::system_error that std::thread throws is not caught (engine code never catches) and ends the process like an allocation failure.
stats
stats[[nodiscard]] job_system_stats stats() const noexcept;Snapshot of the counters accumulated since start(). Sums one per-thread slot per worker plus one for every other thread (a few hundred bytes of loads), so reading it every frame is fine.
stop
stopvoid stop();Stops accepting jobs from non-worker threads, lets the workers finish everything that is queued, then joins them. Safe to call twice and from the destructor. Must not be called from one of the system's own workers.
thread_index
thread_index[[nodiscard]] static u32 thread_index() noexcept;0 on the main thread and on any thread that is not a job system worker; 1..N on the workers of the system that owns them.
wait
waitvoid wait(
const job_handle &handle);Blocks until the handle is complete. While waiting the caller executes queued jobs, so this is safe from inside a job and from a worker. Returns immediately for an invalid or complete handle.
void wait(
const job_fence &fence);Blocks until every job scheduled on the fence has run, helping with queued jobs meanwhile like wait(handle). Returns immediately for a complete fence.
wait_idle
wait_idlevoid wait_idle();Blocks until no job is queued or running, helping with queued jobs meanwhile. From inside a job it cannot wait for the jobs the calling thread is itself executing, nor for jobs that are themselves blocked in wait_idle() (two such jobs would wait for each other forever); it returns once every other job has finished. Only a call made outside every job therefore guarantees that the system is completely idle when it returns.
worker_count
worker_count[[nodiscard]] u32 worker_count() const noexcept;Number of worker threads, 0 when everything runs inline (not started or started with workerCount 0).
Private
parallel_for_impl
parallel_for_implvoid parallel_for_impl(
usize count,
usize grain,
detail::range_invoke_fn invoke,
const void *context);