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
|
/*
* Copyright (C) 2022 Mikhail Burakov. This file is part of toolbox.
*
* toolbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* toolbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with toolbox. If not, see <https://www.gnu.org/licenses/>.
*/
#include "thread_pool.h"
#include <stdatomic.h>
#include <stddef.h>
#include <stdlib.h>
#include <threads.h>
struct ThreadPool_Task {
void (*fun)(void*);
void* user;
};
static _Bool FetchTask(struct ThreadPool* thread_pool,
struct ThreadPool_Task* task) {
for (size_t i = 0; i < thread_pool->tasks_count; i++) {
if (thread_pool->tasks[i].fun) {
*task = thread_pool->tasks[i];
thread_pool->tasks[i].fun = NULL;
return 1;
}
}
return 0;
}
static _Bool StoreTask(struct ThreadPool* thread_pool,
const struct ThreadPool_Task* task) {
for (size_t i = 0; i < thread_pool->tasks_count; i++) {
if (!thread_pool->tasks[i].fun) {
thread_pool->tasks[i] = *task;
return 1;
}
}
size_t tasks_count = thread_pool->tasks_count + 1;
struct ThreadPool_Task* tasks =
realloc(thread_pool->tasks, tasks_count * sizeof(struct ThreadPool_Task));
if (!tasks) return 0;
tasks[thread_pool->tasks_count] = *task;
thread_pool->tasks = tasks;
thread_pool->tasks_count = tasks_count;
return 1;
}
static int ThreadProc(void* arg) {
for (struct ThreadPool* thread_pool = arg;
atomic_load_explicit(&thread_pool->running, memory_order_relaxed);) {
if (mtx_lock(&thread_pool->tasks_mutex) != thrd_success) {
// TODO(mburakov): Could we do something other than just reattempt?
thrd_yield();
continue;
}
for (;;) {
if (!atomic_load_explicit(&thread_pool->running, memory_order_relaxed)) {
mtx_unlock(&thread_pool->tasks_mutex);
return 0;
}
struct ThreadPool_Task task;
if (FetchTask(thread_pool, &task)) {
mtx_unlock(&thread_pool->tasks_mutex);
task.fun(task.user);
break;
}
cnd_wait(&thread_pool->tasks_cond, &thread_pool->tasks_mutex);
}
}
return 0;
}
int ThreadPool_Create(struct ThreadPool* thread_pool, size_t threads_count) {
atomic_init(&thread_pool->running, 1);
thread_pool->threads = malloc(threads_count * sizeof(thrd_t));
if (!thread_pool->threads) return -1;
thread_pool->threads_count = 0;
if (cnd_init(&thread_pool->tasks_cond) != thrd_success) goto rollback_threads;
if (mtx_init(&thread_pool->tasks_mutex, mtx_plain) != thrd_success)
goto rollback_tasks_cond;
thread_pool->tasks = NULL;
thread_pool->tasks_count = 0;
for (; thread_pool->threads_count < threads_count;
thread_pool->threads_count++) {
thrd_t* thread = &thread_pool->threads[thread_pool->threads_count];
if (thrd_create(thread, ThreadProc, thread_pool) != thrd_success)
goto rollback_running;
}
return 0;
rollback_running:
atomic_store_explicit(&thread_pool->running, 0, memory_order_relaxed);
cnd_broadcast(&thread_pool->tasks_cond);
while (thread_pool->threads_count-- > 0)
thrd_join(thread_pool->threads[thread_pool->threads_count], NULL);
mtx_destroy(&thread_pool->tasks_mutex);
rollback_tasks_cond:
cnd_destroy(&thread_pool->tasks_cond);
rollback_threads:
free(thread_pool->threads);
return -1;
}
int ThreadPool_Schedule(struct ThreadPool* thread_pool, void (*fun)(void*),
void* user) {
if (mtx_lock(&thread_pool->tasks_mutex) != thrd_success) return -1;
struct ThreadPool_Task task = {.fun = fun, .user = user};
_Bool result = StoreTask(thread_pool, &task);
if (result) cnd_broadcast(&thread_pool->tasks_cond);
mtx_unlock(&thread_pool->tasks_mutex);
return result ? 0 : -1;
}
void ThreadPool_Destroy(struct ThreadPool* thread_pool) {
atomic_store_explicit(&thread_pool->running, 0, memory_order_relaxed);
cnd_broadcast(&thread_pool->tasks_cond);
while (thread_pool->threads_count-- > 0)
thrd_join(thread_pool->threads[thread_pool->threads_count], NULL);
mtx_destroy(&thread_pool->tasks_mutex);
cnd_destroy(&thread_pool->tasks_cond);
free(thread_pool->threads);
}
|