forked from baresip/re
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.c
367 lines (291 loc) · 6.27 KB
/
async.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
/**
* @file async.c Async API
*
* Copyright (C) 2022 Sebastian Reimers
*/
#include <re_types.h>
#include <re_mem.h>
#include <re_list.h>
#include <re_thread.h>
#include <re_async.h>
#include <re_tmr.h>
#include <re_mqueue.h>
#define DEBUG_MODULE "async"
#define DEBUG_LEVEL 5
#include <re_dbg.h>
struct async_work {
struct le le;
mtx_t *mtx;
re_async_work_h *workh;
re_async_h *cb;
void *arg;
int err;
intptr_t id;
};
struct re_async {
thrd_t *thrd;
uint16_t workers;
volatile bool run;
cnd_t wait;
mtx_t mtx;
struct list freel;
struct list workl;
struct list curl;
struct tmr tmr;
struct mqueue *mqueue;
};
static int worker_thread(void *arg)
{
struct re_async *a = arg;
struct le *le;
struct async_work *work;
for (;;) {
mtx_lock(&a->mtx);
if (!a->run) {
mtx_unlock(&a->mtx);
break;
}
if (list_isempty(&a->workl)) {
cnd_wait(&a->wait, &a->mtx);
if (list_isempty(&a->workl) || !a->run) {
mtx_unlock(&a->mtx);
continue;
}
}
le = list_head(&a->workl);
list_move(le, &a->curl);
mtx_unlock(&a->mtx);
work = le->data;
mtx_lock(work->mtx);
if (work->workh) {
work->err = work->workh(work->arg);
work->workh = NULL;
}
mtx_unlock(work->mtx);
mtx_lock(&a->mtx);
mqueue_push(a->mqueue, 0, work);
mtx_unlock(&a->mtx);
}
return 0;
}
static void async_destructor(void *data)
{
struct re_async *async = data;
tmr_cancel(&async->tmr);
mtx_lock(&async->mtx);
async->run = false;
cnd_broadcast(&async->wait);
mtx_unlock(&async->mtx);
for (int i = 0; i < async->workers; i++) {
thrd_join(async->thrd[i], NULL);
}
/* Notify worker callbacks (so they can call destructors) */
struct le *le;
LIST_FOREACH(&async->workl, le)
{
struct async_work *work = le->data;
if (work->cb) {
work->cb(ECANCELED, work->arg);
work->cb = NULL;
}
}
LIST_FOREACH(&async->curl, le)
{
struct async_work *work = le->data;
if (work->cb) {
work->cb(ECANCELED, work->arg);
work->cb = NULL;
}
}
list_flush(&async->workl);
list_flush(&async->curl);
list_flush(&async->freel);
cnd_destroy(&async->wait);
mtx_destroy(&async->mtx);
mem_deref(async->mqueue);
mem_deref(async->thrd);
}
static void worker_check(void *arg)
{
struct re_async *async = arg;
mtx_lock(&async->mtx);
if (!list_isempty(&async->workl)) {
if (async->workers == list_count(&async->curl))
DEBUG_WARNING("all async workers are busy\n");
else
cnd_broadcast(&async->wait);
}
mtx_unlock(&async->mtx);
tmr_start(&async->tmr, 100, worker_check, async);
}
/* called by re main event loop */
static void queueh(int id, void *data, void *arg)
{
struct async_work *work = data;
struct re_async *async = arg;
(void)id;
mtx_lock(work->mtx);
if (work->cb) {
work->cb(work->err, work->arg);
work->cb =NULL;
}
mtx_unlock(work->mtx);
mtx_lock(&async->mtx);
list_move(&work->le, &async->freel);
mtx_unlock(&async->mtx);
}
static void work_destruct(void *arg)
{
struct async_work *work = arg;
mem_deref(work->mtx);
}
static int work_alloc(struct async_work **workp)
{
int err;
struct async_work *work;
work = mem_zalloc(sizeof(struct async_work), NULL);
if (!work) {
err = ENOMEM;
return err;
}
err = mutex_alloc(&work->mtx);
if (err) {
mem_deref(work);
return err;
}
mem_destructor(work, work_destruct);
*workp = work;
return 0;
}
/**
* Allocate a new async object
*
* @param asyncp Pointer to allocated async object
* @param workers Number of worker threads
*
* @return 0 if success, otherwise errorcode
*/
int re_async_alloc(struct re_async **asyncp, uint16_t workers)
{
int err;
struct re_async *async;
struct async_work *work;
if (!asyncp || !workers)
return EINVAL;
async = mem_zalloc(sizeof(struct re_async), NULL);
if (!async)
return ENOMEM;
err = mqueue_alloc(&async->mqueue, queueh, async);
if (err)
goto err;
async->thrd = mem_zalloc(sizeof(thrd_t) * workers, NULL);
if (!async->thrd) {
err = ENOMEM;
mem_deref(async->mqueue);
goto err;
}
mtx_init(&async->mtx, mtx_plain);
cnd_init(&async->wait);
tmr_init(&async->tmr);
mem_destructor(async, async_destructor);
async->run = true;
for (int i = 0; i < workers; i++) {
err = thread_create_name(&async->thrd[i],
"async worker thread", worker_thread,
async);
if (err)
goto err;
async->workers++;
/* preallocate */
err = work_alloc(&work);
if (err)
goto err;
list_append(&async->freel, &work->le, work);
}
tmr_start(&async->tmr, 10, worker_check, async);
*asyncp = async;
return 0;
err:
mem_deref(async);
return err;
}
/**
* Execute work handler async and get a callback from re main thread
*
* @param async Pointer to async object
* @param id Work identifier
* @param workh Work handler
* @param cb Callback handler (called by re main thread)
* @param arg Handler argument (has to be thread-safe)
*
* @return 0 if success, otherwise errorcode
*/
int re_async(struct re_async *async, intptr_t id, re_async_work_h *workh,
re_async_h *cb, void *arg)
{
int err = 0;
struct async_work *work;
if (unlikely(!async))
return EINVAL;
mtx_lock(&async->mtx);
if (unlikely(list_isempty(&async->freel))) {
err = work_alloc(&work);
if (err)
goto out;
}
else {
work = list_head(&async->freel)->data;
list_unlink(&work->le);
}
work->workh = workh;
work->cb = cb;
work->arg = arg;
work->id = id;
list_append(&async->workl, &work->le, work);
cnd_signal(&async->wait);
out:
mtx_unlock(&async->mtx);
return err;
}
/**
* Cancel pending async work and callback
*
* @param async Pointer to async object
* @param id Work identifier
*/
void re_async_cancel(struct re_async *async, intptr_t id)
{
struct le *le;
if (unlikely(!async))
return;
mtx_lock(&async->mtx);
le = list_head(&async->workl);
while (le) {
struct async_work *w = le->data;
le = le->next;
if (w->id != id)
continue;
mtx_lock(w->mtx);
w->id = 0;
w->workh = NULL;
w->cb = NULL;
w->arg = mem_deref(w->arg);
list_move(&w->le, &async->freel);
mtx_unlock(w->mtx);
}
le = list_head(&async->curl);
while (le) {
struct async_work *w = le->data;
le = le->next;
if (w->id != id)
continue;
mtx_lock(w->mtx);
w->id = 0;
w->workh = NULL;
w->cb = NULL;
w->arg = mem_deref(w->arg);
list_move(&w->le, &async->freel);
mtx_unlock(w->mtx);
}
mtx_unlock(&async->mtx);
}