Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
JobSystem.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <thread>
4#include <functional>
5#include <atomic>
6#include <memory>
7#include <semaphore>
8#include <mutex>
9#include <vector>
10
11
12namespace Engine {
13
18 class JobSystem {
19 public:
24 static JobSystem instance;
25 return instance;
26 }
27
32 void initialize(unsigned int threadCount = 0);
33
37 void shutdown();
38
42 void pushJob(std::function<void()> job);
43
47 void parallelFor(int count, std::function<void(int)> func);
48
52 unsigned int getThreadCount() const { return static_cast<unsigned int>(workers.size()); }
53
54 // Tries to execute a single job from the queue (returns true if a job was executed)
55 bool tryExecuteOneJob();
56
57 private:
58 JobSystem() = default;
59 ~JobSystem();
60
61 // Worker thread function loop
62 void workerThreadLoop();
63
64 std::vector<std::thread> workers;
65
66 // Bounded MPMC queue cell structure
67 struct Cell {
68 std::atomic<size_t> sequence;
69 std::function<void()> job;
70 };
71
72 std::unique_ptr<Cell[]> buffer;
73 size_t bufferMask{0};
74
75 // Align queue cursors on separate cache lines to avoid false sharing
76 alignas(64) std::atomic<size_t> enqueuePos{0};
77 alignas(64) std::atomic<size_t> dequeuePos{0};
78
79 // Semaphore to wake worker threads without a mutex
80 std::counting_semaphore<65536> jobSemaphore{0};
81
82 std::atomic<bool> stop{false};
83 std::atomic<bool> initialized{false};
84 };
85
86} // namespace Engine
std::atomic< size_t > enqueuePos
Definition JobSystem.hpp:76
void workerThreadLoop()
Definition JobSystem.cpp:176
static JobSystem & getInstance()
Get the singleton instance.
Definition JobSystem.hpp:23
std::unique_ptr< Cell[]> buffer
Definition JobSystem.hpp:72
std::atomic< size_t > dequeuePos
Definition JobSystem.hpp:77
std::vector< std::thread > workers
Definition JobSystem.hpp:64
unsigned int getThreadCount() const
Returns active thread count.
Definition JobSystem.hpp:52
std::counting_semaphore< 65536 > jobSemaphore
Definition JobSystem.hpp:80
void parallelFor(int count, std::function< void(int)> func)
Splits a total loop count into chunks and evaluates them in parallel.
Definition JobSystem.cpp:143
void initialize(unsigned int threadCount=0)
Spawns worker threads.
Definition JobSystem.cpp:22
bool tryExecuteOneJob()
Definition JobSystem.cpp:109
size_t bufferMask
Definition JobSystem.hpp:73
~JobSystem()
Definition JobSystem.cpp:75
std::atomic< bool > initialized
Definition JobSystem.hpp:83
JobSystem()=default
std::atomic< bool > stop
Definition JobSystem.hpp:82
void shutdown()
Shuts down all worker threads and waits for them to join.
Definition JobSystem.cpp:51
void pushJob(std::function< void()> job)
Enqueues a single void-returning task.
Definition JobSystem.cpp:79
Definition Application.hpp:16
Definition JobSystem.hpp:67
std::atomic< size_t > sequence
Definition JobSystem.hpp:68
std::function< void()> job
Definition JobSystem.hpp:69
dst values count
Definition ufbx.cpp:10795