aboutsummaryrefslogtreecommitdiff
path: root/core/thread/thread_pool.odin
blob: 37ee4fa981ad71b5975e71c4b1560b4edb1e4f37 (plain)
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
package thread

import "core:intrinsics"
import "core:sync"
import "core:mem"

Task_Status :: enum i32 {
	Ready,
	Busy,
	Waiting,
	Term,
}

Task_Proc :: #type proc(task: ^Task)

Task :: struct {
	procedure: Task_Proc,
	data: rawptr,
	user_index: int,
}

Task_Id :: distinct i32
INVALID_TASK_ID :: Task_Id(-1)


Pool :: struct {
	allocator:             mem.Allocator,
	mutex:                 sync.Mutex,
	sem_available:         sync.Semaphore,
	processing_task_count: int, // atomic
	is_running:            bool,

	threads: []^Thread,

	tasks: [dynamic]Task,
}

pool_init :: proc(pool: ^Pool, thread_count: int, allocator := context.allocator) {
	worker_thread_internal :: proc(t: ^Thread) {
		pool := (^Pool)(t.data)

		for pool.is_running {
			sync.semaphore_wait_for(&pool.sem_available)

			if task, ok := pool_try_and_pop_task(pool); ok {
				pool_do_work(pool, &task)
			}
		}

		sync.semaphore_post(&pool.sem_available, 1)
	}


	context.allocator = allocator
	pool.allocator = allocator
	pool.tasks = make([dynamic]Task)
	pool.threads = make([]^Thread, thread_count)

	sync.mutex_init(&pool.mutex)
	sync.semaphore_init(&pool.sem_available)
	pool.is_running = true

	for _, i in pool.threads {
		t := create(worker_thread_internal)
		t.user_index = i
		t.data = pool
		pool.threads[i] = t
	}
}

pool_destroy :: proc(pool: ^Pool) {
	delete(pool.tasks)

	for thread in &pool.threads {
		destroy(thread)
	}

	delete(pool.threads, pool.allocator)

	sync.mutex_destroy(&pool.mutex)
	sync.semaphore_destroy(&pool.sem_available)
}

pool_start :: proc(pool: ^Pool) {
	for t in pool.threads {
		start(t)
	}
}

pool_join :: proc(pool: ^Pool) {
	pool.is_running = false

	sync.semaphore_post(&pool.sem_available, len(pool.threads))

	yield()

	for t in pool.threads {
		join(t)
	}
}

pool_add_task :: proc(pool: ^Pool, procedure: Task_Proc, data: rawptr, user_index: int = 0) {
	sync.mutex_lock(&pool.mutex)
	defer sync.mutex_unlock(&pool.mutex)

	task: Task
	task.procedure = procedure
	task.data = data
	task.user_index = user_index

	append(&pool.tasks, task)
	sync.semaphore_post(&pool.sem_available, 1)
}

pool_try_and_pop_task :: proc(pool: ^Pool) -> (task: Task, got_task: bool = false) {
	if sync.mutex_try_lock(&pool.mutex) {
		if len(pool.tasks) != 0 {
			intrinsics.atomic_add(&pool.processing_task_count, 1)
			task = pop_front(&pool.tasks)
			got_task = true
		}
		sync.mutex_unlock(&pool.mutex)
	}
	return
}


pool_do_work :: proc(pool: ^Pool, task: ^Task) {
	task.procedure(task)
	intrinsics.atomic_sub(&pool.processing_task_count, 1)
}


pool_wait_and_process :: proc(pool: ^Pool) {
	for len(pool.tasks) != 0 || intrinsics.atomic_load(&pool.processing_task_count) != 0 {
		if task, ok := pool_try_and_pop_task(pool); ok {
			pool_do_work(pool, &task)
		}

		// Safety kick
		if len(pool.tasks) != 0 && intrinsics.atomic_load(&pool.processing_task_count) == 0 {
			sync.mutex_lock(&pool.mutex)
			sync.semaphore_post(&pool.sem_available, len(pool.tasks))
			sync.mutex_unlock(&pool.mutex)
		}

		yield()
	}

	pool_join(pool)
}