-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththreadpool.c
267 lines (221 loc) · 6.29 KB
/
threadpool.c
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
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "threadpool.h"
#include "lib.h"
typedef enum {
immediate_shutdown = 1,
graceful_shutdown = 2
} threadpool_shutdown_t;
/**
* @struct threadpool_task
* @brief the work struct
*
* @var function Pointer to the function that will perform the task.
* @var argument Argument to be passed to the function.
*/
typedef struct {
void (*function)(void *);
void *argument;
} threadpool_task_t;
/**
* @brief The threadpool struct
*
* @var notify Condition variable to notify worker threads.
* @var threads Array containing worker threads ID.
* @var thread_count Number of threads
* @var queue Array containing the task queue.
* @var queue_size Size of the task queue.
* @var head Index of the first element.
* @var tail Index of the next element.
* @var count Number of pending tasks
* @var shutdown Flag indicating if the pool is shutting down
* @var started Number of started threads
*/
struct threadpool_s {
pthread_mutex_t lock;
pthread_cond_t notify;
pthread_t *threads;
threadpool_task_t *queue;
int thread_count;
int queue_size;
int head;
int tail;
int count;
int shutdown;
int started;
};
/**
* @function void *threadpool_thread(void *threadpool)
* @brief the worker thread
* @param threadpool the pool which own the thread
*/
static void *threadpool_thread(void *threadpool);
int threadpool_free(threadpool_t pool);
threadpool_t threadpool_create(int thread_count, int queue_size, int flags) {
threadpool_t pool;
int i;
(void)flags;
if (thread_count <= 0 || thread_count > MAX_THREADS || queue_size <= 0 ||
queue_size > MAX_QUEUE) {
return NULL;
}
if ((pool = (struct threadpool_s *)malloc(sizeof(struct threadpool_s))) ==
NULL) {
goto err;
}
// initialize
pool->thread_count = 0;
pool->queue_size = queue_size;
pool->head = pool->tail = pool->count = 0;
pool->shutdown = pool->started = 0;
// allocate thread and task queue
pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * thread_count);
pool->queue =
(threadpool_task_t *)malloc(sizeof(threadpool_task_t) * queue_size);
// initialize mutex and conditional variable first
if ((pthread_mutex_init(&(pool->lock), NULL) != 0) ||
(pthread_cond_init(&(pool->notify), NULL) != 0) ||
(pool->threads == NULL) || (pool->queue == NULL)) {
goto err;
}
// start worker threads
for (i = 0; i < thread_count; i++) {
if (pthread_create(&(pool->threads[i]), NULL, threadpool_thread,
(void *)pool) != 0) {
threadpool_destroy(pool, 0);
return NULL;
}
pool->thread_count++;
pool->started++;
}
return pool;
err:
if (pool) {
threadpool_free(pool);
}
return NULL;
}
int threadpool_add(threadpool_t pool, void (*function)(void *), void *argument,
int flags) {
int err = 0;
int next;
(void)flags;
if (pool == NULL || function == NULL) {
return threadpool_invalid;
}
if (pthread_mutex_lock(&(pool->lock)) != 0) {
return threadpool_lock_failure;
}
next = (pool->tail + 1) % pool->queue_size;
do {
// are we full ?
if (pool->count == pool->queue_size) {
err = threadpool_queue_full;
break;
}
// are we shutting down ?
if (pool->shutdown) {
err = threadpool_shutdown;
break;
}
// add task to queue
pool->queue[pool->tail].function = function;
pool->queue[pool->tail].argument = argument;
pool->tail = next;
pool->count += 1;
// pthread_cond_broadcast
if (pthread_cond_signal(&(pool->notify)) != 0) {
err = threadpool_lock_failure;
break;
}
} while (0);
if (pthread_mutex_unlock(&pool->lock) != 0) {
err = threadpool_lock_failure;
}
return err;
}
int threadpool_destroy(threadpool_t pool, int flags) {
int i, err = 0;
if (pool == NULL) {
return threadpool_invalid;
}
if (pthread_mutex_lock(&(pool->lock)) != 0) {
return threadpool_lock_failure;
}
do {
// already shutting down
if (pool->shutdown) {
err = threadpool_shutdown;
break;
}
pool->shutdown =
(flags & threadpool_graceful) ? graceful_shutdown : immediate_shutdown;
// wake up all worker threads
if ((pthread_cond_broadcast(&(pool->notify)) != 0) ||
(pthread_mutex_unlock(&(pool->lock)) != 0)) {
err = threadpool_lock_failure;
break;
}
// join all worker thread
for (i = 0; i < pool->thread_count; i++) {
if (pthread_join(pool->threads[i], NULL) != 0) {
err = threadpool_thread_failure;
}
}
} while (0);
// only if everything went well do we deallocate the pool
if (!err) {
threadpool_free(pool);
}
return err;
}
int threadpool_free(threadpool_t pool) {
if (pool == NULL || pool->started > 0) {
return -1;
}
// did we manage to allocate ?
if (pool->threads) {
free(pool->threads);
free(pool->queue);
// because we allocate pool->threads after initializing the
// mutex and condition variable, we're sure they're initialized
// let's lock the mutex just in case
pthread_mutex_lock(&(pool->lock));
pthread_mutex_destroy(&(pool->lock));
pthread_cond_destroy(&(pool->notify));
}
free(pool);
return 0;
}
static void *threadpool_thread(void *threadpool) {
threadpool_t pool = (threadpool_t)threadpool;
threadpool_task_t task;
for (;;) {
// lock must be taken to wait on conditional variable
pthread_mutex_lock(&(pool->lock));
// wait on condition variable, check for spurious wakeups
// when returning from pthread_cond_wait(), we own the lock
while ((pool->count == 0) && (!pool->shutdown)) {
pthread_cond_wait(&(pool->notify), &(pool->lock));
}
if ((pool->shutdown == immediate_shutdown) ||
((pool->shutdown == graceful_shutdown) && (pool->count == 0))) {
break;
}
// grab our task
task.function = pool->queue[pool->head].function;
task.argument = pool->queue[pool->head].argument;
pool->head = (pool->head + 1) % pool->queue_size;
pool->count -= 1;
// unlock
pthread_mutex_unlock(&(pool->lock));
// get to work
(*(task.function))(task.argument);
}
pool->started--;
pthread_mutex_unlock(&(pool->lock));
pthread_exit(NULL);
return (NULL);
}