载入中...
搜索中...
未找到
JobSystem.h
浏览该文件的文档.
1#pragma once
2
3#include <cstddef>
4#include <functional>
5#include <string>
6
7namespace eve {
8namespace thread {
9
10class JobSystem;
11
15using JobFunc = std::function<void()>;
16
22using ParallelForBody = std::function<void(int first, int last)>;
23
41class Job {
42public:
43 virtual ~Job() = default;
44
52 virtual void wait() = 0;
53
55 virtual bool isDone() const = 0;
56
58 virtual bool hasFailed() const = 0;
59
64 virtual std::string getError() const = 0;
65
70 virtual int getPendingDependencyCount() const = 0;
71
81 virtual void addDependency(Job *predecessor) = 0;
82
95 virtual void setCompletionCallback(JobFunc callback) = 0;
96};
97
107public:
108 virtual ~TaskGroup() = default;
109
115 virtual Job *fork(JobFunc body) = 0;
116
124
129 virtual void wait() = 0;
130
132 virtual int getPendingCount() const = 0;
133};
134
154public:
155 virtual ~JobSystem() = default;
156
158 virtual int getWorkerCount() const = 0;
159
161 virtual bool isRunning() const = 0;
162
164 virtual int getPendingCount() const = 0;
165
167 virtual int getOutstandingCount() const = 0;
168
169 // ---- heap jobs (caller owns returned handles) ----
170
177 virtual Job *submit(JobFunc body) = 0;
178
185 virtual Job *createJob(JobFunc body) = 0;
186
196 virtual void schedule(Job *job) = 0;
197
212 virtual Job *parallelFor(int first, int last, ParallelForBody body, int chunk = 1) = 0;
213
219
220 // ---- frame jobs (arena-allocated; valid until next beginFrame) ----
221
228
235
240 virtual Job *parallelForFrame(int first, int last, ParallelForBody body, int chunk = 1) = 0;
241
247
254 virtual void beginFrame() = 0;
255
262 virtual void endFrame() = 0;
263
264 // ---- lifecycle ----
265
270 virtual void waitAll() = 0;
271
276 virtual void stop() = 0;
277};
278
288JobSystem *createJobSystem(int workerCount = 0);
289
290} // namespace thread
291} // namespace eve
JobFunc body
JobFunc onComplete
Abstract job scheduler: dependencies, parallel_for, task_group, arena.
Definition JobSystem.h:153
virtual Job * parallelForFrame(int first, int last, ParallelForBody body, int chunk=1)=0
Frame-scoped parallel_for; see parallelFor().
virtual Job * createJob(JobFunc body)=0
Create a paused job (dependencies can be wired, then schedule()).
virtual int getOutstandingCount() const =0
Number of scheduled jobs that have not finished (approximate).
virtual void beginFrame()=0
Start a new frame: join leftover frame jobs and recycle the arena.
virtual void schedule(Job *job)=0
Kick a paused job so it can run.
virtual int getWorkerCount() const =0
Number of scheduler worker threads.
virtual Job * submit(JobFunc body)=0
Create a job and schedule it immediately.
virtual int getPendingCount() const =0
Number of ready jobs waiting for a worker (approximate).
virtual TaskGroup * createTaskGroup()=0
Create a fork/join task group.
virtual TaskGroup * createFrameTaskGroup()=0
Create a fork/join task group whose children are frame-scoped.
virtual ~JobSystem()=default
virtual void waitAll()=0
Block until every scheduled job has finished.
virtual void stop()=0
Stop accepting work and join workers. Idempotent.
virtual Job * createFrameJob(JobFunc body)=0
Create a paused frame-scoped job.
virtual bool isRunning() const =0
Whether the system is still accepting new jobs.
virtual void endFrame()=0
End the frame: join outstanding frame jobs and recycle the arena.
virtual Job * parallelFor(int first, int last, ParallelForBody body, int chunk=1)=0
Run body over [first, last) in parallel.
virtual Job * submitFrame(JobFunc body)=0
Create a frame-scoped job and schedule it immediately.
Handle to a single job inside a JobSystem.
Definition JobSystem.h:41
virtual bool hasFailed() const =0
Whether the job body (or completion callback) failed.
virtual void addDependency(Job *predecessor)=0
Make this job wait for another job to finish first.
virtual int getPendingDependencyCount() const =0
Number of not-yet-completed predecessors this job is waiting for.
virtual void setCompletionCallback(JobFunc callback)=0
Register a callback fired once when the job finishes.
virtual ~Job()=default
virtual std::string getError() const =0
Error message from a failed body or completion callback.
virtual void wait()=0
Block until this job finishes (done or failed).
virtual bool isDone() const =0
Whether the job has finished (done or failed).
Fork/join group: fork() spawns scheduled children, wait() joins them.
Definition JobSystem.h:106
virtual void wait()=0
Join every forked child: block until all of them finish. Safe from a worker thread.
virtual int getPendingCount() const =0
Number of forked children that have not finished yet.
virtual Job * fork(JobFunc body, JobFunc onComplete)=0
Spawn a scheduled child job with a completion callback.
virtual ~TaskGroup()=default
virtual Job * fork(JobFunc body)=0
Spawn a scheduled child job and add it to this group.
std::function< void(int first, int last)> ParallelForBody
Body of a parallel_for subrange.
Definition JobSystem.h:22
std::function< void()> JobFunc
A unit of work executed by a JobSystem worker.
Definition JobSystem.h:15
JobSystem * createJobSystem(int workerCount)
Create a JobSystem with the configured backend.
Definition JobSystem.cpp:14
Definition Build.cpp:11