-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread_pool.c
More file actions
453 lines (388 loc) · 11.8 KB
/
Copy paththread_pool.c
File metadata and controls
453 lines (388 loc) · 11.8 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
/**
* @file thread_pool.c
* @version 1.0
* @author boyce <boyce.ywr@gmail.com>
* @author Tristan Lee <tristan.lee@qq.com>
* @brief A Thread Pool implementation on Pthread
*
*
* Change Logs:
* Date Author Notes
* 2011-07-25 boyce
* 2011-08-04 boyce
* 2012-05-14 boyce
* 2016-01-13 Tristan fix bugs
* 2016-02-25 Tristan fix manage thread exit problem
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <sys/time.h>
#include "thread_pool.h"
//#define __DEBUG__
#ifdef __DEBUG__
#define DEBUG(format,...) printf(format,##__VA_ARGS__)
#else
#define DEBUG(format,...)
#endif
static int tp_init(TpThreadPool *pTp);
static TpThreadInfo *tp_add_thread(TpThreadPool *pTp, process_job proc_fun, void *job);
static int tp_delete_thread(TpThreadPool *pTp);
static int tp_get_tp_status(TpThreadPool *pTp);
static void *tp_work_thread(void *pthread);
static void *tp_manage_thread(void *pthread);
static void afterms(struct timespec *timeout,unsigned long ms);
/**
* user interface. creat thread pool.
* para:
* num: min thread number to be created in the pool
* return:
* thread pool struct instance be created successfully
*/
TpThreadPool *tp_create(unsigned min_num, unsigned max_num) {
TpThreadPool *pTp;
pTp = (TpThreadPool*) malloc(sizeof(TpThreadPool));
memset(pTp, 0, sizeof(TpThreadPool));
//init member var
pTp->min_th_num = min_num;
pTp->max_th_num = max_num;
tp_init(pTp);
return pTp;
}
/**
* member function reality. thread pool init function.
* para:
* pTp: thread pool struct instance ponter
* return:
* true: successful; false: failed
*/
static int tp_init(TpThreadPool *pTp) {
int err;
unsigned i;
TpThreadInfo *pThi;
//init_queue(&pTp->idle_q, NULL);
pTp->busy_q = ts_queue_create();
pTp->idle_q = ts_queue_create();
pTp->busy_threshold = BUSY_THRESHOLD;
pTp->manage_interval = MANAGE_INTERVAL;
//create work thread and init work thread info
for (i = 0; i < pTp->min_th_num; i++) {
pThi = (TpThreadInfo*) malloc(sizeof(TpThreadInfo));
pThi->tp_pool = pTp;
pThi->stop_flag = FALSE;
pThi->event_sem = (sem_t*)malloc(sizeof(sem_t));
sem_init(pThi->event_sem, 0, 0);
pThi->proc_fun = NULL;
pThi->arg = NULL;
ts_queue_enq_data(pTp->idle_q, pThi);
err = pthread_create(&pThi->thread_id, NULL, tp_work_thread, pThi);
if (0 != err) {
perror("tp_init: create work thread failed.");
ts_queue_destroy(pTp->busy_q);
ts_queue_destroy(pTp->idle_q);
return -1;
}
}
//create manage thread and init manage thread info
pThi = (TpThreadInfo*) malloc(sizeof(TpThreadInfo));
pThi->tp_pool = pTp;
pThi->stop_flag = FALSE;
pThi->event_sem = (sem_t*)malloc(sizeof(sem_t));
sem_init(pThi->event_sem, 0, 0);
pThi->proc_fun = NULL;
pThi->arg = NULL;
err = pthread_create(&pThi->thread_id, NULL, tp_manage_thread, pThi);
if (0 != err) {//clear_queue(&pTp->idle_q);
ts_queue_destroy(pTp->busy_q);
ts_queue_destroy(pTp->idle_q);
fprintf(stderr, "tp_init: creat manage thread failed\n");
return 0;
}
pTp->manage = pThi;
#if 0
//wait for all threads are ready
while(i++ < pTp->cur_th_num){
pthread_mutex_lock(&pTp->tp_lock);
pthread_cond_wait(&pTp->tp_cond, &pTp->tp_lock);
pthread_mutex_unlock(&pTp->tp_lock);
}
DEBUG("All threads are ready now\n");
#endif
return 0;
}
/**
* member function reality. thread pool entirely close function.
* para:
* pTp: thread pool struct instance ponter
* return:
*/
void tp_close(TpThreadPool *pTp, BOOL wait) {
TpThreadInfo *pThi;
pthread_t thread_id;
//close manage thread first
DEBUG("close manage thread\n");
thread_id = pTp->manage->thread_id; //:NOTE: get thread_id before post event
pTp->manage->stop_flag = TRUE;
sem_post(pTp->manage->event_sem);
pthread_join(thread_id, NULL);
DEBUG("total number of threads: %d\n", ts_queue_count(pTp->busy_q)+ts_queue_count(pTp->idle_q));
if (wait) {
while (!ts_queue_is_empty(pTp->busy_q)) {
pThi = (TpThreadInfo *)ts_queue_deq_data(pTp->busy_q);
thread_id = pThi->thread_id; //:NOTE: get thread_id before post event
pThi->stop_flag = TRUE;
sem_post(pThi->event_sem);
DEBUG("join thread 0x%08x\n", (unsigned)thread_id);
if(0 != pthread_join(thread_id, NULL)){
perror("pthread_join");
}
//DEBUG("join a thread success.\n");
}
while (!ts_queue_is_empty(pTp->idle_q)) {
pThi = (TpThreadInfo *)ts_queue_deq_data(pTp->idle_q);
thread_id = pThi->thread_id; //:NOTE: get thread_id before post event
pThi->stop_flag = TRUE;
sem_post(pThi->event_sem);
DEBUG("join thread 0x%08x\n", (unsigned)thread_id);
if(0 != pthread_join(thread_id, NULL)){
perror("pthread_join");
}
//DEBUG("join a thread success.\n");
}
DEBUG("join all thread success.\n");
} else {
//close work thread
while (!ts_queue_is_empty(pTp->busy_q)) {
pThi = (TpThreadInfo *)ts_queue_deq_data(pTp->busy_q);
pThi->stop_flag = TRUE;
sem_post(pThi->event_sem);
}
while (!ts_queue_is_empty(pTp->idle_q)) {
pThi = (TpThreadInfo *)ts_queue_deq_data(pTp->idle_q);
pThi->stop_flag = TRUE;
sem_post(pThi->event_sem);
}
}
//clear_queue(&pTp->idle_q);
ts_queue_destroy(pTp->busy_q);
ts_queue_destroy(pTp->idle_q);
free(pTp);
}
/**
* member function reality. main interface opened.
* after getting own worker and job, user may use the function to process the task.
* para:
* pTp: thread pool struct instance ponter
* worker: user task reality.
* job: user task para
* return:
*/
int tp_process_job(TpThreadPool *pTp, process_job proc_fun, void *arg) {
TpThreadInfo *pThi ;
if (!pTp || !proc_fun) return -1;
pThi = (TpThreadInfo *) ts_queue_deq_data(pTp->idle_q);
if(pThi){
DEBUG("Fetch a thread from pool.\n");
pThi->proc_fun = proc_fun;
pThi->arg = arg;
ts_queue_enq_data(pTp->busy_q, pThi);
//let the thread to deal with this job
DEBUG("wake up thread %u\n", (unsigned)pThi->thread_id);
sem_post(pThi->event_sem);
}
else{
//if all current thread are busy, new thread is created here
if(!(pThi = tp_add_thread(pTp, proc_fun, arg))){
DEBUG("The thread pool is full, no more thread available.\n");
return -1;
}
/* should I wait? */
//pthread_mutex_lock(&pTp->tp_lock);
//pthread_cond_wait(&pTp->tp_cond, &pTp->tp_lock);
//pthread_mutex_unlock(&pTp->tp_lock);
DEBUG("No more idle thread, create a new thread\n");
}
return 0;
}
/**
* member function reality. add new thread into the pool and run immediately.
* para:
* pTp: thread pool struct instance ponter
* proc_fun:
* job:
* return:
* pointer of TpThreadInfo
*/
static TpThreadInfo *tp_add_thread(TpThreadPool *pTp, process_job proc_fun, void *arg) {
int err;
TpThreadInfo *pThi;
if (pTp->max_th_num <= ts_queue_count(pTp->busy_q)+ts_queue_count(pTp->idle_q)){
return NULL;
}
//malloc new thread info struct
pThi = (TpThreadInfo*) malloc(sizeof(TpThreadInfo));
//init status, queue into busy_q, only new process job will call this function
pThi->tp_pool = pTp;
pThi->stop_flag = FALSE;
pThi->event_sem = (sem_t*)malloc(sizeof(sem_t));
sem_init(pThi->event_sem, 0, 0);
pThi->proc_fun = proc_fun;
pThi->arg = arg;
ts_queue_enq_data(pTp->busy_q, pThi);
err = pthread_create(&pThi->thread_id, NULL, tp_work_thread, pThi);
if (0 != err) {
perror("tp_add_thread: pthread_create");
ts_queue_rm_data(pTp->busy_q, pThi);
sem_destroy(pThi->event_sem);
free(pThi->event_sem);
free(pThi);
return NULL;
}
sem_post(pThi->event_sem);
return pThi;
}
/**
* member function reality. delete idle thread in the pool.
* only delete last idle thread in the pool.
* para:
* pTp: thread pool struct instance ponter
* return:
* true: successful; false: failed
*/
int tp_delete_thread(TpThreadPool *pTp) {
TpThreadInfo *pThi;
pthread_t thread_id;
//current thread num can't < min thread num
if (ts_queue_count(pTp->busy_q)+ts_queue_count(pTp->idle_q) <= pTp->min_th_num)
return -1;
//all threads are busy
pThi = (TpThreadInfo *) ts_queue_deq_data(pTp->idle_q);
if(!pThi)
return -1;
DEBUG("Delete idle thread 0x%08x\n", (unsigned)pThi->thread_id);
//close the idle thread
thread_id = pThi->thread_id; //:NOTE: get thread_id before post event
pThi->stop_flag = TRUE;
sem_post(pThi->event_sem);
pthread_join(thread_id, NULL);
return 0;
}
/**
* internal interface. real work thread.
* @params:
* arg: args for this method
* @return:
* none
*/
static void *tp_work_thread(void *arg) {
TpThreadInfo *pThi = (TpThreadInfo *) arg;
TpThreadPool *pTp = pThi->tp_pool;
#if 0
//wake up waiting thread, notify it I am ready
pthread_cond_signal(&pTp->tp_cond);
#endif
while (1) {
//wait event for processing real job.
sem_wait(pThi->event_sem);
//process
if(pThi->proc_fun){
DEBUG("thread 0x%08x is running\n", (unsigned)pThi->thread_id);
pThi->proc_fun(pThi->arg);
//thread state should be set idle after work
pThi->proc_fun = NULL;
//pThi->arg = NULL;
}
//stop
if(pThi->stop_flag){
DEBUG("thread 0x%08x stop\n", (unsigned)pThi->thread_id);
break;
}
//work thread is idle now, we must check stop_flag before
//accessing pTp in case of pTp already freed by tp_close()
if (ts_queue_rm_data(pTp->busy_q, pThi) != NULL) {
ts_queue_enq_data(pTp->idle_q, pThi);
}
}
DEBUG("thread 0x%08x exit\n", (unsigned)pThi->thread_id);
sem_destroy(pThi->event_sem);
free(pThi->event_sem);
free(pThi);
return NULL;
}
/**
* member function reality. get current thread pool status:idle, normal, busy, .etc.
* para:
* pTp: thread pool struct instance ponter
* return:
* 0: idle; 1: normal or busy(don't process)
*/
int tp_get_tp_status(TpThreadPool *pTp) {
float busy_rate = 0.0;
unsigned busy_nr, idle_nr;
//get busy thread number
busy_nr = ts_queue_count(pTp->busy_q);
idle_nr = ts_queue_count(pTp->idle_q);
busy_rate = busy_nr;
busy_rate = busy_rate / (busy_nr+idle_nr);
DEBUG("Thread pool status, total num: %d, busy num: %d, idle num: %d\n", \
(busy_nr+idle_nr), busy_nr, idle_nr);
if (busy_rate < pTp->busy_threshold)
return 0;//idle status
else
return 1;//busy or normal status
}
/**
* internal interface. manage thread pool to delete idle thread.
* para:
* pthread: thread struct ponter
* return:
*/
static void *tp_manage_thread(void *arg) {
TpThreadInfo *pThi = (TpThreadInfo *) arg;
TpThreadPool *pTp = pThi->tp_pool;
while (1) {
struct timespec abs_timeout;
afterms(&abs_timeout, pTp->manage_interval*1000);
sem_timedwait(pThi->event_sem, &abs_timeout);
if(pThi->stop_flag){
break;
}
if (tp_get_tp_status(pTp) == 0) {
tp_delete_thread(pTp);
}
}
DEBUG("manage thread 0x%08x exit\n", (unsigned)pThi->thread_id);
sem_destroy(pThi->event_sem);
free(pThi->event_sem);
free(pThi);
return NULL;
}
float tp_get_busy_threshold(TpThreadPool *pTp){
return pTp->busy_threshold;
}
int tp_set_busy_threshold(TpThreadPool *pTp, float bt){
if(bt <= 1.0 && bt > 0.) {
pTp->busy_threshold = bt;
return 0;
}
return -1;
}
unsigned tp_get_manage_interval(TpThreadPool *pTp){
return pTp->manage_interval;
}
int tp_set_manage_interval(TpThreadPool *pTp, unsigned mi){
pTp->manage_interval = mi;
return 0;
}
static void afterms(struct timespec *timeout,unsigned long ms)
{
struct timeval tt;
gettimeofday(&tt,NULL);
timeout->tv_sec = tt.tv_sec+ms/1000;
timeout->tv_nsec = tt.tv_usec*1000 + (ms%1000) * 1000 *1000; //ÕâÀï¿ÉÄÜÔì³ÉÄÉÃë>1000 000 000
timeout->tv_sec += timeout->tv_nsec/(1000 * 1000 *1000);
timeout->tv_nsec %= (1000 * 1000 *1000);
}