-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackground.odin
72 lines (56 loc) · 1.7 KB
/
background.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
package jobs_example_background
import common ".."
import jobs "../.."
import "core:fmt"
frame: int
counter: int
main :: proc() {
jobs.initialize(num_worker_threads = 1)
common._profiler_init()
g: jobs.Group
// For this to work without stalling the main thread we need more than 1 thread.
assert(jobs.num_threads() > 1)
// Dispatch long-lived job on any thread other than the main thread.
// Warning: you are responsible for freeing the memory.
// That's why all short-lived jobs should just use context.temp_allocator
bg_jobs := jobs.dispatch(
.Medium,
jobs.make_job(&g, background_job, ignored_threads = jobs.Ignored_Threads{jobs.MAIN_THREAD_INDEX}),
allocator = context.allocator,
)
for !jobs.group_is_finished(&g) {
free_all(context.temp_allocator)
fmt.printf("Frame %i\n", frame)
frame += 1
short_g: jobs.Group
for i in 0 ..< 5 {
jobs.dispatch(.Low, jobs.make_job(&short_g, proc(_: rawptr) {
fmt.println(" short job")
}))
}
jobs.wait(&short_g)
}
fmt.println("Background job finished!")
fmt.println("Counter:", counter)
fmt.println("Frame:", frame)
delete(bg_jobs)
common._profiler_shutdown()
jobs.shutdown()
}
background_job :: proc(_: rawptr) {
for frame < 100 {
fmt.println("waiting for frame 100")
}
g: jobs.Group
for i in 0 ..< 5 {
jobs.dispatch(.High, jobs.make_job(&g, proc(_: rawptr) {
fmt.println(" background child job")
}))
}
N :: 1_000_000
fmt.println("Count to", N)
for i in 0 ..< N {
counter += 1
}
jobs.wait(&g)
}