-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.c
More file actions
145 lines (141 loc) · 3.58 KB
/
threadpool.c
File metadata and controls
145 lines (141 loc) · 3.58 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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <zconf.h>
#include "threadpool.h"
int append(threadpool *pool, work_t* work);
work_t *pop_first(threadpool *pool);
void usage_error()
{
printf("Usage: threadpool <pool-size> <max-number-of-jobs>\n");
exit(EXIT_FAILURE);
}
void error(char* msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
threadpool* create_threadpool(int num_threads_in_pool)
{
int i;
if(num_threads_in_pool>MAXT_IN_POOL || num_threads_in_pool<=0)
usage_error();
threadpool *pool=malloc(sizeof(threadpool));
if(pool == NULL)
error("ERROR malloc");
pool->num_threads=num_threads_in_pool;
pool->qsize=0;
pool->threads=malloc(num_threads_in_pool * sizeof(pthread_t));
if(pool->threads == NULL)
{
free(pool);
error("ERROR malloc");
}
pool->qhead=NULL;
pool->qtail=NULL;
pool->shutdown=0;
pool->dont_accept=0;
pthread_mutex_init(&pool->qlock, NULL);
pthread_cond_init(&pool->q_not_empty, NULL);
pthread_cond_init(&pool->q_empty, NULL);
for(i=0; i < pool->num_threads; ++i)
{
pthread_create(pool->threads + i, NULL, do_work, (void*)pool);
}
return pool;
}
void dispatch(threadpool* from_me, dispatch_fn dispatch_to_here, void *arg)
{
work_t *work;
if(from_me->dont_accept)
return;
work=malloc(sizeof(work_t));
if(work==NULL)
{
destroy_threadpool(from_me);
error("ERROR malloc");
}
work->arg=arg;
work->routine=dispatch_to_here;
pthread_mutex_lock(&from_me->qlock);
if(append(from_me, work)<0) //add a job to the queue
{
destroy_threadpool(from_me);
exit(EXIT_FAILURE);
}
pthread_cond_signal(&from_me->q_not_empty);
pthread_mutex_unlock(&from_me->qlock);
}
int append(threadpool *pool, work_t* work)
{
if(pool==NULL || work==NULL)
return -1;
work->next=NULL;
if (pool->qhead == NULL)
pool->qhead=work;
else pool->qtail->next=work;
pool->qtail=work;
pool->qsize++;
return 0;
}
work_t *pop_first(threadpool *pool)
{
work_t* tmp;
if (pool == NULL)
return NULL;
tmp = pool->qhead;
if (tmp == NULL)
return NULL;
pool->qhead = pool->qhead->next;
if(pool->qhead==NULL)
pool->qtail=NULL;
pool->qsize--;
return tmp;
}
void* do_work(void* p)
{
threadpool *pool;
work_t *to_do;
pool=(threadpool*)p;
while(1)
{
pthread_mutex_lock(&pool->qlock);
if(pool->shutdown)
{
pthread_mutex_unlock(&pool->qlock);
return NULL;
}
if(pool->qsize==0)
pthread_cond_wait(&pool->q_not_empty, &pool->qlock);
if(pool->shutdown)
{
pthread_mutex_unlock(&pool->qlock);
return NULL;
}
if(pool->qsize==0)
continue;
to_do=pop_first(pool); //take a job from the queue
if(pool->dont_accept && pool->qsize==0)
{
pthread_cond_signal(&pool->q_empty);
}
pthread_mutex_unlock(&pool->qlock);
(to_do->routine)(to_do->arg);
free(to_do);
}
}
void destroy_threadpool(threadpool* destroyme)
{
pthread_mutex_lock(&destroyme->qlock);
destroyme->dont_accept=1;
pthread_cond_wait(&destroyme->q_empty, &destroyme->qlock);
destroyme->shutdown=1;
pthread_cond_broadcast(&destroyme->q_not_empty);
pthread_mutex_unlock(&destroyme->qlock);
for(int i=0; i<destroyme->num_threads; i++)
{
pthread_join(destroyme->threads[i], NULL);
}
free(destroyme->threads);
free(destroyme);
}