diff options
| author | @syxhe <https://t.me/syxhe> | 2025-06-06 13:30:34 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-06-06 13:30:34 -0500 |
| commit | 16528ac295215e788cb226f0cc49f11f82919741 (patch) | |
| tree | 8bbe621a58a421b894330205bf07285e80e40e9e /src/threadpool.h | |
| parent | 8fe1a7ea459829145dfef4b0aa8f627f96841cbd (diff) | |
Get threadpool implementation workingthreadpool-debugging
Diffstat (limited to 'src/threadpool.h')
| -rw-r--r-- | src/threadpool.h | 90 |
1 files changed, 69 insertions, 21 deletions
diff --git a/src/threadpool.h b/src/threadpool.h index d7b713c..3048eea 100644 --- a/src/threadpool.h +++ b/src/threadpool.h | |||
| @@ -1,26 +1,74 @@ | |||
| 1 | #ifndef __VXGG_REWRITE___THREADPOOL_H___13601325413136___ | 1 | #ifndef __VXGG_REWRITE___THREADPOOL_H___193271180830131___ |
| 2 | #define __VXGG_REWRITE___THREADPOOL_H___13601325413136___ | 2 | #define __VXGG_REWRITE___THREADPOOL_H___193271180830131___ 1 |
| 3 | 3 | ||
| 4 | #include "shared.h" | ||
| 4 | #include <threads.h> | 5 | #include <threads.h> |
| 5 | 6 | ||
| 6 | typedef int (*task_callback)(void*); | 7 | typedef struct task { |
| 7 | typedef struct task task; | 8 | gcallback callback; |
| 8 | typedef struct cq cqueue; | 9 | fcallback freecb; |
| 9 | typedef struct tp threadpool; | 10 | void *data; |
| 10 | 11 | } task; | |
| 11 | task * task_init(task_callback cb, void *arg); | 12 | |
| 12 | void task_free(void *ts); | 13 | typedef struct tqnode { |
| 13 | int task_fire(task *ts); | 14 | struct tqnode *next; |
| 14 | 15 | struct tqnode *prev; | |
| 15 | cqueue * cqueue_init(); | 16 | task *task; |
| 16 | void cqueue_cancel(cqueue * const cq); | 17 | } tqnode; |
| 17 | void cqueue_free(void *cq); | 18 | |
| 18 | int cqueue_addtask(cqueue * const cq, task * const tsk); | 19 | typedef struct taskqueue { |
| 19 | task * cqueue_waitpop(cqueue * const cq); | 20 | tqnode *start; |
| 20 | int cqueue_registerthreads(cqueue * const cq, int threads); | 21 | tqnode *end; |
| 21 | int cqueue_registerthread(cqueue * const cq); | 22 | unsigned int size; |
| 22 | int cqueue_iscanceled(cqueue * const cq); | 23 | } taskqueue; |
| 23 | int cqueue_numthreads(cqueue * const cq); | 24 | |
| 24 | int cqueue_numtasks(cqueue * const cq); | 25 | typedef struct ctqueue { |
| 26 | mtx_t mutex; | ||
| 27 | cnd_t cond; | ||
| 28 | unsigned char canceled; | ||
| 29 | |||
| 30 | taskqueue *tq; | ||
| 31 | thrd_t *thrdarr; | ||
| 32 | int talen; | ||
| 33 | } ctqueue; | ||
| 34 | |||
| 35 | // Create a new task. Sets `errno` and returns `NULL` on error | ||
| 36 | task * task_init(gcallback callback, fcallback freecb, void *data); | ||
| 37 | // Free a task. Passes the `.data` member to specified fcallback as a function parameter. Does not return a value or set `errno` | ||
| 38 | void task_free(void *tsk); | ||
| 39 | // Fire a task. Passes the `.data` member to specified gcallback as a function parameter. Returns value of gcallback, or sets `errno` and returns `-1` on error | ||
| 40 | int task_fire(task *tsk); | ||
| 41 | // Fire and free a task simultaneously. Calls specified gcallback and fcallback on associated data. Returns value of gcallback, or sets `errno` and returns `-1` on error | ||
| 42 | int task_fired(task *tsk); | ||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | // Create a FIFO queue of task objects. Returns a new taskqueue on success, sets `errno` and returns `NULL` on error | ||
| 47 | taskqueue * taskqueue_init(void); | ||
| 48 | // Free a taskqueue. Does not return a value or set `errno` | ||
| 49 | void taskqueue_free(void *tq); | ||
| 50 | // Push a task onto the queue. Returns 0 on success, sets `errno` and returns `-1` on error | ||
| 51 | int taskqueue_push(taskqueue *tq, task *tsk); | ||
| 52 | // Pop a task from the queue. Returns a task on success, sets `errno` and returns `NULL` on error | ||
| 53 | task * taskqueue_pop(taskqueue *tq); | ||
| 54 | // Push a task to the front of the queue (append, task becomes first in line to be popped). Returns 0 on success, sets `errno` and returns `-1` on error | ||
| 55 | int taskqueue_pushfront(taskqueue *tq, task *tsk); | ||
| 56 | // Pop a task from the back of the queue (pop the most recently (normally) pushed item). Returns a task on success, sets `errno` and returns `NULL` on error | ||
| 57 | task * taskqueue_popback(taskqueue *tq); | ||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | // Create a concurrent taskqueue with `size` allocated threads | ||
| 62 | ctqueue * ctqueue_init(int size); | ||
| 63 | // Cancel a currently running ctq | ||
| 64 | int ctqueue_cancel(ctqueue *ctq); | ||
| 65 | // Free a ctq (cancels any remaining operations) | ||
| 66 | void ctqueue_free(void *ctq); | ||
| 67 | // Push a new task to the queue, waiting via mutex to do so | ||
| 68 | int ctqueue_waitpush(ctqueue *ctq, task *tsk); | ||
| 69 | // Pop a task from the queue, waiting via mutex to do so | ||
| 70 | task * ctqueue_waitpop(ctqueue *ctq); | ||
| 71 | // Spawn the allocated threads for a ctq | ||
| 72 | int ctqueue_start(ctqueue *ctq); | ||
| 25 | 73 | ||
| 26 | #endif \ No newline at end of file | 74 | #endif \ No newline at end of file |
