summaryrefslogtreecommitdiff
path: root/src/threadpool.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/threadpool.h')
-rw-r--r--src/threadpool.h90
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
6typedef int (*task_callback)(void*); 7typedef struct task {
7typedef struct task task; 8 gcallback callback;
8typedef struct cq cqueue; 9 fcallback freecb;
9typedef struct tp threadpool; 10 void *data;
10 11} task;
11task * task_init(task_callback cb, void *arg); 12
12void task_free(void *ts); 13typedef struct tqnode {
13int task_fire(task *ts); 14 struct tqnode *next;
14 15 struct tqnode *prev;
15cqueue * cqueue_init(); 16 task *task;
16void cqueue_cancel(cqueue * const cq); 17} tqnode;
17void cqueue_free(void *cq); 18
18int cqueue_addtask(cqueue * const cq, task * const tsk); 19typedef struct taskqueue {
19task * cqueue_waitpop(cqueue * const cq); 20 tqnode *start;
20int cqueue_registerthreads(cqueue * const cq, int threads); 21 tqnode *end;
21int cqueue_registerthread(cqueue * const cq); 22 unsigned int size;
22int cqueue_iscanceled(cqueue * const cq); 23} taskqueue;
23int cqueue_numthreads(cqueue * const cq); 24
24int cqueue_numtasks(cqueue * const cq); 25typedef 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
36task * 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`
38void 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
40int 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
42int 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
47taskqueue * taskqueue_init(void);
48// Free a taskqueue. Does not return a value or set `errno`
49void taskqueue_free(void *tq);
50// Push a task onto the queue. Returns 0 on success, sets `errno` and returns `-1` on error
51int 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
53task * 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
55int 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
57task * taskqueue_popback(taskqueue *tq);
58
59
60
61// Create a concurrent taskqueue with `size` allocated threads
62ctqueue * ctqueue_init(int size);
63// Cancel a currently running ctq
64int ctqueue_cancel(ctqueue *ctq);
65// Free a ctq (cancels any remaining operations)
66void ctqueue_free(void *ctq);
67// Push a new task to the queue, waiting via mutex to do so
68int ctqueue_waitpush(ctqueue *ctq, task *tsk);
69// Pop a task from the queue, waiting via mutex to do so
70task * ctqueue_waitpop(ctqueue *ctq);
71// Spawn the allocated threads for a ctq
72int ctqueue_start(ctqueue *ctq);
25 73
26#endif \ No newline at end of file 74#endif \ No newline at end of file