From 16528ac295215e788cb226f0cc49f11f82919741 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Fri, 6 Jun 2025 13:30:34 -0500 Subject: Get threadpool implementation working --- src/threadpool.h | 90 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 21 deletions(-) (limited to 'src/threadpool.h') 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 @@ -#ifndef __VXGG_REWRITE___THREADPOOL_H___13601325413136___ -#define __VXGG_REWRITE___THREADPOOL_H___13601325413136___ +#ifndef __VXGG_REWRITE___THREADPOOL_H___193271180830131___ +#define __VXGG_REWRITE___THREADPOOL_H___193271180830131___ 1 +#include "shared.h" #include -typedef int (*task_callback)(void*); -typedef struct task task; -typedef struct cq cqueue; -typedef struct tp threadpool; - -task * task_init(task_callback cb, void *arg); -void task_free(void *ts); -int task_fire(task *ts); - -cqueue * cqueue_init(); -void cqueue_cancel(cqueue * const cq); -void cqueue_free(void *cq); -int cqueue_addtask(cqueue * const cq, task * const tsk); -task * cqueue_waitpop(cqueue * const cq); -int cqueue_registerthreads(cqueue * const cq, int threads); -int cqueue_registerthread(cqueue * const cq); -int cqueue_iscanceled(cqueue * const cq); -int cqueue_numthreads(cqueue * const cq); -int cqueue_numtasks(cqueue * const cq); +typedef struct task { + gcallback callback; + fcallback freecb; + void *data; +} task; + +typedef struct tqnode { + struct tqnode *next; + struct tqnode *prev; + task *task; +} tqnode; + +typedef struct taskqueue { + tqnode *start; + tqnode *end; + unsigned int size; +} taskqueue; + +typedef struct ctqueue { + mtx_t mutex; + cnd_t cond; + unsigned char canceled; + + taskqueue *tq; + thrd_t *thrdarr; + int talen; +} ctqueue; + +// Create a new task. Sets `errno` and returns `NULL` on error +task * task_init(gcallback callback, fcallback freecb, void *data); +// Free a task. Passes the `.data` member to specified fcallback as a function parameter. Does not return a value or set `errno` +void task_free(void *tsk); +// 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 +int task_fire(task *tsk); +// 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 +int task_fired(task *tsk); + + + +// Create a FIFO queue of task objects. Returns a new taskqueue on success, sets `errno` and returns `NULL` on error +taskqueue * taskqueue_init(void); +// Free a taskqueue. Does not return a value or set `errno` +void taskqueue_free(void *tq); +// Push a task onto the queue. Returns 0 on success, sets `errno` and returns `-1` on error +int taskqueue_push(taskqueue *tq, task *tsk); +// Pop a task from the queue. Returns a task on success, sets `errno` and returns `NULL` on error +task * taskqueue_pop(taskqueue *tq); +// 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 +int taskqueue_pushfront(taskqueue *tq, task *tsk); +// 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 +task * taskqueue_popback(taskqueue *tq); + + + +// Create a concurrent taskqueue with `size` allocated threads +ctqueue * ctqueue_init(int size); +// Cancel a currently running ctq +int ctqueue_cancel(ctqueue *ctq); +// Free a ctq (cancels any remaining operations) +void ctqueue_free(void *ctq); +// Push a new task to the queue, waiting via mutex to do so +int ctqueue_waitpush(ctqueue *ctq, task *tsk); +// Pop a task from the queue, waiting via mutex to do so +task * ctqueue_waitpop(ctqueue *ctq); +// Spawn the allocated threads for a ctq +int ctqueue_start(ctqueue *ctq); #endif \ No newline at end of file -- cgit v1.2.3