-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy paththreadpool.h
More file actions
74 lines (60 loc) · 1.89 KB
/
threadpool.h
File metadata and controls
74 lines (60 loc) · 1.89 KB
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 THREAD_H_
#define THREAD_H_
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
/**
* @defgroup Tasks
* @{
*/
/**
* @struct _task threadpool.h "threadpool.h"
* @brief The data structure for a task which will be put in a task queue
*/
typedef struct _task {
void (*func)(void *); /**< Pointer to task function */
void *arg; /**< Pointer to the arguments passed to _func_ */
struct _task *next; /**< Pointer to the next task */
} task_t;
int task_free(task_t *the_task);
task_t *task_new(void (*func)(void *), void *arg);
/** @} */
/**
* @defgroup Task_Queue
* @{
*/
/**
* @struct _tqueue_t threadpool.h "threadpool.h"
* @brief The data structure for a task queue of _task\_t_
*/
typedef struct {
task_t *head; /**< Pointer to the fist _task\_t_ in queue */
task_t *tail; /**< Pointer to the last _task\_t_ in queue */
pthread_mutex_t mutex; /**< The mutex lock of this queue */
pthread_cond_t cond; /**< The conitional variable of this queue */
uint32_t size; /**< The size of the queue */
uint32_t num_of_consumed; /**< The number of consumed tasks */
} tqueue_t;
int tqueue_init(tqueue_t *the_queue);
task_t *tqueue_pop(tqueue_t * const the_queue);
uint32_t tqueue_size(tqueue_t * const the_queue);
int tqueue_push(tqueue_t * const the_queue, task_t *task);
uint32_t tqueue_free(tqueue_t *the_queue);
/** @} */
/**
* @defgroup Thread_Pool
* @{
*/
/**
* @struct _tpool_t threadpool.h "threadpool.h"
* @brief The data structure for managing thread pool
*/
typedef struct {
pthread_t *threads; /**< Pointer to the array of threads */
uint32_t count; /**< Number of working threads */
tqueue_t *queue; /**< Pointer to the task queue */
} tpool_t;
int tpool_init(tpool_t *the_pool, uint32_t count, void *(*func)(void *));
uint32_t tpool_free(tpool_t *the_pool);
/** @} */
#endif