1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#ifndef __VXGG_REWRITE___THREADPOOL_H___193271180830131___
#define __VXGG_REWRITE___THREADPOOL_H___193271180830131___ 1
#include "shared.h"
#include <threads.h>
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
|