-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobSystem.cpp
More file actions
272 lines (217 loc) · 8.34 KB
/
JobSystem.cpp
File metadata and controls
272 lines (217 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// ============================================================================
// @file JobSystem.cpp
// @brief Job System implementation — work-stealing thread pool
// @note Part of threading/ module
// ============================================================================
#include "threading/JobSystem.hpp"
#include <algorithm>
#include <chrono>
#include <random>
namespace Caffeine::Threading {
// ============================================================================
// JobHandle
// ============================================================================
JobHandle::JobHandle(u32 index, u32 version, std::atomic<u32>* completionFlag)
: m_index(index)
, m_version(version)
, m_completionFlag(completionFlag) {}
void JobHandle::wait() const {
if (!m_completionFlag) return;
while (m_completionFlag->load(std::memory_order_acquire) == 0) {
std::this_thread::yield();
}
}
bool JobHandle::isComplete() const {
if (!m_completionFlag) return false;
return m_completionFlag->load(std::memory_order_acquire) != 0;
}
// ============================================================================
// JobBarrier
// ============================================================================
JobBarrier::JobBarrier(u32 targetCount)
: m_count(targetCount) {}
void JobBarrier::add() {
m_count.fetch_add(1, std::memory_order_acq_rel);
}
void JobBarrier::release() {
u32 prev = m_count.fetch_sub(1, std::memory_order_acq_rel);
if (prev == 1) {
std::lock_guard<std::mutex> lock(m_mutex);
m_cv.notify_all();
}
}
void JobBarrier::wait() {
if (m_count.load(std::memory_order_acquire) == 0) return;
std::unique_lock<std::mutex> lock(m_mutex);
m_cv.wait(lock, [this]() {
return m_count.load(std::memory_order_acquire) == 0;
});
}
// ============================================================================
// WorkStealDeque
// ============================================================================
void JobSystem::WorkStealDeque::push(JobEntry&& entry) {
std::lock_guard<std::mutex> lock(mutex);
jobs.push_front(std::move(entry));
}
bool JobSystem::WorkStealDeque::pop(JobEntry& out) {
std::lock_guard<std::mutex> lock(mutex);
if (jobs.empty()) return false;
out = std::move(jobs.front());
jobs.pop_front();
return true;
}
bool JobSystem::WorkStealDeque::steal(JobEntry& out) {
std::lock_guard<std::mutex> lock(mutex);
if (jobs.empty()) return false;
out = std::move(jobs.back());
jobs.pop_back();
return true;
}
bool JobSystem::WorkStealDeque::empty() const {
return jobs.empty();
}
u32 JobSystem::WorkStealDeque::size() const {
return static_cast<u32>(jobs.size());
}
// ============================================================================
// JobSystem
// ============================================================================
JobSystem::JobSystem(u32 workerCount) {
if (workerCount == 0) {
u32 hw = std::thread::hardware_concurrency();
m_workerCount = (hw > 1) ? (hw - 1) : 1;
} else {
m_workerCount = workerCount;
}
m_localQueues.reserve(static_cast<usize>(m_workerCount) * PRIORITY_COUNT);
for (usize i = 0; i < static_cast<usize>(m_workerCount) * PRIORITY_COUNT; ++i) {
m_localQueues.push_back(std::make_unique<WorkStealDeque>());
}
m_running.store(true, std::memory_order_release);
m_workers.reserve(m_workerCount);
for (u32 i = 0; i < m_workerCount; ++i) {
m_workers.emplace_back(&JobSystem::workerMain, this, i);
}
}
JobSystem::~JobSystem() {
waitAll();
m_running.store(false, std::memory_order_release);
m_wakeCV.notify_all();
for (auto& t : m_workers) {
if (t.joinable()) t.join();
}
}
std::pair<u32, u32> JobSystem::allocateSlot() {
u32 idx = m_nextSlot.fetch_add(1, std::memory_order_relaxed) % MAX_SLOTS;
u32 ver = m_slots[idx].version.fetch_add(1, std::memory_order_relaxed) + 1;
m_slots[idx].flag.store(0, std::memory_order_release);
return {idx, ver};
}
JobHandle JobSystem::schedule(std::unique_ptr<IJob> job,
JobBarrier* barrier,
JobPriority prio) {
auto [idx, ver] = allocateSlot();
JobEntry entry;
entry.job = std::move(job);
entry.barrier = barrier;
entry.slotIndex = idx;
entry.slotVersion = ver;
m_pendingJobs.fetch_add(1, std::memory_order_acq_rel);
u32 prioIdx = static_cast<u32>(prio);
m_globalQueues[prioIdx].push(std::move(entry));
m_wakeCV.notify_one();
return JobHandle(idx, ver, &m_slots[idx].flag);
}
void JobSystem::pushToQueue(JobEntry&& entry, JobPriority prio, u32 workerHint) {
u32 prioIdx = static_cast<u32>(prio);
if (workerHint < m_workerCount) {
m_localQueues[static_cast<usize>(workerHint) * PRIORITY_COUNT + prioIdx]->push(std::move(entry));
} else {
m_globalQueues[prioIdx].push(std::move(entry));
}
}
bool JobSystem::tryExecuteOne(u32 workerIndex) {
JobEntry entry;
// 1. Try local queues in priority order (Critical first)
for (u32 p = 0; p < PRIORITY_COUNT; ++p) {
auto& localQ = *m_localQueues[static_cast<usize>(workerIndex) * PRIORITY_COUNT + p];
if (localQ.pop(entry)) goto execute;
}
// 2. Try global queues in priority order
for (u32 p = 0; p < PRIORITY_COUNT; ++p) {
if (m_globalQueues[p].pop(entry)) goto execute;
}
// 3. Work-stealing: try to steal from other workers (priority order)
for (u32 p = 0; p < PRIORITY_COUNT; ++p) {
for (u32 w = 1; w <= m_workerCount; ++w) {
u32 victim = (workerIndex + w) % m_workerCount;
auto& victimQ = *m_localQueues[static_cast<usize>(victim) * PRIORITY_COUNT + p];
if (victimQ.steal(entry)) goto execute;
}
}
return false;
execute:
m_activeWorkers.fetch_add(1, std::memory_order_relaxed);
auto startTime = std::chrono::high_resolution_clock::now();
entry.job->execute();
auto endTime = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count();
m_totalJobTimeNs.fetch_add(static_cast<u64>(elapsed), std::memory_order_relaxed);
m_slots[entry.slotIndex].flag.store(1, std::memory_order_release);
if (entry.barrier) {
entry.barrier->release();
}
m_completedTotal.fetch_add(1, std::memory_order_relaxed);
m_activeWorkers.fetch_sub(1, std::memory_order_relaxed);
u32 prev = m_pendingJobs.fetch_sub(1, std::memory_order_acq_rel);
if (prev == 1) {
std::lock_guard<std::mutex> lock(m_waitAllMutex);
m_waitAllCV.notify_all();
}
return true;
}
void JobSystem::workerMain(u32 workerIndex) {
while (m_running.load(std::memory_order_acquire)) {
if (!tryExecuteOne(workerIndex)) {
std::unique_lock<std::mutex> lock(m_wakeMutex);
m_wakeCV.wait_for(lock, std::chrono::milliseconds(1), [this]() {
return !m_running.load(std::memory_order_acquire) ||
m_pendingJobs.load(std::memory_order_acquire) > 0;
});
}
}
// Drain remaining jobs on shutdown
while (tryExecuteOne(workerIndex)) {}
}
void JobSystem::waitAll() {
if (m_pendingJobs.load(std::memory_order_acquire) == 0) return;
std::unique_lock<std::mutex> lock(m_waitAllMutex);
m_waitAllCV.wait(lock, [this]() {
return m_pendingJobs.load(std::memory_order_acquire) == 0;
});
}
JobSystem::Stats JobSystem::stats() const {
Stats s;
s.activeWorkers = m_activeWorkers.load(std::memory_order_relaxed);
s.completedJobsTotal = m_completedTotal.load(std::memory_order_relaxed);
u32 pending = 0;
for (u32 p = 0; p < PRIORITY_COUNT; ++p) {
pending += m_globalQueues[p].size();
}
for (u32 w = 0; w < m_workerCount; ++w) {
for (u32 p = 0; p < PRIORITY_COUNT; ++p) {
pending += m_localQueues[static_cast<usize>(w) * PRIORITY_COUNT + p]->size();
}
}
s.pendingJobs = pending;
u64 completed = s.completedJobsTotal;
if (completed > 0) {
u64 totalNs = m_totalJobTimeNs.load(std::memory_order_relaxed);
s.avgJobMs = static_cast<f64>(totalNs) / static_cast<f64>(completed) / 1e6;
} else {
s.avgJobMs = 0.0;
}
return s;
}
} // namespace Caffeine::Threading