-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_pool.h
143 lines (124 loc) · 3.38 KB
/
thread_pool.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdbool.h>
#ifndef THREAD_POOL_DEFINED
#define THREAD_POOL_DEFINED
struct thread_pool;
struct thread_task;
typedef void *(*thread_task_f)(void *);
enum {
TPOOL_MAX_THREADS = 20,
TPOOL_MAX_TASKS = 100000,
};
enum thread_poool_errcode {
TPOOL_ERR_INVALID_ARGUMENT = 1,
TPOOL_ERR_TOO_MANY_TASKS,
TPOOL_ERR_HAS_TASKS,
TPOOL_ERR_TASK_NOT_PUSHED,
TPOOL_ERR_TASK_IN_POOL,
TPOOL_ERR_NOT_IMPLEMENTED,
TPOOL_ERR_TIMEOUT,
};
/** Thread pool API. */
/**
* Create a new thread pool with maximum @a max_thread_count
* threads.
* @param max_thread_count Maximum pool size.
* @param[out] Pointer to store result pool object.
*
* @retval 0 Success.
* @retval != 0 Error code.
* - TPOOL_ERR_INVALID_ARGUMENT - max_thread_count is too big,
* or 0.
*/
int
thread_pool_new(int max_thread_count, struct thread_pool **pool);
/**
* How many threads are created by this pool. Can be less than
* max.
* @param pool Thread pool to get thread count of.
* @retval Thread count.
*/
int
thread_pool_thread_count(const struct thread_pool *pool);
/**
* Delete @a pool, free its memory.
* @param pool Pool to delete.
* @retval 0 Success.
* @retval != Error code.
* - TPOOL_ERR_HAS_TASKS - pool still has tasks.
*/
int
thread_pool_delete(struct thread_pool *pool);
/**
* Push @a task into thread pool queue.
* @param pool Pool to push into.
* @param task Task to push.
*
* @retval 0 Success.
* @retval != Error code.
* - TPOOL_ERR_TOO_MANY_TASKS - pool has too many tasks
* already.
*/
int
thread_pool_push_task(struct thread_pool *pool, struct thread_task *task);
/** Thread pool task API. */
/**
* Create a new task to push it into a pool.
* @param[out] task Pointer to store result task object.
* @param function Function to run by this task.
* @param arg Argument for @a function.
*
* @retval Always 0.
*/
int
thread_task_new(struct thread_task **task, thread_task_f function, void *arg);
/**
* Check if @a task is finished and its result can be obtained.
* @param task Task to check.
*/
bool
thread_task_is_finished(const struct thread_task *task);
/**
* Check if @a task is running right now.
* @param task Task to check.
*/
bool
thread_task_is_running(const struct thread_task *task);
/**
* Join the task. If it is not finished, then wait until it is.
* Note, this function does not delete task object. It can be
* reused for a next task or deleted via thread_task_delete.
* @param task Task to join.
* @param[out] result Pointer to stored result of @a task.
*
* @retval 0 Success.
* @retval != 0 Error code.
* - TPOOL_ERR_TASK_NOT_PUSHED - task is not pushed to a pool.
*/
int
thread_task_join(struct thread_task *task, void **result);
/**
* Delete a task, free its memory.
* @param task Task to delete.
*
* @retval 0 Success.
* @retval != Error code.
* - TPOOL_ERR_TASK_IN_POOL - can not drop the task. It still
* is in a pool. Need to join it firstly.
*/
int
thread_task_delete(struct thread_task *task);
#ifdef NEED_DETACH
/**
* Detach a task so as to auto-delete it when it is finished.
* After detach a task can not be accessed via any functions.
* If it is already finished, then just delete it.
* @param task Task to detach.
* @retval 0 Success.
* @retval != Error code.
* - TPOOL_ERR_TASK_NOT_PUSHED - task is not pushed to a
* pool.
*/
int
thread_task_detach(struct thread_task *task);
#endif
#endif /* THREAD_POOL_DEFINED */