forked from sysprog21/mergesort-concurrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadpool.c
158 lines (149 loc) · 4.25 KB
/
threadpool.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "threadpool.h"
/**
* @brief Release the memory allocated in _task\_t_
*
* Note that you have to make sure that the resourse which
* _task\_t->arg_ points to is no longer used after this
* function call.
*
* @param the_task Pointer to the target _task\_t_
*/
int task_free(task_t *the_task)
{
free(the_task->arg);
free(the_task);
return 0;
}
/**
* @brief Create a new task.
*/
task_t *task_new(void (*func)(void *), void *arg)
{
task_t *new_task = (task_t *)malloc(sizeof(task_t));
new_task->func = func;
new_task->arg = arg;
return new_task;
}
/**
* @brief Initialize the members in _tqueue\_t_.
*
* Note that this function doesn't allocate memory for the task queue.
*
* @param the_queue Pointer to the target _tqueue\_t_
* @return -1 if _the\_queue_ is NULL. Otherwise, return 0.
*/
int tqueue_init(tqueue_t *the_queue)
{
if (!the_queue)
return -1;
the_queue->head = NULL;
the_queue->tail = NULL;
pthread_mutex_init(&(the_queue->mutex), NULL);
pthread_cond_init(&(the_queue->cond), NULL);
the_queue->size = 0;
the_queue->num_of_consumed = 0;
return 0;
}
/**
* @brief Pop a task from the task queue.
* @param the_queue Pointer to the target task queue
* @return The popped task. NULL if the queue is empty.
*/
task_t *tqueue_pop(tqueue_t * const the_queue)
{
task_t *ret;
pthread_mutex_lock(&(the_queue->mutex));
ret = the_queue->head;
if (ret) {
the_queue->head = ret->next;
ret->next = NULL;
if (!(the_queue->head)) {
the_queue->tail = NULL;
}
the_queue->size--;
the_queue->num_of_consumed++;
}
pthread_mutex_unlock(&(the_queue->mutex));
return ret;
}
/**
* @brief Get the number of the tasks in the task queue
* @param the_queue Pointer to the target task queue
* @return The number of the tasks in the task queue
*/
uint32_t tqueue_size(tqueue_t * const the_queue)
{
uint32_t ret;
pthread_mutex_lock(&(the_queue->mutex));
ret = the_queue->size;
pthread_mutex_unlock(&(the_queue->mutex));
return ret;
}
/**
* @brief Push a task to the task queue
* @param the_queue Pointer to the target task queue
* @param task Pointer to the task to push
*/
int tqueue_push(tqueue_t * const the_queue, task_t *task)
{
pthread_mutex_lock(&(the_queue->mutex));
task->next = NULL;
if (the_queue->tail)
the_queue->tail->next = task;
the_queue->tail = task;
if (the_queue->size++ == 0)
the_queue->head = task;
pthread_mutex_unlock(&(the_queue->mutex));
return 0;
}
/**
* @brief Release the memory of the remaining tasks in the task queue and destory the mutex lock
* @param the_queue The target task queue
* @return The number of task consumed.
*/
uint32_t tqueue_free(tqueue_t *the_queue)
{
uint32_t num_of_consumed = the_queue->num_of_consumed;
task_t *cur = the_queue->head;
while (cur) {
the_queue->head = the_queue->head->next;
free(cur);
cur = the_queue->head;
}
pthread_mutex_destroy(&(the_queue->mutex));
return num_of_consumed;
}
/**
* @brief Create and initialize the thread pool
* @param the_pool Pointer to the created thread pool
* @param tcount The number of the threads to be created
* @param func Pointer to the task function
*/
int tpool_init(tpool_t *the_pool, uint32_t tcount, void *(*func)(void *))
{
the_pool->threads = (pthread_t *) malloc(sizeof(pthread_t) * tcount);
the_pool->count = tcount;
the_pool->queue = (tqueue_t *) malloc(sizeof(tqueue_t));
tqueue_init(the_pool->queue);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (uint32_t i = 0; i < tcount; ++i)
pthread_create(&(the_pool->threads[i]), &attr, func, NULL);
pthread_attr_destroy(&attr);
return 0;
}
/**
* @brief Join all the threads and release the allocated memory of thread pool
* @param the_pool The target thread pool
* @return The number of consumed tasks
*/
uint32_t tpool_free(tpool_t *the_pool)
{
int num_of_consumed;
for (uint32_t i = 0; i < the_pool->count; ++i)
pthread_join(the_pool->threads[i], NULL);
free(the_pool->threads);
num_of_consumed = tqueue_free(the_pool->queue);
return num_of_consumed;
}