aboutsummaryrefslogtreecommitdiff
path: root/src/thread_pool.cpp
blob: 1b3e74fbe797d6b2e25a8e4e06db1e437d09a5c2 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// thread_pool.cpp

#define WORKER_TASK_PROC(name) isize name(void *data)
typedef WORKER_TASK_PROC(WorkerTaskProc);

struct WorkerTask {
	WorkerTask *    next;
	WorkerTaskProc *do_work;
	void *          data;
};

struct ThreadPool {
	gbAllocator   allocator;
	BlockingMutex mutex;
	Condition     task_cond;
	
	Slice<Thread> threads;
	
	WorkerTask *task_queue;
	
	std::atomic<isize> ready;
	std::atomic<bool>  stop;
	
};

gb_internal THREAD_PROC(thread_pool_thread_proc);

gb_internal void thread_pool_init(ThreadPool *pool, gbAllocator const &a, isize thread_count, char const *worker_name) {
	pool->allocator = a;
	pool->stop = false;
	mutex_init(&pool->mutex);
	condition_init(&pool->task_cond);
	
	slice_init(&pool->threads, a, thread_count);
	for_array(i, pool->threads) {
		Thread *t = &pool->threads[i];
		thread_init(t);
	}
	
	for_array(i, pool->threads) {
		Thread *t = &pool->threads[i];
		thread_start(t, thread_pool_thread_proc, pool);
	}
}

gb_internal void thread_pool_destroy(ThreadPool *pool) {
	mutex_lock(&pool->mutex);
	pool->stop = true;
	condition_broadcast(&pool->task_cond);
	mutex_unlock(&pool->mutex);

	for_array(i, pool->threads) {
		Thread *t = &pool->threads[i];
		thread_join(t);
	}
	
	for_array(i, pool->threads) {
		Thread *t = &pool->threads[i];
		thread_destroy(t);
	}
	
	gb_free(pool->allocator, pool->threads.data);
	mutex_destroy(&pool->mutex);
	condition_destroy(&pool->task_cond);
}

gb_internal bool thread_pool_queue_empty(ThreadPool *pool) {
	return pool->task_queue == nullptr;
}

gb_internal WorkerTask *thread_pool_queue_pop(ThreadPool *pool) {
	GB_ASSERT(pool->task_queue != nullptr);
	WorkerTask *task = pool->task_queue;
	pool->task_queue = task->next;
	return task;
}
gb_internal void thread_pool_queue_push(ThreadPool *pool, WorkerTask *task) {
	GB_ASSERT(task != nullptr);
	task->next = pool->task_queue;
	pool->task_queue = task;
}

gb_internal bool thread_pool_add_task(ThreadPool *pool, WorkerTaskProc *proc, void *data) {
	GB_ASSERT(proc != nullptr);
	mutex_lock(&pool->mutex);
	WorkerTask *task = gb_alloc_item(permanent_allocator(), WorkerTask);
	if (task == nullptr) {
		mutex_unlock(&pool->mutex);
		GB_PANIC("Out of memory");
		return false;
	}
	task->do_work = proc;
	task->data = data;
		
	thread_pool_queue_push(pool, task);
	GB_ASSERT(pool->ready >= 0);
	pool->ready++;
	condition_broadcast(&pool->task_cond);
	mutex_unlock(&pool->mutex);
	return true;
}	


gb_internal void thread_pool_do_task(WorkerTask *task) {
	task->do_work(task->data);
}

gb_internal void thread_pool_wait(ThreadPool *pool) {
	if (pool->threads.count == 0) {
		while (!thread_pool_queue_empty(pool)) {
			thread_pool_do_task(thread_pool_queue_pop(pool));
			--pool->ready;
		}
		GB_ASSERT(pool->ready == 0);
		return;
	}
	for (;;) {
		mutex_lock(&pool->mutex);

		while (!pool->stop && pool->ready > 0 && thread_pool_queue_empty(pool)) {
			condition_wait(&pool->task_cond, &pool->mutex);
		}
		if ((pool->stop || pool->ready == 0) && thread_pool_queue_empty(pool)) {
			mutex_unlock(&pool->mutex);
			return;
		}

		WorkerTask *task = thread_pool_queue_pop(pool);
		mutex_unlock(&pool->mutex);
	
		thread_pool_do_task(task);
		if (--pool->ready == 0) {
			mutex_lock(&pool->mutex);
			condition_broadcast(&pool->task_cond);
			mutex_unlock(&pool->mutex);
		}
	}
}


gb_internal THREAD_PROC(thread_pool_thread_proc) {
	ThreadPool *pool = cast(ThreadPool *)thread->user_data;
	
	for (;;) {
		mutex_lock(&pool->mutex);

		while (!pool->stop && thread_pool_queue_empty(pool)) {
			condition_wait(&pool->task_cond, &pool->mutex);
		}
		if (pool->stop && thread_pool_queue_empty(pool)) {
			mutex_unlock(&pool->mutex);
			return 0;
		}

		WorkerTask *task = thread_pool_queue_pop(pool);
		mutex_unlock(&pool->mutex);
	
		thread_pool_do_task(task);
		if (--pool->ready == 0) {
			mutex_lock(&pool->mutex);
			condition_broadcast(&pool->task_cond);
			mutex_unlock(&pool->mutex);
		}
	}
}