载入中...
搜索中...
未找到
JobSystemThreadPool.h
浏览该文件的文档.
1#pragma once
2
3#include "thread/JobSystem.h"
4#include "thread/Task.h"
5
6#include <memory>
7#include <mutex>
8#include <vector>
9
10namespace eve {
11namespace thread {
12
13class ThreadPool;
14
24class JobSystemThreadPool final : public JobSystem {
25public:
27 struct State;
28
29 explicit JobSystemThreadPool(int workerCount);
30 ~JobSystemThreadPool() override;
31
34
35 int getWorkerCount() const override;
36 bool isRunning() const override;
37 int getPendingCount() const override;
38 int getOutstandingCount() const override;
39
40 Job *submit(JobFunc body) override;
41 Job *createJob(JobFunc body) override;
42 void schedule(Job *job) override;
43 Job *parallelFor(int first, int last, ParallelForBody body, int chunk = 1) override;
44 TaskGroup *createTaskGroup() override;
45
46 Job *submitFrame(JobFunc body) override;
47 Job *createFrameJob(JobFunc body) override;
48 Job *parallelForFrame(int first, int last, ParallelForBody body, int chunk = 1) override;
50 void beginFrame() override;
51 void endFrame() override;
52
53 void waitAll() override;
54 void stop() override;
55
56private:
57 Job *parallelForImpl(int first, int last, ParallelForBody body, int chunk, bool frameScope);
58
59 std::shared_ptr<State> state_;
60 std::unique_ptr<ThreadPool> pool_;
61 std::vector<Task *> poolTasks_;
62 std::mutex lifecycleMu_;
63};
64
65} // namespace thread
66} // namespace eve
JobFunc body
Default JobSystem backend: a dependency-aware scheduler over the existing ThreadPool worker pool.
Job * createJob(JobFunc body) override
Create a paused job (dependencies can be wired, then schedule()).
void endFrame() override
End the frame: join outstanding frame jobs and recycle the arena.
Job * submitFrame(JobFunc body) override
Create a frame-scoped job and schedule it immediately.
Job * parallelFor(int first, int last, ParallelForBody body, int chunk=1) override
Run body over [first, last) in parallel.
JobSystemThreadPool & operator=(const JobSystemThreadPool &)=delete
Job * parallelForFrame(int first, int last, ParallelForBody body, int chunk=1) override
Frame-scoped parallel_for; see parallelFor().
JobSystemThreadPool(const JobSystemThreadPool &)=delete
TaskGroup * createTaskGroup() override
Create a fork/join task group.
int getPendingCount() const override
Number of ready jobs waiting for a worker (approximate).
void waitAll() override
Block until every scheduled job has finished.
int getOutstandingCount() const override
Number of scheduled jobs that have not finished (approximate).
int getWorkerCount() const override
Number of scheduler worker threads.
Job * submit(JobFunc body) override
Create a job and schedule it immediately.
void stop() override
Stop accepting work and join workers. Idempotent.
void schedule(Job *job) override
Kick a paused job so it can run.
TaskGroup * createFrameTaskGroup() override
Create a fork/join task group whose children are frame-scoped.
bool isRunning() const override
Whether the system is still accepting new jobs.
Job * createFrameJob(JobFunc body) override
Create a paused frame-scoped job.
void beginFrame() override
Start a new frame: join leftover frame jobs and recycle the arena.
Abstract job scheduler: dependencies, parallel_for, task_group, arena.
Definition JobSystem.h:153
Handle to a single job inside a JobSystem.
Definition JobSystem.h:41
Fork/join group: fork() spawns scheduled children, wait() joins them.
Definition JobSystem.h:106
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
Definition Build.cpp:11