-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.c
334 lines (283 loc) · 7.86 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
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
320
321
322
323
324
325
326
327
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include "threadpool.h"
//------------------------------------------------------------------------------
//initialize the thread pool struct
threadpool* create_threadpool(int num_threads_in_pool)
{
threadpool *th_pool;
int i;
//check if the "num_threads" input is valid
if(num_threads_in_pool <= 0 || num_threads_in_pool > MAXT_IN_POOL)
{
fprintf(stderr, "ERROR: invalid num of thread\n");
return NULL;
}
//allocate memory for thread pool struct
th_pool = (threadpool*)malloc(sizeof(threadpool));
if(!th_pool)
{
perror("allocated memory");
return NULL;
}
//allocate memory for threads array
th_pool->threads = (pthread_t*) malloc (sizeof(pthread_t) * num_threads_in_pool);
if(!th_pool->threads)
{
perror("allocated memory");
free(th_pool);
return NULL;
}
//initialize the mutex variable inside the struct
if(pthread_mutex_init(&th_pool->qlock, NULL) != 0)
{
fprintf(stderr, "ERROR: pthread mutex initialization failed\n");
free(th_pool->threads);
free(th_pool);
return NULL;
}
//initialize the condition variable inside the struct
if(pthread_cond_init(&th_pool->q_not_empty, NULL) != 0)
{
fprintf(stderr, "ERROR: pthread conditional initialization failed\n");
free(th_pool->threads);
pthread_mutex_destroy(&th_pool->qlock);
free(th_pool);
return NULL;
}
//initialize the condition variable inside the struct
if(pthread_cond_init(&th_pool->q_empty, NULL) != 0)
{
fprintf(stderr, "ERROR: pthread conditional initialization failed\n");
free(th_pool->threads);
pthread_mutex_destroy(&th_pool->qlock);
pthread_cond_destroy(&th_pool->q_not_empty);
free(th_pool);
return NULL;
}
//initialize all the other members inside the struct
th_pool->num_threads = num_threads_in_pool;
th_pool->qsize = 0;
th_pool->qhead = th_pool->qtail = NULL;
th_pool->shutdown = 0;
th_pool->dont_accept = 0;
//create the threads, the thread init function is do_work and its
//argument is the initialized threadpool
for (i = 0;i < num_threads_in_pool;i++)
{
if(pthread_create(&(th_pool->threads[i]), NULL, do_work, th_pool) != 0)
{
fprintf(stderr, "ERROR: pthread create failed\n");
free(th_pool->threads);
pthread_mutex_destroy(&th_pool->qlock);
pthread_cond_destroy(&th_pool->q_not_empty);
free(th_pool);
return NULL;
}
}
//finally return this init struct
return th_pool;
}
//------------------------------------------------------------------------------
//method to enter a "job" of type work_t into the queue
void dispatch(threadpool* from_me, dispatch_fn dispatch_to_here, void *arg)
{
//check if threadpool or function exist
if(!from_me || !dispatch_to_here)
{
fprintf(stderr, "ERROR: threadpool or function is NULL \n");
return;
}
//allocate memory for work_t
work_t *currWork = (work_t*)malloc(sizeof(work_t));
if(!currWork)
{
perror("allocated memory");
return;
}
//initialize work_t struct
currWork->routine = dispatch_to_here;
currWork->arg = arg;
currWork->next = NULL;
//lock other threads from doing this code
if(pthread_mutex_lock(&(from_me->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread mutex lock failed\n");
free(currWork);
return;
}
//check if dont accept new items is turn on
if(from_me->dont_accept)
{
if(pthread_mutex_unlock(&(from_me->qlock)) != 0)
fprintf(stderr, "ERROR: pthread mutex unlock failed\n");
free(currWork);
return;
}
//if we want add this work_t item to queue and queue is empty
if(from_me->qsize == 0)
{
from_me->qhead = from_me->qtail = currWork;
from_me->qsize++;
//signal to other sleeping threads that queue is not empty
if(pthread_cond_signal(&(from_me->q_not_empty)) != 0)
{
fprintf(stderr, "ERROR: pthread conditional signal failed\n");
return;
}
}
//if we want add this work_t item to queue and queue is not empty
else
{
from_me->qtail->next = currWork;
from_me->qtail = currWork;
from_me->qsize++;
}
//finally unlock mutex
if(pthread_mutex_unlock(&(from_me->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread mutex unlock failed\n");
return;
}
}
//------------------------------------------------------------------------------
// The work function of the thread
void* do_work(void* p)
{
//check if argument p exist
if(!p)
return NULL;
threadpool *th_pool = (threadpool*)p;
work_t* currWork;
while(1)
{
//lock the critical code
if(pthread_mutex_lock(&(th_pool->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread mutex lock failed\n");
return NULL;
}
while(th_pool->qsize == 0)
{
//check to see if in shutdown mode.
if(th_pool->shutdown)
{
if(pthread_mutex_unlock(&(th_pool->qlock)) != 0)
fprintf(stderr, "ERROR: pthread mutex unlock failed\n");
return NULL;
}
//wait until the condition says its no emtpy
if(pthread_cond_wait(&(th_pool->q_not_empty),&(th_pool->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread cinditional wait failed\n");
return NULL;
}
//check again to see if in shutdown mode.
if(th_pool->shutdown)
{
if(pthread_mutex_unlock(&(th_pool->qlock)) != 0)
fprintf(stderr, "ERROR: pthread mutex unlock failed\n");
return NULL;
}
}
//get the job
currWork = th_pool->qhead;
th_pool->qsize--;
//reorder hte queue
if(th_pool->qsize == 0)
{
th_pool->qhead = th_pool->qtail = NULL;
}
else if(th_pool->qsize > 0){
th_pool->qhead = currWork->next;
}
//if the queue became empty so signal that queue is empty
if(th_pool->qsize == 0 && th_pool->dont_accept)
{
if(pthread_cond_signal(&(th_pool->q_empty)) != 0)
{
fprintf(stderr, "ERROR: pthread conditional signal failed\n");
free(currWork);
return NULL;
}
}
//unlock mutex
if(pthread_mutex_unlock(&(th_pool->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread mutex unlock failed\n");
free(currWork);
return NULL;
}
//finally excute the function and free the work_t struct
currWork->routine(currWork->arg);
free(currWork);
}
}
//------------------------------------------------------------------------------
/*method to kills the threadpool, causing
all threads in it to commit suicide, and then
frees all the memory associated with the threadpool*/
void destroy_threadpool(threadpool* destroyme)
{
//check if destroyme exist
if(!destroyme)
return;
int i;
void* retval;
//unlock this code from other threads
if(pthread_mutex_lock(&(destroyme->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread mutex lock failed\n");
return;
}
//set dont accept flag to 1
destroyme->dont_accept = 1;
while(destroyme->qsize > 0)
{
//wait for queue to become empty
if(pthread_cond_wait(&(destroyme->q_empty), &(destroyme->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread conditional wait failed\n");
return;
}
}
//set shutdown flag to 1
destroyme->shutdown = 1;
//Signal threads that wait on empty queue, so they can wake up, see shutdown flag and exit.
if(pthread_cond_broadcast(&(destroyme->q_not_empty)) != 0)
{
fprintf(stderr, "ERROR: pthread conditional broadcast failed\n");
return;
}
if(pthread_mutex_unlock(&(destroyme->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread mutex unlock failed\n");
return;
}
for(i = 0; i < destroyme->num_threads ; i++)
{
pthread_join(destroyme->threads[i], &retval);
}
//destroy the mutex and conditions
if(pthread_mutex_destroy(&(destroyme->qlock)) != 0)
{
fprintf(stderr, "ERROR: pthread mutex destroy failed\n");
return;
}
if(pthread_cond_destroy(&(destroyme->q_empty)) != 0)
{
fprintf(stderr, "ERROR: pthread conditional destroy failed\n");
return;
}
if(pthread_cond_destroy(&(destroyme->q_not_empty)) != 0)
{
fprintf(stderr, "ERROR: pthread conditional destroy failed\n");
return;
}
//free threads array and finally the threadpool struct itself
free(destroyme->threads);
free(destroyme);
}