-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.h
More file actions
460 lines (395 loc) · 12.2 KB
/
Copy paththreadpool.h
File metadata and controls
460 lines (395 loc) · 12.2 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#ifndef THREADPOOL_H
#define THREADPOOL_H
#if defined(__linux__)
#include <sys/prctl.h>
#endif
#include <signal.h>
static volatile int threads_keepalive;
static volatile int threads_on_hold;
/* #define bool int */
typedef int bool;
/* queue status and conditional variable */
typedef struct staconv
{
pthread_mutex_t mutex; /* contorl the threads in the pool */
pthread_cond_t cond; /* false:no task, ture:has task */
int status;
}staconv;
/* task */
typedef struct task
{
struct task *next; /* pointer to next task */
void (*function)(void *arg); /* pointer of function */
void *arg; /* pointer of function argument */
}task;
/* teak queue */
typedef struct taskqueue
{
pthread_mutex_t mutex; /* for read/write command queue */
task *front; /* point the head of the queue */
task *rear; /* point the tail of the queue */
staconv *has_jobs; /* block the thread based on status */
int len; /* the number of task in the queue */
}taskqueue;
/* thread */
typedef struct thread
{
int id; /* thread id */
pthread_t pthread; /* POSIX thread */
struct threadpool *pool; /* band with thread pool */
}thread;
/* thread pool */
typedef struct threadpool
{
thread **threads; /* threads pointer array */
volatile int num_threads; /* the number of threads in the pool */
volatile int num_working; /* the working number of thread */
pthread_mutex_t thcount_lock; /* threadpool mutex for the previous two variate */
pthread_cond_t threads_all_idle; /* the conition variate for destory thread */
taskqueue queue; /* task queue */
volatile bool is_alive; /* the threadpool is alive or not */
}threadpool;
int init_taskqueue(taskqueue *queue);
int create_thread(threadpool *pool, thread **pthread, int id);
threadpool* initThreadPool(int num_threads);
void push_taskqueue(taskqueue *queue, task *curtask);
void addTask2ThreadPool(threadpool *pool, void (*function_p)(void*), void* arg_p);
void waitThreadPool(threadpool *pool);
static void bsem_init(staconv *bsem_p, int value);
static void bsem_reset(staconv *bsem_p);
void destory_taskqueue(taskqueue *queue);
static void bsem_post_all(staconv *bsem_p);
static void thread_destroy (thread* thread_p);
void distoryThreadPool(threadpool *pool);
int getNumofThreadWorking(threadpool *pool);
static void bsem_post(staconv *bsem_p);
task* take_taskqueue(taskqueue *queue);
void bsem_wait(staconv* bsem_p);
static void thread_hold(int sig_id);
void* thread_do(thread* pthread);
/* initialize the task queue */
int init_taskqueue(taskqueue *queue)
{
pthread_mutex_init(&(queue->mutex), NULL);
queue->front = NULL;
queue->rear = NULL;
queue->len = 0;
queue->has_jobs = (struct staconv *)malloc(sizeof(struct staconv)); /* malloc for the initial */
if (queue->has_jobs == NULL)
{
return 0;
}
bsem_init(queue->has_jobs, 0);
// pthread_mutex_init(&(queue->has_jobs->mutex), NULL);
// pthread_cond_init(&(queue->has_jobs->cond), NULL);
// queue->has_jobs->status = 0; /* false means no task */
return 1;
}
/* create thread */
int create_thread(threadpool *pool, thread **pthread, int id)
{
/* malloc for thread */
*pthread = (struct thread *)malloc(sizeof(struct thread));
if (*pthread == NULL)
{
printf("create_thread(): Could not allocate memory for thread\n");
return -1;
}
/* set the attribute of the thread */
(*pthread)->pool = pool;
(*pthread)->id = id;
/* create thread */
pthread_create(&(*pthread)->pthread, NULL, (void *)thread_do, (*pthread));
pthread_detach((*pthread)->pthread);
return 0;
}
/* the initialization fuction of threadpool */
threadpool* initThreadPool(int num_threads)
{
threads_on_hold = 0;
threads_keepalive = 1;
/* create the space of threadpool */
threadpool *pool;
pool = (threadpool *)malloc(sizeof(threadpool));
if (pool == NULL)
{
printf("thpool_init(): Could not allocate memory for thread pool\n");
return NULL;
}
pool->num_threads = 0;
pool->num_working = 0;
pthread_mutex_init(&(pool->thcount_lock), NULL);
pthread_cond_init(&pool->threads_all_idle, NULL);
/* initialize the task queue */
init_taskqueue(&pool->queue);
/* create thread array */
pool->threads = (struct thread **)malloc(sizeof(struct thread *) * num_threads);
/* create thread */
for (int i = 0; i < num_threads; i++)
{
create_thread(pool, &(pool->threads[i]), i); /* i:thread id */
}
// /* Thread init */
// for (int n = 0; n < num_threads; n++)
// {
// thread_init(pool, &pool->threads[n], n);
// }
/* waiting all the threads been created, oprate "pool->num_threads++" in each thread function */
/* so that here is a busy wait, waiting all the threads been created, run the block code to return */
while (pool->num_threads != num_threads);
return pool;
}
/* add task to queue */
void push_taskqueue(taskqueue *queue, task *curtask)
{
pthread_mutex_lock(&queue->mutex);
curtask->next = NULL;
if (queue->len == 0)
{
queue->front = curtask;
queue->rear = curtask;
}
else
{
queue->rear->next = curtask;
queue->rear = curtask;
}
queue->len++;
bsem_post(queue->has_jobs);
// queue->has_jobs->status = 1; /* ture means have job */
pthread_mutex_unlock(&queue->mutex);
}
/* add task to threadpool */
void addTask2ThreadPool(threadpool *pool, void (*function_p)(void*), void* arg_p)
{
task* newjob;
newjob = (struct task*)malloc(sizeof(struct task));
/* add function and argument */
newjob->function = function_p;
newjob->arg = arg_p;
/* add task to queue */
push_taskqueue(&pool->queue, newjob);
}
/* waiting al the task finished */
void waitThreadPool(threadpool *pool)
{
pthread_mutex_lock(&pool->thcount_lock);
while (pool->queue.len || pool->num_working)
{
pthread_cond_wait(&pool->threads_all_idle, &pool->thcount_lock);
}
pthread_mutex_unlock(&pool->thcount_lock);
}
/* Init semaphore to 1 or 0 copied*/
static void bsem_init(staconv *bsem_p, int value)
{
if (value < 0 || value > 1) {
// err("bsem_init(): Binary semaphore can take only values 1 or 0");
exit(1);
}
pthread_mutex_init(&(bsem_p->mutex), NULL);
pthread_cond_init(&(bsem_p->cond), NULL);
bsem_p->status = value;
}
/* Reset semaphore to 0 copied*/
static void bsem_reset(staconv *bsem_p)
{
bsem_init(bsem_p, 0);
}
void destory_taskqueue(taskqueue *queue)
{
while (queue->len)
{
free(take_taskqueue(queue));
}
queue->front = NULL;
queue->rear = NULL;
bsem_reset(queue->has_jobs);
free(queue->has_jobs);
}
/* Post to all threads, copied*/
static void bsem_post_all(staconv *bsem_p)
{
pthread_mutex_lock(&bsem_p->mutex);
bsem_p->status = 1;
pthread_cond_broadcast(&bsem_p->cond);
pthread_mutex_unlock(&bsem_p->mutex);
}
/* copied */
static void thread_destroy (thread* thread_p)
{
free(thread_p);
}
/* distory the threadpool */
void distoryThreadPool(threadpool *pool)
{
/* No need to destory if it's NULL */
if (pool == NULL) return ;
volatile int threads_total = pool->num_threads;
/* End each thread 's infinite loop */
threads_keepalive = 0;
/* Give one second to kill idle threads */
double TIMEOUT = 1.0;
time_t start, end;
double tpassed = 0.0;
time (&start);
while (tpassed < TIMEOUT && pool->num_threads){
bsem_post_all(pool->queue.has_jobs);
time (&end);
tpassed = difftime(end,start);
}
/* Poll remaining threads */
while (pool->num_threads){
bsem_post_all(pool->queue.has_jobs);
sleep(1);
}
/* Job queue cleanup */
destory_taskqueue(&pool->queue);
/* Deallocs */
int n;
for (n=0; n < threads_total; n++){
thread_destroy(pool->threads[n]);
}
free(pool->threads);
free(pool);
// /* if the task queue is not empty, waiting */
// while (pool->queue.len != 0 && pool->queue.has_jobs->status == 0);
// /* distory the task queue */
// destory_taskqueue(&pool->queue);
// /* distory the thread pointer array, and free the memory of threadpool */
// free(pool->threads);
// free(pool);
}
/* get the number of working thread in the pool */
int getNumofThreadWorking(threadpool *pool)
{
return pool->num_working;
}
/* Post to at least one thread copied */
static void bsem_post(staconv *bsem_p)
{
pthread_mutex_lock(&bsem_p->mutex);
bsem_p->status = 1;
pthread_cond_signal(&bsem_p->cond);
pthread_mutex_unlock(&bsem_p->mutex);
}
task* take_taskqueue(taskqueue *queue)
{
pthread_mutex_lock(&queue->mutex);
task* job_p = queue->front;
switch(queue->len){
case 0: /* if no jobs in queue */
break;
case 1: /* if one job in queue */
queue->front = NULL;
queue->rear = NULL;
queue->len = 0;
break;
default: /* if >1 jobs in queue */
queue->front = job_p->next;
queue->len--;
/* more than one job in queue -> post it */
bsem_post(queue->has_jobs);
}
pthread_mutex_unlock(&queue->mutex);
return job_p;
// nwejob = queue->front;
// queue->front = first->next;
// len--;
// return first;
}
/* Wait on semaphore until semaphore has value 0 */
void bsem_wait(staconv* bsem_p)
{
pthread_mutex_lock(&bsem_p->mutex);
while (bsem_p->status != 1) {
pthread_cond_wait(&bsem_p->cond, &bsem_p->mutex);
}
bsem_p->status = 0;
pthread_mutex_unlock(&bsem_p->mutex);
}
/* Sets the calling thread on hold copied*/
static void thread_hold(int sig_id)
{
(void)sig_id;
threads_on_hold = 1;
while (threads_on_hold){
sleep(1);
}
}
/* the logical fuction of running thread */
void* thread_do(thread* pthread)
{
/* set the name of thread */
char thread_name[128] = {0};
sprintf(thread_name, "thread-pool-%d", pthread->id); /* not sprint */
#if defined(__linux__)
/* Use prctl instead to prevent using _GNU_SOURCE flag and implicit declaration */
prctl(PR_SET_NAME, thread_name);
#elif defined(__APPLE__) && defined(__MACH__)
pthread_setname_np(thread_name);
#else
err("thread_do(): pthread_setname_np is not supported on this system");
#endif
// prctl(PR_SET_NAME, thread_name);
/* obtain the threadpool */
threadpool* pool = pthread->pool;
/* Register signal handler copied */
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = thread_hold;
if (sigaction(SIGUSR1, &act, NULL) == -1) {
// err("thread_do(): cannot handle SIGUSR1");
}
/* while initialize the threadpool, count the number of finishing creation, do "pool->num_thread++" */
pthread_mutex_lock(&pool->thcount_lock);
(pool->num_threads)++;
pthread_mutex_unlock(&pool->thcount_lock);
/* the thread can be repeat, until pool->is_alive became false */
while (threads_keepalive)
{
/* if the task queue still have task, keep runing, else block */
/* if (pool->queue->len == 0)
{
waitThreadPool(pool);
} */
bsem_wait(pool->queue.has_jobs);
if (threads_keepalive)
{
/* the thread is working, need count the working number */
pthread_mutex_lock(&(pool->thcount_lock));
(pool->num_working)++;
pthread_mutex_unlock(&(pool->thcount_lock));
/* get the first task and run */
void (*func)(void*);
void* arg;
/* take_taskqueue get the task from the head of the queue, and delete the task in the queue */
task* curtask = take_taskqueue(&pool->queue);
if (curtask)
{
func = curtask->function;
arg = curtask->arg;
/* do the task */
func(arg);
/* release the task */
free(curtask);
}
/* thread is finished, need to change the num of working threads */
pthread_mutex_lock(&pool->thcount_lock);
pool->num_working--;
if (!pool->num_working)
{
pthread_cond_signal(&pool->threads_all_idle);
}
pthread_mutex_unlock(&pool->thcount_lock);
/* if the num of working thread is 0, should run the thread which is block in the waitThreadPool() */
}
}
/* the thread is going to exit, need to change the num of thread in the pool */
pthread_mutex_lock(&pool->thcount_lock);
pool->num_threads--;
pthread_mutex_unlock(&pool->thcount_lock);
return NULL;
}
#endif