-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.c
More file actions
319 lines (287 loc) · 7.51 KB
/
threadpool.c
File metadata and controls
319 lines (287 loc) · 7.51 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//#define DEBUG
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> /* close */
#include <netdb.h> /* gethostbyname */
#include "threadpool.h"
/////////////////////////////////////////////////////////
//create_threadPool
/////////////////////////////////////////////////////////
threadpool* create_threadpool(int num_threads_in_pool)
{
int i,rc;
if(num_threads_in_pool < 1 || num_threads_in_pool> MAXT_IN_POOL )
{
fprintf(stderr,"incorrect number of threads\n");
return NULL;
}
//create the threadPool
threadpool *pool = (threadpool*)malloc(sizeof(threadpool));
if(pool==NULL)
{
perror("error of malloc to create the pool\n");
return NULL;
}
//ThreadPool's values
if(pthread_mutex_init(&(pool->qlock), NULL)!=0 || pthread_cond_init(&(pool->q_not_empty),NULL)!=0 || pthread_cond_init(&(pool->q_empty), NULL)!=0 )
{
fprintf(stderr, "Error to init mutex or condition\n");
free(pool);
return NULL;
}
pool->num_threads=num_threads_in_pool;
pool->qsize=0;
pool->qhead=NULL;
pool->qtail=NULL;
pool->shutdown=0;
pool->dont_accept=0;
pthread_t *threadsArr = (pthread_t*)malloc(num_threads_in_pool*sizeof(pthread_t));
if(threadsArr==NULL)
{
perror("Error to malloc the arrays's threads \n");
free(pool);
return NULL;
}
for(i=0; i<num_threads_in_pool ; i++)
{
rc = pthread_create (&threadsArr[i], NULL, do_work ,(void*)pool);
if(rc!=0)
{
fprintf(stderr, "Error to create the thread number : %d \n",i);
pool->num_threads=i;
destroy_threadpool(pool);
return NULL;
}
}
pool->threads=threadsArr;
return pool;
}
/////////////////////////////////////////////////////////
//Dispatch
/////////////////////////////////////////////////////////
void dispatch(threadpool* from_me, dispatch_fn dispatch_to_here, void *arg)
{
if(from_me==NULL || dispatch_to_here==NULL)
{
fprintf(stderr, "Your function is NULL or your pointer to pool is NULL\n");
return;
}
//lock the mutex
if(pthread_mutex_lock(&(from_me->qlock))!=0)
{
fprintf(stderr, "Error to lock mutex\n");
return ;
}
//Check if we don't want to destruct the pool
if(from_me->dont_accept==1)
{
if(pthread_mutex_unlock(&(from_me->qlock))!=0)
{
fprintf(stderr, "Error to unlock mutex\n");
return ;
}
return ;
}
//contruct the job
work_t *work = (work_t*)malloc(sizeof(work_t));
if(work==NULL)
{
perror("Error to malloc a new job\n");
return;
}
//Values of the new job
work->routine = dispatch_to_here;
work->arg = arg;
work->next=NULL;
//insert the job into the queue
//manage the list
//if the queue is empty
if(from_me->qsize==0)
{
from_me->qhead=work;
from_me->qtail=work;
from_me->qsize++;
//the queue was empty so we have to update the status to not empty
if(pthread_cond_signal(&(from_me->q_not_empty))!=0)
{
fprintf(stderr, "Error cond_signal\n");
return ;
}
}
else//there is already jobs in the queue
{
from_me->qtail->next=work;
from_me->qtail=work;
from_me->qsize++;
}
//unlock the mutex
if(pthread_mutex_unlock(&(from_me->qlock))!=0)
{
fprintf(stderr, "Error to unlock mutex\n");
return ;
}
}
/////////////////////////////////////////////////////////
//Do_WORK
/////////////////////////////////////////////////////////
void* do_work(void* p)
{
if(p==NULL)
{
fprintf(stderr, "Error of pointer in do_work\n");
return NULL;
}
threadpool *p1=p;
int ret;
while(1)
{
#if defined(DEBUG)
// compiled only when DEBUG exists
printf("id: %lu\n",pthread_self());
#endif
//lock the mutex
if(pthread_mutex_lock(&(p1->qlock))!=0)
{
fprintf(stderr, "Error to lock mutex\n");
return NULL;
}
//if the destroy is active
if(p1->shutdown==1)
{
//unlock the mutex
if(pthread_mutex_unlock(&(p1->qlock))!=0)
{
fprintf(stderr, "Error to unlock mutex\n");
return NULL;
}
return NULL;
}
//if there is no work,the queue is empty so go to sleep
if(p1->qsize==0)
{
if(pthread_cond_wait(&(p1->q_not_empty), &(p1->qlock))!=0)
{
fprintf(stderr, "Error cond_wait\n");
return NULL;
}
}
//if the destroy process was activated when the thread slept
if(p1->shutdown==1)
{
//unlock the mutex
if(pthread_mutex_unlock(&(p1->qlock))!=0)
{
fprintf(stderr, "Error to unlock mutex\n");
return NULL ;
}
return NULL;
}
//prepare the execution of the function
//manage the list
work_t *work = p1->qhead;
if(work!=NULL)
{
p1->qhead=p1->qhead->next;
p1->qsize--;
//if the queue is empty so the tail is NULL
if(p1->qsize==0)
p1->qtail=NULL;
}
//if the work is the last job and we want to destroy
if(p1->qsize == 0 && p1->dont_accept == 1 )
{
//the funct destroy can destroy the pool because the list is now empty
if(pthread_cond_signal(&(p1->q_empty))!=0)
{
fprintf(stderr, "Error cond_signal\n");
return NULL;
}
}
//unlock the mutex
if(pthread_mutex_unlock(&(p1->qlock))!=0)
{
fprintf(stderr, "Error to unlock mutex\n");
return NULL;
}
//Do the work : execute the function
if(work!=NULL)
{
ret = work->routine(work->arg);
free(work);
}
}
}
/////////////////////////////////////////////////////////
//DESTROY
/////////////////////////////////////////////////////////
void destroy_threadpool(threadpool* destroyme)
{
if(destroyme==NULL)
{
fprintf(stderr, "Your pointer to pool is NULL\n");
return ;
}
int i;
//lock the mutex
if(pthread_mutex_lock(&(destroyme->qlock))!=0)
{
fprintf(stderr, "Error to lock mutex\n");
return ;
}
//Don't accept new jobs in the list
destroyme->dont_accept=1;
//if the list is not empty so wait the end of the jobs
if(destroyme->qsize!=0)
{
//wait that the list is empty
if(pthread_cond_wait(&(destroyme->q_empty),&(destroyme->qlock))!=0)
{
fprintf(stderr, "Error cond_wait\n");
return ;
}
}
//the pool is in distruction process
destroyme->shutdown=1;//the dispatch will not insert new jobs
//send signal to all the threads
if(pthread_cond_broadcast(&(destroyme->q_not_empty))!=0)
{
fprintf(stderr, "Error cond_broadcast\n");
return ;
}
//unlock the mutex
if(pthread_mutex_unlock(&(destroyme->qlock))!=0)
{
fprintf(stderr, "Error to unlock mutex\n");
return ;
}
//wait all the threads
for(i=0 ; i< (destroyme->num_threads) ; i++ )
{
int rc = pthread_join(destroyme->threads[i], NULL);
if(rc!=0)
{
perror("ERROR THREAD's JOIN\n");
return ;
}
}
//destroy all mutex and condition
if(pthread_mutex_destroy(&(destroyme->qlock))!=0 || pthread_cond_destroy(&(destroyme->q_not_empty)) !=0 || pthread_cond_destroy(&(destroyme->q_empty))!=0)
{
fprintf(stderr, "Error to destroy all the mutex (mutex , cond)\n");
return ;
}
//free the threads and the pool
free(destroyme->threads);
free(destroyme);
}
/////////////////////////////////////////////////////////
//END
/////////////////////////////////////////////////////////