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
|
/**
* @file threadpool.h
* @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_H___193271180830131___
#define __VXGG_REWRITE___THREADPOOL_H___193271180830131___ 1
#include "shared.h"
#include <threads.h>
typedef struct task task;
typedef struct taskqueue taskqueue;
typedef struct ctqueue 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_init(gcallback callback, fcallback freecb, void *data);
/**
* @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);
/**
* @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);
/**
* @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);
/**
* @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_init(void);
/**
* @brief Free a taskqueue
*
* @param tq A taskqueue to be freed. May be null
*/
void taskqueue_free(void *tq);
/**
* @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);
/**
* @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);
/**
* @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);
/**
* @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);
/**
* @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 size);
/**
* @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);
/**
* @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);
/**
* @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) Returns `thrd_success` on success, returns `thrd_error` or `thrd_nomem` on error
*/
int ctqueue_waitpush(ctqueue *ctq, task *tsk);
/**
* @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);
/**
* @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);
#endif
|