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