#ifndef __VXGG_REWRITE___THREADPOOL_H___193271180830131___ #define __VXGG_REWRITE___THREADPOOL_H___193271180830131___ 1 #include "shared.h" #include 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 * @return 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 * @return 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 * @return 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 * * @return 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 * @return int 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 * @return 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 * @return int Returns 0 on success, sets errno and returns `null` 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 * @return 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 * @return 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 * @return int 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 * @return 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 * @return 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 * @return int Returns 0 on success, sets errno and returns -1 on error */ int ctqueue_start(ctqueue *ctq); #endif