forked from tinylcy/vino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreadpool.c
166 lines (132 loc) · 3.85 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
159
160
161
162
163
164
165
166
/*
* Copyright (C) Chenyang Li
* Copyright (C) tinyhttpd
*/
#include <stdio.h>
#include <stdlib.h>
#include "threadpool.h"
/*-----------------------------------------------------------*
each thread runs this function: fetching the first job of
queue in threadpool and executing the callback function.
-----------------------------------------------------------*/
void *threadpool_function(void *arg) {
threadpool_t *pool = (threadpool_t *)arg;
while(1) {
if(pthread_mutex_lock(&(pool->mutex)) != 0) {
perror("fail to lock the mutex");
pthread_exit(NULL);
}
while(pool->job_cur_num == 0) {
if(pthread_cond_wait(&(pool->queue_not_empty), &(pool->mutex)) != 0) {
perror("fail to wait queue_not_empty");
pthread_exit(NULL);
}
}
job_t *pjob = pool->head;
pool->job_cur_num--;
if(pool->job_cur_num == 0) {
pool->head = pool->tail = NULL;
} else {
pool->head = pjob->next;
}
(*(pjob->p_callback_func))(pjob->arg);
free(pjob);
pjob = NULL;
if(pthread_mutex_unlock(&(pool->mutex)) != 0) {
perror("fail to unlock the mutex");
pthread_exit(NULL);
}
}
}
/*-----------------------------------------------------------*
initialize threadpool.
-----------------------------------------------------------*/
threadpool_t *threadpool_init(int thread_num, int job_max_num) {
threadpool_t *pool = NULL;
pool = (threadpool_t *)malloc(sizeof(threadpool_t));
if(pool == NULL) {
perror("fail to malloc threadpool");
return NULL;
}
pool->thread_num = thread_num;
pool->job_max_num = job_max_num;
pool->job_cur_num = 0;
pool->head = NULL;
pool->tail = NULL;
if(pthread_mutex_init(&(pool->mutex), NULL) != 0) {
perror("fail to initialize the mutex");
exit(EXIT_FAILURE);
}
if(pthread_cond_init(&(pool->queue_not_empty), NULL) != 0) {
perror("fail to initialize queue_not_empty");
exit(EXIT_FAILURE);
}
if(pthread_cond_init(&(pool->queue_not_full), NULL) != 0) {
perror("fail to initialize queue_not_full");
exit(EXIT_FAILURE);
}
if(pthread_cond_init(&(pool->queue_empty), NULL) != 0) {
perror("fail to initialize queue_empty");
exit(EXIT_FAILURE);
}
pool->pthreads = (pthread_t *)malloc(sizeof(pthread_t) * pool->thread_num);
if(pool->pthreads == NULL) {
perror("fail to malloc pthreads");
exit(EXIT_FAILURE);
}
int i;
for(i = 0; i < pool->thread_num; i++) {
if(pthread_create(&(pool->pthreads[i]), NULL, threadpool_function, (void*)pool) != 0) {
perror("fail to create the pthread");
exit(EXIT_FAILURE);
}
}
return pool;
}
/*-----------------------------------------------------------*
add one job into threadpool
-----------------------------------------------------------*/
int threadpool_add_job(threadpool_t *pool, callback_func p_callback_func, void *arg) {
if(pool == NULL || p_callback_func == NULL) {
return -1;
}
if(pthread_mutex_lock(&(pool->mutex)) != 0) {
perror("fail to lock the mutex");
return -1;
}
while(pool->job_cur_num == pool->job_max_num - 1) {
pthread_cond_wait(&(pool->queue_not_full), &(pool->mutex));
}
job_t *pjob = (job_t *)malloc(sizeof(job_t));
if(pjob == NULL) {
perror("fail to malloc job");
pthread_mutex_unlock(&(pool->mutex));
return -1;
}
pjob->p_callback_func = p_callback_func;
pjob->arg = arg;
pjob->next = NULL;
if(pool->job_cur_num == 0) {
pool->head = pool->tail = pjob;
} else {
pool->tail->next = pjob;
pool->tail = pjob;
}
pool->job_cur_num++;
if(pthread_mutex_unlock(&(pool->mutex)) != 0) {
perror("fail to unlock the mutex");
return -1;
}
if(pthread_cond_signal(&(pool->queue_not_empty)) != 0) {
perror("fail to signal signal queue_not_empty");
return -1;
}
return 0;
}
/*-----------------------------------------------------------*
destroy threadpool.
-----------------------------------------------------------*/
int threadpool_destroy(threadpool_t *pool) {
// TODO
return 0;
}