#ifndef __VXGG_REWRITE___THREADPOOL_H___193271180830131___ #define __VXGG_REWRITE___THREADPOOL_H___193271180830131___ 1 #include "shared.h" #include 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