-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththread-pool.c
More file actions
252 lines (196 loc) · 4.98 KB
/
thread-pool.c
File metadata and controls
252 lines (196 loc) · 4.98 KB
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
/*
* Copyright (C) 2015, CompuLab ltd.
* Author: Andrey Gelman <andrey.gelman@compulab.co.il>
* License: GNU GPLv2 or later, at your option
*/
#include <stdbool.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "thread-pool.h"
#include "watchdog.h"
#include "common.h"
typedef struct {
ThreadPoolWork func;
void *context;
WDeadline *deadline;
} ThreadPoolRequest;
static Watchdog *thread_pool_watchdog = NULL;
static int watchdog_refcount = 0;
/*
* thread_pool_watchdog singleton object
*
* thread_pool_watchdog_{init | cleanup} functions
* are assumed to run sequentially, thus not mutual
* exclusion required.
*/
static void thread_pool_watchdog_init(void)
{
if (watchdog_refcount == 0)
thread_pool_watchdog = watchdog_create();
++watchdog_refcount;
}
static void thread_pool_watchdog_cleanup(void)
{
if (watchdog_refcount <= 0)
return;
if (--watchdog_refcount == 0) {
watchdog_destroy(thread_pool_watchdog);
thread_pool_watchdog = NULL;
}
}
static void thread_pool_runner_cleanup(void *arg)
{
pthread_mutex_t *lock = (pthread_mutex_t *)arg;
pthread_mutex_unlock(lock);
}
static void *thread_pool_runner(void *arg)
{
ThreadPool *p = (ThreadPool *)arg;
ThreadPoolRequest req;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
while ( 1 ) {
pthread_mutex_lock(&p->lock);
/* cleanup routine to be run upon thread cancellation */
pthread_cleanup_push(thread_pool_runner_cleanup, &p->lock);
while (queue_is_empty(p->work_queue)) {
pthread_cond_wait(&p->queue_not_empty, &p->lock);
}
queue_pop_front(p->work_queue, &req);
pthread_cond_signal(&p->queue_not_full);
/* execute cleanup routine */
pthread_cleanup_pop(true);
req.func(req.context, p->shared_context);
if (req.deadline != NULL)
watchdog_clear_deadline(thread_pool_watchdog, req.deadline);
}
return NULL;
}
ThreadPool *thread_pool_create(int thread_count, int queue_size, void *shared_context)
{
ThreadPool *p;
int i;
int err;
p = (ThreadPool *)malloc(sizeof(ThreadPool) + (sizeof(pthread_t) * thread_count));
if ( !p ) {
sloge("thread-pool: could not allocate memory");
return NULL;
}
pthread_mutex_init(&p->lock, NULL);
pthread_cond_init(&p->queue_not_empty, NULL);
pthread_cond_init(&p->queue_not_full, NULL);
p->work_queue = queue_create(sizeof(ThreadPoolRequest), queue_size);
p->shared_context = shared_context;
/*
* As the threads are born live,
* they should be started when all thread pool data fields are well initialized.
*/
p->thread_count = 0;
for (i = 0; i < thread_count; ++i) {
err = pthread_create(&p->thread_pool[i], NULL, thread_pool_runner, p);
if ( err ) {
sloge("thread-pool: could not spawn a thread: %d", err);
break;
}
p->thread_count++;
}
thread_pool_watchdog_init();
return p;
}
void thread_pool_join(ThreadPool *p)
{
int i;
for (i = 0; i < p->thread_count; ++i) {
pthread_cancel(p->thread_pool[i]);
}
for (i = 0; i < p->thread_count; ++i) {
pthread_join(p->thread_pool[i], NULL);
}
p->thread_count = 0;
}
void thread_pool_destroy(ThreadPool *p)
{
thread_pool_join(p);
pthread_mutex_destroy(&p->lock);
pthread_cond_destroy(&p->queue_not_empty);
pthread_cond_destroy(&p->queue_not_full);
queue_destroy(p->work_queue);
/* zero everything against evil eye */
memset(p, 0, sizeof(ThreadPool));
free(p);
thread_pool_watchdog_cleanup();
}
void thread_pool_add_request(ThreadPool *p, ThreadPoolWork func, void *context)
{
ThreadPoolRequest req = {
.func = func,
.context = context,
.deadline = watchdog_submit_deadline(thread_pool_watchdog, ATFP_WATCHDOG_DEFAULT_DELAY),
};
pthread_mutex_lock(&p->lock);
while (queue_is_full(p->work_queue)) {
pthread_cond_wait(&p->queue_not_full, &p->lock);
}
queue_push_back(p->work_queue, &req);
pthread_cond_signal(&p->queue_not_empty);
pthread_mutex_unlock(&p->lock);
}
/* unittest */
int thread_pool_test(void)
{
typedef struct {
double x;
double y;
double ans;
double ref;
void (*func)(void *, void *);
} Test;
const int test_length = 50000;
Test t[test_length];
void func_plus(void *a, void *b)
{
Test *t = (Test *)a;
usleep(1);
t->ans = t->x + t->y;
}
void func_minus(void *a, void *b)
{
Test *t = (Test *)a;
usleep(2);
t->ans = t->x - t->y;
}
ThreadPool *tp;
int i;
int err = 0;
tp = thread_pool_create(4, 10, NULL);
srand(0);
for (i = 0; i < test_length; ++i) {
t[i].x = (double)rand() * 100.0 / (double)RAND_MAX;
t[i].y = (double)rand() * 100.0 / (double)RAND_MAX;
t[i].ans = 0.0;
if (i % 2 == 0) {
t[i].ref = t[i].x + t[i].y;
t[i].func = func_plus;
}
else {
t[i].ref = t[i].x - t[i].y;
t[i].func = func_minus;
}
}
for (i = 0; i < test_length; ++i) {
thread_pool_add_request(tp, t[i].func, &t[i]);
}
/* no way to wait for completion */
for (i = 0; i < test_length; ++i) {
if (t[i].ans != t[i].ref) {
err = -i;
goto test_out;
}
}
test_out:
thread_pool_destroy(tp);
return err;
}