载入中...
搜索中...
未找到
JobSystem.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include <cstddef>
6#include <functional>
7#include <string>
8
9namespace eve {
10namespace thread {
11
12class JobSystem;
13
17using JobFunc = std::function<void()>;
18
24using ParallelForBody = std::function<void(int first, int last)>;
25
43class Job {
44public:
46 virtual ~Job() = default;
47
55 virtual void wait() = 0;
56
58 virtual bool isDone() const = 0;
59
61 virtual bool hasFailed() const = 0;
62
67 virtual std::string getError() const = 0;
68
73 virtual int getPendingDependencyCount() const = 0;
74
84 virtual void addDependency(Job *predecessor) = 0;
85
99};
100
110public:
112 virtual ~TaskGroup() = default;
113
119 virtual Job *fork(JobFunc body) = 0;
120
128
133 virtual void wait() = 0;
134
136 virtual int getPendingCount() const = 0;
137};
138
158public:
160 virtual ~JobSystem() = default;
161
163 virtual int getWorkerCount() const = 0;
164
166 virtual bool isRunning() const = 0;
167
169 virtual int getPendingCount() const = 0;
170
172 virtual int getOutstandingCount() const = 0;
173
174 // ---- heap jobs (caller owns returned handles) ----
175
182 virtual Job *submit(JobFunc body) = 0;
183
190 virtual Job *createJob(JobFunc body) = 0;
191
201 virtual void schedule(Job *job) = 0;
202
217 virtual Job *parallelFor(int first, int last, ParallelForBody body, int chunk = 1) = 0;
218
224
225 // ---- frame jobs (arena-allocated; valid until next beginFrame) ----
226
233
240
245 virtual Job *parallelForFrame(int first, int last, ParallelForBody body, int chunk = 1) = 0;
246
252
259 virtual void beginFrame() = 0;
260
267 virtual void endFrame() = 0;
268
269 // ---- lifecycle ----
270
275 virtual void waitAll() = 0;
276
281 virtual void stop() = 0;
282};
283
296
297} // namespace thread
298} // namespace eve
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::int32_t first
JobFunc onComplete
std::map< Cell, Cell > predecessor
std::string body
ViewPreparation callback
Abstract job scheduler: dependencies, parallel_for, task_group, arena.
Definition JobSystem.h:157
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
Releases JobSystem resources.
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:43
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
Releases Job resources.
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:109
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
Releases TaskGroup resources.
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:24
std::function< void()> JobFunc
A unit of work executed by a JobSystem worker.
Definition JobSystem.h:17
JobSystem * createJobSystem(int workerCount)
Create a JobSystem with the configured backend.
Definition JobSystem.cpp:14
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16