Line data Source code
1 : /*
2 : * Copyright (C) 2025 aeml
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <functional>
21 : #include <memory>
22 : #include <vector>
23 :
24 : namespace jobs
25 : {
26 : struct JobHandle
27 : {
28 : // Opaque handle for future extension
29 : std::size_t id{};
30 : };
31 :
32 : class IJob
33 : {
34 : public:
35 320324 : virtual ~IJob() = default;
36 : virtual void Execute() = 0;
37 : };
38 :
39 : class JobSystem
40 : {
41 : public:
42 : JobSystem();
43 : ~JobSystem();
44 :
45 : JobSystem(const JobSystem&) = delete;
46 : JobSystem& operator=(const JobSystem&) = delete;
47 :
48 : JobSystem(JobSystem&&) = delete;
49 : JobSystem& operator=(JobSystem&&) = delete;
50 :
51 : JobHandle Schedule(std::unique_ptr<IJob> job);
52 : JobHandle ScheduleFunction(const std::function<void()>& fn);
53 :
54 : // Dispatches a job to be run in parallel across a range of items.
55 : // Returns a list of handles for each batch job.
56 : std::vector<JobHandle> Dispatch(std::size_t jobCount, std::size_t batchSize, const std::function<void(std::size_t, std::size_t)>& job);
57 :
58 : void Wait(const JobHandle& handle);
59 : void Wait(const std::vector<JobHandle>& handles);
60 :
61 : std::size_t WorkerCount() const noexcept;
62 :
63 : private:
64 : struct Impl;
65 : std::unique_ptr<Impl> m_impl;
66 : };
67 : }
|