-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreadpool.c
202 lines (171 loc) · 3.92 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <threadpool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#define DEBUG
#ifdef __cplusplus
extern "C" {
#endif
struct task
{
void *(*callback)(void *arg);
void *arg;
};
struct threadpool_t
{
int nthread;
int queue_size;
pthread_t *tid;
struct task *tasks;
int head,tail;
pthread_mutex_t mutex;
pthread_cond_t queue_not_empty;
pthread_cond_t queue_not_full;
};
#ifdef DEBUG
struct take_task_param
{
threadpool_t *pool;
int id;
int ntask;
};
#endif
void *take_task(void *arg)
{
threadpool_t *pool=(threadpool_t*)arg;
struct task task;
#ifdef DEBUG
struct take_task_param *param=(struct take_task_param*)arg;
pool=param->pool;
#endif
while(1)
{
pthread_mutex_lock(&pool->mutex);
while ( pool->head==pool->tail ) /* task queue is empty */
{
pthread_cond_wait(&pool->queue_not_empty,&pool->mutex);
}
task= pool->tasks[pool->head];
pool->head=(pool->head+1)%pool->queue_size;
pthread_mutex_unlock(&pool->mutex);
pthread_cond_signal(&pool->queue_not_full);
if (task.callback) /* normal task */
{
#ifdef DEBUG
param->ntask++;
printf("id-->%d start ntask:%d\n",param->id,param->ntask);
#endif
task.callback( task.arg );
}
else /* abnormal: posion pill */
{
#ifdef DEBUG
printf("id-->%d quit\n",param->id);
#endif
pthread_exit(NULL);
}
}
return NULL;
}
threadpool_t *threadpool_init(int nthread,int queue_size)
{
threadpool_t *pool;
int i,ret;
#ifdef DEBUG
struct take_task_param *param;
#endif
/* Allocate Threadpool Structure & initalize the mutex & cond */
pool=(threadpool_t*)malloc(sizeof(threadpool_t));
if (pool==NULL)
{
fprintf(stderr,"Error: malloc error\n");
exit(0);
}
pool->nthread=nthread;
pool->tid=(pthread_t*)malloc(sizeof(pthread_t)*nthread);
if (pool->tid==NULL)
{
fprintf(stderr,"Error: malloc error\n");
exit(0);
}
/* Allocate the Task Queue */
pool->queue_size=queue_size;
pool->tasks=(struct task*)malloc( sizeof(struct task)*queue_size );
if (pool->tasks==NULL)
{
fprintf(stderr,"Error: malloc error\n");
exit(0);
}
if ((ret=pthread_mutex_init(&pool->mutex,NULL))!=0)
{
fprintf(stderr,"Error: pthread_mutex_init error\n");
fprintf(stderr," %s\n",strerror(errno));
exit(0);
}
if ((ret=pthread_cond_init(&pool->queue_not_empty,NULL))!=0)
{
fprintf(stderr,"Error: pthread_cond_init error\n");
fprintf(stderr," %s\n",strerror(errno));
exit(0);
}
if ((ret=pthread_cond_init(&pool->queue_not_full,NULL))!=0)
{
fprintf(stderr,"Error: pthread_cond_init error\n");
fprintf(stderr," %s\n",strerror(errno));
exit(0);
}
#ifdef DEBUG
param=(struct take_task_param*)malloc(sizeof(struct take_task_param)*nthread);
#endif
pool->head=0;
pool->tail=0;
for (i=0;i<nthread;++i)
{
#ifdef DEBUG
param[i].pool=pool;
param[i].id =i;
param[i].ntask=0;
pthread_create(&((pool->tid)[i]),NULL,take_task,¶m[i]);
#else
pthread_create(&((pool->tid)[i]),NULL,take_task,pool);
#endif
}
return pool;
}
void threadpool_addtask(threadpool_t *pool,void *(*callback)(void *),void *arg)
{
pthread_mutex_lock(&pool->mutex);
while ( pool->head==(pool->tail+1)%pool->queue_size ) /* task is full */
{
pthread_cond_wait(&pool->queue_not_full,&pool->mutex);
}
pool->tasks[pool->tail].callback=callback;
pool->tasks[pool->tail].arg =arg;
pool->tail=(pool->tail+1)%pool->queue_size;
pthread_mutex_unlock(&pool->mutex);
pthread_cond_signal(&pool->queue_not_empty);
}
void threadpool_destroy(threadpool_t *pool)
{
int i;
/* add posion pill */
for (i=0;i<pool->nthread;++i)
{
threadpool_addtask(pool,NULL,NULL);
}
for (i=0;i<pool->nthread;++i)
{
pthread_join(pool->tid[i],NULL);
}
/* release resource */
pthread_mutex_destroy(&pool->mutex);
pthread_cond_destroy(&pool->queue_not_empty);
pthread_cond_destroy(&pool->queue_not_full);
free(pool->tasks);
free(pool->tid);
free(pool);
}
#ifdef __cplusplus
}
#endif