-
Notifications
You must be signed in to change notification settings - Fork 4
/
jobs.odin
344 lines (290 loc) · 8.6 KB
/
jobs.odin
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Jobs
// A simple job system for Odin.
// https://github.com/jakubtomsu/jobs
//
// The design is inspired by fiber-based job systems, most notably the one used at Naughty Dog
// (see https://www.gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine)
//
// BUT! This job system doesn't use any fibers at all.
// Instead of using fibers, this job system just directly runs queued job
// in the waiting thread. From an API perspective, this is the same as fibers.
// It might require more stack space in your worker threads, but there is no
// need to allocate stacks for fibers.
//
// Features:
// - dispatching and waiting for jobs to finish
// - nested jobs
// - utilities for batch processing of slices/arrays
// - full control over the thread processing loop
//
// Notes:
// - the jobs are queued on a linked list (FILO queue)
// - the individual jobs are allocated with context.temp_allocator (or manually allocated)
package jobs
import "core:intrinsics"
import "core:log"
import "core:math"
import "core:runtime"
import "core:sync"
MAIN_THREAD_INDEX :: 0
Job_Proc :: #type proc(arg: rawptr)
Thread_Proc :: #type proc(arg: rawptr)
Thread :: _Thread
// A collection of jobs which can be waited on
Group :: struct {
atomic_counter: u64,
}
Job :: struct {
procedure: Job_Proc,
arg: rawptr,
group: ^Group,
ignored_threads: Ignored_Threads,
_next: ^Job,
}
Ignored_Threads :: bit_set[0 ..< 64]
Priority :: enum u8 {
Medium = 0,
Low,
High,
}
@(private)
_state: struct {
running: bool,
job_lists: [Priority]Job_List,
threads: []Thread,
thread_proc: Thread_Proc,
thread_arg: rawptr,
thread_counter: int,
allocator: runtime.Allocator,
}
Job_List :: struct {
head: ^Job,
mutex: sync.Atomic_Mutex,
}
@(thread_local)
_thread_state: struct {
index: int,
}
num_threads :: proc() -> int {
return 1 + len(_state.threads)
}
// Get the index of the current thread, between 0..<num_threads
current_thread_index :: proc() -> int {
return _thread_state.index
}
// Get the current thread ID from the OS
current_thread_id :: proc() -> u64 {
return _current_thread_id()
}
// Check if the job system is running
is_running :: proc() -> bool {
return _state.running
}
make_job_typed :: proc(
group: ^Group,
arg: ^$T,
p: proc(arg: ^T),
ignored_threads: Ignored_Threads = {},
) -> Job {
assert(group != nil)
assert(p != nil)
return {procedure = cast(proc(a: rawptr))p, arg = rawptr(arg), group = group}
}
make_job_raw :: proc(group: ^Group, arg: rawptr, p: Job_Proc, ignored_threads: Ignored_Threads = {}) -> Job {
assert(group != nil)
assert(p != nil)
return {procedure = p, arg = arg, group = group}
}
make_job_noarg :: proc(group: ^Group, p: Job_Proc, ignored_threads: Ignored_Threads = {}) -> Job {
assert(group != nil)
assert(p != nil)
return {procedure = p, group = group}
}
make_job :: proc {
make_job_typed,
make_job_raw,
make_job_noarg,
}
Batch :: struct($T: typeid) {
data: []T,
index: i32,
offset: i32,
}
// Process slice in a fixed number of batches.
dispatch_batches :: proc(
group: ^Group,
data: []$T,
num_batches := 0,
priority: Priority = .Medium,
p: proc(batch: ^Batch(T)),
) {
num_batches := num_batches
if len(data) <= 0 {
return
}
if num_batches <= 0 {
num_batches = num_threads()
}
dispatch_batches_fixed(
group = group,
data = data,
batch_size = div_ceil(len(data), num_batches),
priority = priority,
p = p,
)
}
// Process slice in batches of fixed size.
// Note: batch_size is the _maximum_ batch size.
dispatch_batches_fixed :: proc(
group: ^Group,
data: []$T,
batch_size := 1,
priority: Priority = .Medium,
p: proc(batch: ^Batch(T)),
allocator := context.temp_allocator,
) {
assert(p != nil)
assert(batch_size > 0)
assert(group != nil)
if len(data) <= 0 {
return // nothing to process
}
num_batches := div_ceil(len(data), batch_size)
jobs := make_slice([]Job, num_batches, allocator)
batches := make_slice([]Batch(T), num_batches, allocator)
for &batch, i in batches {
offset := i * batch_size
batch = {
index = i32(i),
offset = i32(offset),
data = data[offset:min(offset + batch_size, len(data))],
}
}
for &job, i in jobs {
job = {
procedure = Job_Proc(p),
group = group,
arg = &batches[i],
}
}
dispatch_jobs(priority, jobs)
}
@(private)
div_ceil :: #force_inline proc(a, b: int) -> int {
return (a + b - 1) / b
}
// Note: it's on you to clean up the memory after the jobs if you use a custom allocator.
dispatch :: proc(priority: Priority = .Medium, jobs: ..Job, allocator := context.temp_allocator) -> []Job {
_jobs := make([]Job, len(jobs), allocator)
copy(_jobs, jobs)
dispatch_jobs(priority, _jobs)
return _jobs
}
// Push jobs to the queue for the given priority.
dispatch_jobs :: proc(priority: Priority, jobs: []Job) {
for &job, i in jobs {
assert(job.group != nil)
intrinsics.atomic_add(&job.group.atomic_counter, 1)
if i < len(jobs) - 1 {
job._next = &jobs[i + 1]
}
}
sync.atomic_mutex_lock(&_state.job_lists[priority].mutex)
jobs[len(jobs) - 1]._next = _state.job_lists[priority].head
_state.job_lists[priority].head = &jobs[0]
sync.atomic_mutex_unlock(&_state.job_lists[priority].mutex)
}
// Block the current thread until all jobs in the group are finished.
// Other queued jobs are executed while waiting.
wait :: proc(group: ^Group) {
for !group_is_finished(group) {
try_execute_queued_job()
}
group^ = {}
}
// Check if all jobs in the group are finished.
@(require_results)
group_is_finished :: #force_inline proc(group: ^Group) -> bool {
return intrinsics.atomic_load(&group.atomic_counter) <= 0
}
@(private)
run_worker_thread :: proc() {
_thread_state.index = intrinsics.atomic_add(&_state.thread_counter, 1)
if _state.thread_proc != nil {
_state.thread_proc(_state.thread_arg)
}
}
// Warning:
default_thread_proc :: proc(_: rawptr) {
for is_running() {
try_execute_queued_job()
}
}
@(optimization_mode = "speed")
try_execute_queued_job :: proc() -> (result: bool) {
ORDERED_PRIORITIES :: [len(Priority)]Priority{.High, .Medium, .Low}
block: for priority in ORDERED_PRIORITIES {
if _state.job_lists[priority].head == nil {
continue
}
if sync.atomic_mutex_try_lock(&_state.job_lists[priority].mutex) {
if job := _state.job_lists[priority].head; job != nil {
if _thread_state.index in job.ignored_threads {
sync.atomic_mutex_unlock(&_state.job_lists[priority].mutex)
continue
}
_state.job_lists[priority].head = job._next
sync.atomic_mutex_unlock(&_state.job_lists[priority].mutex)
assert(job.group != nil)
assert(job.procedure != nil)
job.procedure(job.arg)
intrinsics.atomic_sub(&job.group.atomic_counter, 1)
result = true
break block
}
sync.atomic_mutex_unlock(&_state.job_lists[priority].mutex)
}
}
return
}
// Spawns all threads.
initialize :: proc(
num_worker_threads := -1,
thread_proc := default_thread_proc,
thread_arg: rawptr = nil,
allocator := context.allocator,
) {
_state = {
thread_proc = thread_proc,
thread_arg = thread_arg,
thread_counter = 1,
running = true,
allocator = allocator,
}
// Main thread TLS
_thread_state = {
index = 0,
}
// Worker threads
{
// Note: more than 64 threads need special handling on windows.
// TODO
num_hw_threads := min(64, _get_num_hardware_threads())
num_threads := num_worker_threads < 0 ? (num_hw_threads - 1) : num_worker_threads
if num_threads > 0 {
_state.threads = make([]Thread, num_threads, _state.allocator)
for i in 0 ..< num_threads {
thread := _create_worker_thread()
_state.threads[i] = thread
}
}
}
}
// Stop all threads and wait for them to finish.
shutdown :: proc() {
_state.running = false
if len(_state.threads) > 0 {
_wait_for_threads_to_finish(_state.threads[:])
}
delete(_state.threads, _state.allocator)
}