diff options
Diffstat (limited to 'src/threadpool.h')
| -rw-r--r-- | src/threadpool.h | 160 |
1 files changed, 0 insertions, 160 deletions
diff --git a/src/threadpool.h b/src/threadpool.h deleted file mode 100644 index 61dbacd..0000000 --- a/src/threadpool.h +++ /dev/null | |||
| @@ -1,160 +0,0 @@ | |||
| 1 | /** | ||
| 2 | * @file threadpool.h | ||
| 3 | * @author syxhe (https://t.me/syxhe) | ||
| 4 | * @brief An implementation of a threadpool using libc threads | ||
| 5 | * @version 0.1 | ||
| 6 | * @date 2025-06-09 | ||
| 7 | * | ||
| 8 | * @copyright Copyright (c) 2025 | ||
| 9 | * | ||
| 10 | */ | ||
| 11 | |||
| 12 | #ifndef __VXGG_REWRITE___THREADPOOL_H___193271180830131___ | ||
| 13 | #define __VXGG_REWRITE___THREADPOOL_H___193271180830131___ 1 | ||
| 14 | |||
| 15 | #include "shared.h" | ||
| 16 | #include <threads.h> | ||
| 17 | |||
| 18 | typedef struct task task; | ||
| 19 | typedef struct taskqueue taskqueue; | ||
| 20 | typedef struct ctqueue ctqueue; | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @brief Create a task | ||
| 24 | * | ||
| 25 | * @param callback Callback function the given data should be ran with. Must be non-null | ||
| 26 | * @param freecb Callback function for freeing the given data. May be null | ||
| 27 | * @param data Data to be passed to the callback. May be null | ||
| 28 | * @retval (task*)[NULL, task*] Returns a task object with set parameters. Returns `null` and sets errno on error | ||
| 29 | */ | ||
| 30 | task * task_init(gcallback callback, fcallback freecb, void *data); | ||
| 31 | |||
| 32 | /** | ||
| 33 | * @brief Free a task | ||
| 34 | * | ||
| 35 | * @param tsk A task object to free. Frees data associated with task via `freecb` value specified in its creation. May be null | ||
| 36 | */ | ||
| 37 | void task_free(void *tsk); | ||
| 38 | |||
| 39 | /** | ||
| 40 | * @brief Fire a task. Passes the `data` member to the specified `callback` | ||
| 41 | * | ||
| 42 | * @param tsk A task to be fired. Must be non-null | ||
| 43 | * @retval (int) Returns value of the fired callback. Returns -1 and sets errno on error | ||
| 44 | */ | ||
| 45 | int task_fire(task *tsk); | ||
| 46 | |||
| 47 | /** | ||
| 48 | * @brief Fire and destroy a task simultaneously. Calls specified callback and free-callback on associated data | ||
| 49 | * | ||
| 50 | * @param tsk Task to be fired and destroyed. Must be non-null | ||
| 51 | * @retval (int) Returns value of the callback. Returns -1 and sets errno on error | ||
| 52 | */ | ||
| 53 | int task_fired(task *tsk); | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /** | ||
| 58 | * @brief Create a FIFO queue of tasks | ||
| 59 | * | ||
| 60 | * @retval (taskqueue*)[NULL, taskqueue*] Returns a new taskqueue object. Returns `null` and sets errno on error | ||
| 61 | */ | ||
| 62 | taskqueue * taskqueue_init(void); | ||
| 63 | |||
| 64 | /** | ||
| 65 | * @brief Free a taskqueue | ||
| 66 | * | ||
| 67 | * @param tq A taskqueue to be freed. May be null | ||
| 68 | */ | ||
| 69 | void taskqueue_free(void *tq); | ||
| 70 | |||
| 71 | /** | ||
| 72 | * @brief Push a task onto a taskqueue | ||
| 73 | * | ||
| 74 | * @param tq The taskqueue to be modified. Must be non-null | ||
| 75 | * @param tsk The task to push. Must be non-null | ||
| 76 | * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error | ||
| 77 | */ | ||
| 78 | int taskqueue_push(taskqueue *tq, task *tsk); | ||
| 79 | |||
| 80 | /** | ||
| 81 | * @brief Pop a task from a taskqueue | ||
| 82 | * | ||
| 83 | * @param tq A taskqueue to grab a task from. Must be non-null | ||
| 84 | * @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error | ||
| 85 | */ | ||
| 86 | task * taskqueue_pop(taskqueue *tq); | ||
| 87 | |||
| 88 | /** | ||
| 89 | * @brief Append a task to the front of a taskqueue | ||
| 90 | * | ||
| 91 | * @param tq The taskqueue to be modified. Must be non-null | ||
| 92 | * @param tsk The task to be appended. Must be non-null | ||
| 93 | * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error | ||
| 94 | */ | ||
| 95 | int taskqueue_pushfront(taskqueue *tq, task *tsk); | ||
| 96 | |||
| 97 | /** | ||
| 98 | * @brief Pop a task from the back (most recently pushed task) of a taskqueue | ||
| 99 | * | ||
| 100 | * @param tq A taskqueue to pop from. Must be non-null | ||
| 101 | * @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error | ||
| 102 | */ | ||
| 103 | task * taskqueue_popback(taskqueue *tq); | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | /** | ||
| 108 | * @brief Create a concurrent taskqueue with `size` allocated threads | ||
| 109 | * | ||
| 110 | * @param size Number of threads in the threadpool. Must be greater than zero | ||
| 111 | * @retval (ctqueue*)[NULL, ctqueue*] Returns a new ctqueue, sets errno and returns `null` on error | ||
| 112 | */ | ||
| 113 | ctqueue * ctqueue_init(int size); | ||
| 114 | |||
| 115 | /** | ||
| 116 | * @brief Cancel all tasks being processed in a currently running concurrent taskqueue | ||
| 117 | * | ||
| 118 | * @param ctq The concurrent taskqueue to be canceled. Must be non-null | ||
| 119 | * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error | ||
| 120 | */ | ||
| 121 | int ctqueue_cancel(ctqueue *ctq); | ||
| 122 | |||
| 123 | /** | ||
| 124 | * @brief Free a concurrent taskqueue | ||
| 125 | * @attention This cancels all currently running threads via `ctqueue_cancel` | ||
| 126 | * | ||
| 127 | * @param ctq The concurrent taskqueue to free. May be null | ||
| 128 | */ | ||
| 129 | void ctqueue_free(void *ctq); | ||
| 130 | |||
| 131 | /** | ||
| 132 | * @brief Push a task onto a concurrent taskqueue | ||
| 133 | * @attention May block for an indefinite amount of time to push the task | ||
| 134 | * | ||
| 135 | * @param ctq The concurrent taskqueue to modify. Must be non-null | ||
| 136 | * @param tsk The task to push. Must be non-null | ||
| 137 | * @retval (int) Returns `thrd_success` on success, returns `thrd_error` or `thrd_nomem` on error | ||
| 138 | */ | ||
| 139 | int ctqueue_waitpush(ctqueue *ctq, task *tsk); | ||
| 140 | |||
| 141 | |||
| 142 | /** | ||
| 143 | * @brief Pop a task from the concurrent taskqueue | ||
| 144 | * @attention May block for an indefinite amount of time to pop the task | ||
| 145 | * | ||
| 146 | * @param ctq The concurrent taskqueue to pop from. Must be non-null | ||
| 147 | * @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error | ||
| 148 | */ | ||
| 149 | task * ctqueue_waitpop(ctqueue *ctq); | ||
| 150 | |||
| 151 | /** | ||
| 152 | * @brief Start the threads allocated to a concurrent taskqueue | ||
| 153 | * @attention Threads will not consume pushed tasks until this function is ran | ||
| 154 | * | ||
| 155 | * @param ctq A concurrent taskqueue to start. Must be non-null | ||
| 156 | * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error | ||
| 157 | */ | ||
| 158 | int ctqueue_start(ctqueue *ctq); | ||
| 159 | |||
| 160 | #endif \ No newline at end of file | ||
