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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
/**
* @file threadpool.c
* @author syxhe (https://t.me/syxhe)
* @brief An implementation of a threadpool using libc threads
* @version 0.1
* @date 2025-06-09
*
* @copyright Copyright (c) 2025
*
*/
#ifndef __VXGG_REWRITE___THREADPOOL_C___193271180830131___
#define __VXGG_REWRITE___THREADPOOL_C___193271180830131___ 1
#include "shared.c"
#include <threads.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>
/**
* @brief A generic task - A function, data for that function, and a way to free the data
*
*/
typedef struct task {
gcallback callback; //!< A generic callback to be ran when executing the task
fcallback freecb; //!< A free()-like callback to deal with the data
void *data; //!< Some generic data for the generic callback
} task;
/**
* @brief An internal structure used for the `taskqueue`. Analogous to a doubly-linked list's internal node
*
*/
typedef struct tqnode {
struct tqnode *next; //!< The next element in the `taskqueue`
struct tqnode *prev; //!< The previous element in the `taskqueue`
task *task; //!< The current element's `task`
} tqnode;
/**
* @brief A FIFO queue of tasks
*
*/
typedef struct taskqueue {
tqnode *start; //!< The first element of the queue
tqnode *end; //!< The final element of the queue
size_t size; //!< The number of elements in the queue
} taskqueue;
/**
* @brief A `taskqueue` built for concurrent access. Essentially a threadpool
*
*/
typedef struct ctqueue {
mtx_t mutex; //!< A mutex for locking sensitive resources
cnd_t cond; //!< A conditional for waiting on / sending a signal
uint8_t canceled; //!< Whether the threads are currently canceled or not
taskqueue *tq; //!< A taskqueue to be accessed concurrently
thrd_t *thrdarr; //!< An array of threads to be dispatched as consumers
int talen; //!< The length of the thread array
// Consider making these another linked list or stack or something
} ctqueue;
/**
* @brief Create a task
*
* @param callback Callback function the given data should be ran with. Must be non-null
* @param freecb Callback function for freeing the given data. May be null
* @param data Data to be passed to the callback. May be null
* @retval (task*)[NULL, task*] Returns a task object with set parameters. Returns `null` and sets errno on error
*/
task * task_new(gcallback callback, fcallback freecb, void *data) {
if(callback == NULL) ERRRET(EINVAL, NULL);
task *tsk = calloc(1, sizeof(*tsk));
if(!tsk) return NULL;
tsk->callback = callback;
tsk->freecb = freecb;
tsk->data = data;
return tsk;
}
/**
* @brief Free a task
*
* @param tsk A task object to free. Frees data associated with task via `freecb` value specified in its creation. May be null
*/
void task_free(void *tsk) {
task *real = tsk;
if(!real) return;
if(real->freecb) real->freecb(real->data);
free(real);
return;
}
/**
* @brief Fire a task. Passes the `data` member to the specified `callback`
*
* @param tsk A task to be fired. Must be non-null
* @retval (int) Returns value of the fired callback. Returns -1 and sets errno on error
*/
int task_fire(task *tsk) {
if(!tsk) ERRRET(EINVAL, -1);
return tsk->callback(tsk->data);
}
/**
* @brief Fire and destroy a task simultaneously. Calls specified callback and free-callback on associated data
*
* @param tsk Task to be fired and destroyed. Must be non-null
* @retval (int) Returns value of the callback. Returns -1 and sets errno on error
*/
int task_fired(task *tsk) {
if(!tsk) return -1;
int retval = task_fire(tsk);
task_free(tsk);
return retval;
}
tqnode * tqnode_new(tqnode *next, tqnode *prev, task *tsk) {
if(!tsk) ERRRET(EINVAL, NULL);
tqnode *node = calloc(1, sizeof(*node));
if(!node) return NULL;
node->next = next;
node->prev = prev;
node->task = tsk;
return node;
}
// Create a tqnode and task at the same time. Returns a valid tqnode with a valid task on success, NULL on error. Does not call task_free on error
tqnode * tqnode_newtask(tqnode *next, tqnode *prev, gcallback callback, fcallback freecb, void *data) {
task *tsk = task_new(callback, freecb, data);
if(!tsk) return NULL;
tqnode *node = tqnode_new(next, prev, tsk);
if(!node) free(tsk);
return node;
}
void tqnode_free(void *tqn) {
tqnode *real = tqn;
if(!real) return;
task_free(real->task);
free(real);
return;
}
/**
* @brief Create a FIFO queue of tasks
*
* @retval (taskqueue*)[NULL, taskqueue*] Returns a new taskqueue object. Returns `null` and sets errno on error
*/
taskqueue * taskqueue_new(void) {
taskqueue *tq = calloc(1, sizeof(*tq));
if(!tq) return NULL;
tq->start = NULL;
tq->end = NULL;
tq->size = 0;
return tq;
}
/**
* @brief Free a taskqueue
*
* @param tq A taskqueue to be freed. May be null
*/
void taskqueue_free(void *tq) {
taskqueue *real = tq;
if(!real) return;
for(tqnode *p = real->start, *n; p != NULL;) {
n = p->next;
tqnode_free(p);
p = n;
}
free(real);
return;
}
int taskqueue_handlefirst(taskqueue *tq, task *tsk) {
if(!tq || !tsk) ERRRET(EINVAL, -1);
if(tq->size) {return 0;}
tqnode *first = tqnode_new(NULL, NULL, tsk);
if(!first) return -1;
tq->start = first;
tq->end = first;
tq->size = 1;
return 1;
}
/**
* @brief Push a task onto a taskqueue
*
* @param tq The taskqueue to be modified. Must be non-null
* @param tsk The task to push. Must be non-null
* @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
*/
int taskqueue_push(taskqueue *tq, task *tsk) {
if(!tq || !tsk) ERRRET(EINVAL, -1);
int hf;
if((hf = taskqueue_handlefirst(tq, tsk))) return (hf >= 0) ? 0 : -1;
tqnode *curstart = tq->start;
tqnode *newstart = tqnode_new(curstart, NULL, tsk);
if(!newstart) return -1;
curstart->prev = newstart;
tq->start = newstart;
tq->size++;
return 0;
}
/**
* @brief Pop a task from a taskqueue
*
* @param tq A taskqueue to grab a task from. Must be non-null
* @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error
*/
task * taskqueue_pop(taskqueue *tq) {
if(!tq) ERRRET(EINVAL, NULL);
if(tq->size <= 0) ERRRET(ENODATA, NULL);
tqnode *curend = tq->end;
task *ret = curend->task;
if(tq->size == 1) {
tq->end = NULL;
tq->start = NULL;
} else {
tq->end = curend->prev;
tq->end->next = NULL;
}
free(curend);
tq->size--;
return ret;
}
/**
* @brief Append a task to the front of a taskqueue
*
* @param tq The taskqueue to be modified. Must be non-null
* @param tsk The task to be appended. Must be non-null
* @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
*/
int taskqueue_pushfront(taskqueue *tq, task *tsk) {
if(!tq || !tsk) ERRRET(EINVAL, -1);
int hf;
if((hf = taskqueue_handlefirst(tq, tsk))) return (hf >= 0) ? 0 : -1;
tqnode *end = tq->end;
tqnode *newend = tqnode_new(NULL, end, tsk);
if(!newend) return -1;
end->next = newend;
tq->end = newend;
tq->size++;
return 0;
}
/**
* @brief Pop a task from the back (most recently pushed task) of a taskqueue
*
* @param tq A taskqueue to pop from. Must be non-null
* @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error
*/
task * taskqueue_popback(taskqueue *tq) {
if(!tq) ERRRET(EINVAL, NULL);
if(tq->size <= 0) ERRRET(ENODATA, NULL);
tqnode *curstart = tq->start;
task *ret = curstart->task;
if(tq->size == 1) {
tq->start = NULL;
tq->end = NULL;
} else {
tq->start = curstart->next;
tq->start->prev = NULL;
}
free(curstart);
tq->size--;
return ret;
}
int taskqueue_size(taskqueue *tq) {
if(!tq) ERRRET(EINVAL, -1);
return tq->size;
}
//! Internal helper macro for ctq functions. Acquires a lock via the ctq's mutex, checks to see if the queue has been canceled, then executes "code" as written
#define __CTQ_INLOCK(ctq, retval, code) do {\
mtx_lock(&(ctq)->mutex); \
if((ctq)->canceled) { \
errno = ECANCELED; \
mtx_unlock(&(ctq)->mutex); \
return (retval); \
} \
code \
mtx_unlock(&(ctq)->mutex); \
} while (0)
static void mtxd_helper(mtx_t *mutex) {
if(!mutex) return;
mtx_destroy(mutex);
return;
}
static void cndd_helper(cnd_t *cond) {
if(!cond) return;
cnd_destroy(cond);
return;
}
/**
* @brief Create a concurrent taskqueue with `size` allocated threads
*
* @param size Number of threads in the threadpool. Must be greater than zero
* @retval (ctqueue*)[NULL, ctqueue*] Returns a new ctqueue, sets errno and returns `null` on error
*/
ctqueue * ctqueue_init(int nthreads) {
if(nthreads <= 0) ERRRET(EINVAL, NULL);
ctqueue *ctq = calloc(1, sizeof(*ctq));
if(!ctq) return NULL;
ctq->canceled = 0;
ctq->talen = nthreads;
ctq->tq = taskqueue_new();
if(!ctq->tq) goto ERR_ctqueue_init;
if(mtx_init(&ctq->mutex, mtx_plain) != thrd_success) goto ERR_ctqueue_init;
if(cnd_init(&ctq->cond) != thrd_success) goto ERR_ctqueue_init;
ctq->thrdarr = calloc(ctq->talen, sizeof(thrd_t));
if(!ctq->thrdarr) goto ERR_ctqueue_init;
return ctq;
ERR_ctqueue_init:
free(ctq->thrdarr);
cndd_helper(&ctq->cond);
mtxd_helper(&ctq->mutex);
taskqueue_free(ctq->tq);
free(ctq);
return NULL;
}
/**
* @brief Cancel all tasks being processed in a currently running concurrent taskqueue
*
* @param ctq The concurrent taskqueue to be canceled. Must be non-null
* @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
*/
int ctqueue_cancel(ctqueue *ctq) {
if(!ctq) ERRRET(EINVAL, -1);
__CTQ_INLOCK(ctq, 1,
ctq->canceled = 1;
);
cnd_broadcast(&ctq->cond);
return 0;
}
/**
* @brief Free a concurrent taskqueue
* @attention This cancels all currently running threads via `ctqueue_cancel`
*
* @param ctq The concurrent taskqueue to free. May be null
*/
void ctqueue_free(void *ctq) {
if(!ctq) return;
ctqueue *real = (ctqueue *)ctq;
ctqueue_cancel(real);
for(int i = 0; i < real->talen; i++)
thrd_join(real->thrdarr[i], NULL);
// Threads are dead, everything's free game
mtx_destroy(&real->mutex);
cnd_destroy(&real->cond);
taskqueue_free(real->tq);
free(real->thrdarr);
free(real);
return;
}
/**
* @brief Push a task onto a concurrent taskqueue
* @attention May block for an indefinite amount of time to push the task
*
* @param ctq The concurrent taskqueue to modify. Must be non-null
* @param tsk The task to push. Must be non-null
* @retval (int)[`thrd_error` | `thrd_nomem`, `thrd_success`]
*/
int ctqueue_waitpush(ctqueue *ctq, task *tsk) {
if(!ctq || !tsk) ERRRET(EINVAL, -1);
int retval = 0;
__CTQ_INLOCK(ctq, -1,
retval = taskqueue_push(ctq->tq, tsk);
);
if(retval == 0)
cnd_signal(&ctq->cond);
return retval;
}
/**
* @brief Pop a task from the concurrent taskqueue
* @attention May block for an indefinite amount of time to pop the task
*
* @param ctq The concurrent taskqueue to pop from. Must be non-null
* @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error
*/
task * ctqueue_waitpop(ctqueue *ctq) {
if(!ctq) ERRRET(EINVAL, NULL);
task *retval = NULL;
__CTQ_INLOCK(ctq, NULL,
while(taskqueue_size(ctq->tq) == 0 && !ctq->canceled)
cnd_wait(&ctq->cond, &ctq->mutex);
if(ctq->canceled) {
mtx_unlock(&ctq->mutex);
ERRRET(ECANCELED, NULL);
}
retval = taskqueue_pop(ctq->tq);
);
return retval;
}
//! Simple consumer for eating and executing tasks from the ctq
static int __CTQ_CONSUMER(void *ctq) {
if(!ctq) {errno = EINVAL; thrd_exit(-1);}
ctqueue *real = (ctqueue *)ctq;
for(task *ctask = NULL;;) {
ctask = ctqueue_waitpop(real);
if(!ctask) break;
task_fire(ctask);
task_free(ctask);
}
thrd_exit(1);
}
// TODO: Make this function return 0 or -1 depending on whether the overall ctq has been canceled or not. Canceling shouldn't
// be treated as an error
/**
* @brief Start the threads allocated to a concurrent taskqueue
* @attention Threads will not consume pushed tasks until this function is ran
*
* @param ctq A concurrent taskqueue to start. Must be non-null
* @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
*/
int ctqueue_start(ctqueue *ctq) {
if(!ctq) ERRRET(EINVAL, -1);
ctq->canceled = 0;
int retval = 0;
for(int i = 0; i < ctq->talen; i++)
if((retval = thrd_create(&ctq->thrdarr[i], __CTQ_CONSUMER, ctq)) != thrd_success)
break;
if(retval != thrd_success)
ctqueue_cancel(ctq);
return (retval == thrd_success) ? 0 : -1;
}
#endif
|