forked from ambrop72/badvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BThreadWork.c
444 lines (355 loc) · 12.5 KB
/
BThreadWork.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* @file BThreadWork.c
* @author Ambroz Bizjak <[email protected]>
*
* @section LICENSE
*
* This file is part of BadVPN.
*
* BadVPN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* BadVPN 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdint.h>
#include <stddef.h>
#ifdef BADVPN_THREADWORK_USE_PTHREAD
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#endif
#include <misc/offset.h>
#include <base/BLog.h>
#include <generated/blog_channel_BThreadWork.h>
#include <threadwork/BThreadWork.h>
#ifdef BADVPN_THREADWORK_USE_PTHREAD
static void * dispatcher_thread (struct BThreadWorkDispatcher_thread *t)
{
BThreadWorkDispatcher *o = t->d;
ASSERT_FORCE(pthread_mutex_lock(&o->mutex) == 0)
while (1) {
// exit if requested
if (o->cancel) {
break;
}
if (LinkedList2_IsEmpty(&o->pending_list)) {
// wait for event
ASSERT_FORCE(pthread_cond_wait(&t->new_cond, &o->mutex) == 0)
continue;
}
// grab the work
BThreadWork *w = UPPER_OBJECT(LinkedList2_GetFirst(&o->pending_list), BThreadWork, list_node);
ASSERT(w->state == BTHREADWORK_STATE_PENDING)
LinkedList2_Remove(&o->pending_list, &w->list_node);
t->running_work = w;
w->state = BTHREADWORK_STATE_RUNNING;
// do the work
ASSERT_FORCE(pthread_mutex_unlock(&o->mutex) == 0)
w->work_func(w->work_func_user);
ASSERT_FORCE(pthread_mutex_lock(&o->mutex) == 0)
// release the work
t->running_work = NULL;
LinkedList2_Append(&o->finished_list, &w->list_node);
w->state = BTHREADWORK_STATE_FINISHED;
ASSERT_FORCE(sem_post(&w->finished_sem) == 0)
// write to pipe
uint8_t b = 0;
int res = write(o->pipe[1], &b, sizeof(b));
if (res < 0) {
int error = errno;
ASSERT_FORCE(error == EAGAIN || error == EWOULDBLOCK)
}
}
ASSERT_FORCE(pthread_mutex_unlock(&o->mutex) == 0)
return NULL;
}
static void dispatch_job (BThreadWorkDispatcher *o)
{
ASSERT(o->num_threads > 0)
// lock
ASSERT_FORCE(pthread_mutex_lock(&o->mutex) == 0)
// check for finished job
if (LinkedList2_IsEmpty(&o->finished_list)) {
ASSERT_FORCE(pthread_mutex_unlock(&o->mutex) == 0)
return;
}
// grab finished job
BThreadWork *w = UPPER_OBJECT(LinkedList2_GetFirst(&o->finished_list), BThreadWork, list_node);
ASSERT(w->state == BTHREADWORK_STATE_FINISHED)
LinkedList2_Remove(&o->finished_list, &w->list_node);
// schedule more
if (!LinkedList2_IsEmpty(&o->finished_list)) {
BPending_Set(&o->more_job);
}
// set state forgotten
w->state = BTHREADWORK_STATE_FORGOTTEN;
// unlock
ASSERT_FORCE(pthread_mutex_unlock(&o->mutex) == 0)
// call handler
w->handler_done(w->user);
return;
}
static void pipe_fd_handler (BThreadWorkDispatcher *o, int events)
{
ASSERT(o->num_threads > 0)
DebugObject_Access(&o->d_obj);
// read data from pipe
uint8_t b[64];
int res = read(o->pipe[0], b, sizeof(b));
if (res < 0) {
int error = errno;
ASSERT_FORCE(error == EAGAIN || error == EWOULDBLOCK)
} else {
ASSERT(res > 0)
}
dispatch_job(o);
return;
}
static void more_job_handler (BThreadWorkDispatcher *o)
{
ASSERT(o->num_threads > 0)
DebugObject_Access(&o->d_obj);
dispatch_job(o);
return;
}
static void stop_threads (BThreadWorkDispatcher *o)
{
// set cancelling
ASSERT_FORCE(pthread_mutex_lock(&o->mutex) == 0)
o->cancel = 1;
ASSERT_FORCE(pthread_mutex_unlock(&o->mutex) == 0)
while (o->num_threads > 0) {
struct BThreadWorkDispatcher_thread *t = &o->threads[o->num_threads - 1];
// wake up thread
ASSERT_FORCE(pthread_cond_signal(&t->new_cond) == 0)
// wait for thread to exit
ASSERT_FORCE(pthread_join(t->thread, NULL) == 0)
// free condition variable
ASSERT_FORCE(pthread_cond_destroy(&t->new_cond) == 0)
o->num_threads--;
}
}
#endif
static void work_job_handler (BThreadWork *o)
{
#ifdef BADVPN_THREADWORK_USE_PTHREAD
ASSERT(o->d->num_threads == 0)
#endif
DebugObject_Access(&o->d_obj);
// do the work
o->work_func(o->work_func_user);
// call handler
o->handler_done(o->user);
return;
}
int BThreadWorkDispatcher_Init (BThreadWorkDispatcher *o, BReactor *reactor, int num_threads_hint)
{
// init arguments
o->reactor = reactor;
if (num_threads_hint < 0) {
num_threads_hint = 2;
}
if (num_threads_hint > BTHREADWORK_MAX_THREADS) {
num_threads_hint = BTHREADWORK_MAX_THREADS;
}
#ifdef BADVPN_THREADWORK_USE_PTHREAD
if (num_threads_hint > 0) {
// init pending list
LinkedList2_Init(&o->pending_list);
// init finished list
LinkedList2_Init(&o->finished_list);
// init mutex
if (pthread_mutex_init(&o->mutex, NULL) != 0) {
BLog(BLOG_ERROR, "pthread_mutex_init failed");
goto fail0;
}
// init pipe
if (pipe(o->pipe) < 0) {
BLog(BLOG_ERROR, "pipe failed");
goto fail1;
}
// set read end non-blocking
if (fcntl(o->pipe[0], F_SETFL, O_NONBLOCK) < 0) {
BLog(BLOG_ERROR, "fcntl failed");
goto fail2;
}
// set write end non-blocking
if (fcntl(o->pipe[1], F_SETFL, O_NONBLOCK) < 0) {
BLog(BLOG_ERROR, "fcntl failed");
goto fail2;
}
// init BFileDescriptor
BFileDescriptor_Init(&o->bfd, o->pipe[0], (BFileDescriptor_handler)pipe_fd_handler, o);
if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed");
goto fail2;
}
BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
// init more job
BPending_Init(&o->more_job, BReactor_PendingGroup(o->reactor), (BPending_handler)more_job_handler, o);
// set not cancelling
o->cancel = 0;
// init threads
o->num_threads = 0;
for (int i = 0; i < num_threads_hint; i++) {
struct BThreadWorkDispatcher_thread *t = &o->threads[i];
// set parent pointer
t->d = o;
// set no running work
t->running_work = NULL;
// init condition variable
if (pthread_cond_init(&t->new_cond, NULL) != 0) {
BLog(BLOG_ERROR, "pthread_cond_init failed");
goto fail3;
}
// init thread
if (pthread_create(&t->thread, NULL, (void * (*) (void *))dispatcher_thread, t) != 0) {
BLog(BLOG_ERROR, "pthread_create failed");
ASSERT_FORCE(pthread_cond_destroy(&t->new_cond) == 0)
goto fail3;
}
o->num_threads++;
}
}
#endif
DebugObject_Init(&o->d_obj);
DebugCounter_Init(&o->d_ctr);
return 1;
#ifdef BADVPN_THREADWORK_USE_PTHREAD
fail3:
stop_threads(o);
BPending_Free(&o->more_job);
BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
fail2:
ASSERT_FORCE(close(o->pipe[0]) == 0)
ASSERT_FORCE(close(o->pipe[1]) == 0)
fail1:
ASSERT_FORCE(pthread_mutex_destroy(&o->mutex) == 0)
fail0:
return 0;
#endif
}
void BThreadWorkDispatcher_Free (BThreadWorkDispatcher *o)
{
#ifdef BADVPN_THREADWORK_USE_PTHREAD
if (o->num_threads > 0) {
ASSERT(LinkedList2_IsEmpty(&o->pending_list))
for (int i = 0; i < o->num_threads; i++) { ASSERT(!o->threads[i].running_work) }
ASSERT(LinkedList2_IsEmpty(&o->finished_list))
}
#endif
DebugObject_Free(&o->d_obj);
DebugCounter_Free(&o->d_ctr);
#ifdef BADVPN_THREADWORK_USE_PTHREAD
if (o->num_threads > 0) {
// stop threads
stop_threads(o);
// free more job
BPending_Free(&o->more_job);
// free BFileDescriptor
BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
// free pipe
ASSERT_FORCE(close(o->pipe[0]) == 0)
ASSERT_FORCE(close(o->pipe[1]) == 0)
// free mutex
ASSERT_FORCE(pthread_mutex_destroy(&o->mutex) == 0)
}
#endif
}
int BThreadWorkDispatcher_UsingThreads (BThreadWorkDispatcher *o)
{
#ifdef BADVPN_THREADWORK_USE_PTHREAD
return (o->num_threads > 0);
#else
return 0;
#endif
}
void BThreadWork_Init (BThreadWork *o, BThreadWorkDispatcher *d, BThreadWork_handler_done handler_done, void *user, BThreadWork_work_func work_func, void *work_func_user)
{
DebugObject_Access(&d->d_obj);
// init arguments
o->d = d;
o->handler_done = handler_done;
o->user = user;
o->work_func = work_func;
o->work_func_user = work_func_user;
#ifdef BADVPN_THREADWORK_USE_PTHREAD
if (d->num_threads > 0) {
// set state
o->state = BTHREADWORK_STATE_PENDING;
// init finished semaphore
ASSERT_FORCE(sem_init(&o->finished_sem, 0, 0) == 0)
// post work
ASSERT_FORCE(pthread_mutex_lock(&d->mutex) == 0)
LinkedList2_Append(&d->pending_list, &o->list_node);
for (int i = 0; i < d->num_threads; i++) {
if (!d->threads[i].running_work) {
ASSERT_FORCE(pthread_cond_signal(&d->threads[i].new_cond) == 0)
break;
}
}
ASSERT_FORCE(pthread_mutex_unlock(&d->mutex) == 0)
} else {
#endif
// schedule job
BPending_Init(&o->job, BReactor_PendingGroup(d->reactor), (BPending_handler)work_job_handler, o);
BPending_Set(&o->job);
#ifdef BADVPN_THREADWORK_USE_PTHREAD
}
#endif
DebugObject_Init(&o->d_obj);
DebugCounter_Increment(&d->d_ctr);
}
void BThreadWork_Free (BThreadWork *o)
{
BThreadWorkDispatcher *d = o->d;
DebugObject_Free(&o->d_obj);
DebugCounter_Decrement(&d->d_ctr);
#ifdef BADVPN_THREADWORK_USE_PTHREAD
if (d->num_threads > 0) {
ASSERT_FORCE(pthread_mutex_lock(&d->mutex) == 0)
switch (o->state) {
case BTHREADWORK_STATE_PENDING: {
BLog(BLOG_DEBUG, "remove pending work");
// remove from pending list
LinkedList2_Remove(&d->pending_list, &o->list_node);
} break;
case BTHREADWORK_STATE_RUNNING: {
BLog(BLOG_DEBUG, "remove running work");
// wait for the work to finish running
ASSERT_FORCE(pthread_mutex_unlock(&d->mutex) == 0)
ASSERT_FORCE(sem_wait(&o->finished_sem) == 0)
ASSERT_FORCE(pthread_mutex_lock(&d->mutex) == 0)
ASSERT(o->state == BTHREADWORK_STATE_FINISHED)
// remove from finished list
LinkedList2_Remove(&d->finished_list, &o->list_node);
} break;
case BTHREADWORK_STATE_FINISHED: {
BLog(BLOG_DEBUG, "remove finished work");
// remove from finished list
LinkedList2_Remove(&d->finished_list, &o->list_node);
} break;
case BTHREADWORK_STATE_FORGOTTEN: {
BLog(BLOG_DEBUG, "remove forgotten work");
} break;
default:
ASSERT(0);
}
ASSERT_FORCE(pthread_mutex_unlock(&d->mutex) == 0)
// free finished semaphore
ASSERT_FORCE(sem_destroy(&o->finished_sem) == 0)
} else {
#endif
BPending_Free(&o->job);
#ifdef BADVPN_THREADWORK_USE_PTHREAD
}
#endif
}