-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.c
More file actions
205 lines (194 loc) · 5.6 KB
/
threadpool.c
File metadata and controls
205 lines (194 loc) · 5.6 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
#include "threadpool.h"
#include <stdio.h>
#include <stdlib.h>
#define MEMORY_FAILED 1
#define MUTEX_INIT_FAILED 2
#define COND_INIT_FAILED 3
#define THREAD_CREATE_FAILED 4
void err(int err_type, void *threadpool, void *threads)
{
if (threadpool)
free(threadpool);
if (threads)
free(threads);
switch (err_type)
{
case MEMORY_FAILED:
perror("ERROR: MEMORY_ALOC_FAILED");
break;
case MUTEX_INIT_FAILED:
perror("ERROR: MUTEX_INIT_FAILED");
break;
case COND_INIT_FAILED:
perror("ERROR: COND_INIT_FAILED");
break;
case THREAD_CREATE_FAILED:
perror("ERROR: THREAD_CREATE_FAILED");
break;
}
}
threadpool *create_threadpool(int num_threads_in_pool)
{
if (num_threads_in_pool < 1 || num_threads_in_pool > MAXT_IN_POOL)
return NULL;
threadpool *t = (threadpool *)calloc(1, sizeof(threadpool));
if (!t)
{
err(MEMORY_FAILED, NULL, NULL);
return NULL;
}
// threadpool arguments init
t->num_threads = num_threads_in_pool;
t->threads = (pthread_t *)calloc(num_threads_in_pool, sizeof(pthread_t));
if (!t->threads)
{
err(MEMORY_FAILED, t, NULL);
return NULL;
}
// start with empty queue
t->qhead = NULL;
t->qtail = NULL;
t->qsize = 0;
// threadpool flags init
t->shutdown = 0;
t->dont_accept = 0;
// threadpool mutex and cond's init
if (pthread_mutex_init(&(t->qlock), NULL))
{
err(MUTEX_INIT_FAILED, t, t->threads);
return NULL;
}
if (pthread_cond_init(&(t->q_empty), NULL))
{
err(COND_INIT_FAILED, t, t->threads);
pthread_mutex_destroy(&(t->qlock));
return NULL;
}
if (pthread_cond_init(&(t->q_not_empty), NULL))
{
err(COND_INIT_FAILED, t, t->threads);
pthread_mutex_destroy(&(t->qlock));
pthread_cond_destroy(&(t->q_empty));
return NULL;
}
// create requested number of threads in threadpool
for (int i = 0; i < num_threads_in_pool; i++)
{
if (pthread_create(&(t->threads[i]), NULL, do_work, t))
{
err(THREAD_CREATE_FAILED, t, t->threads);
pthread_mutex_destroy(&(t->qlock));
pthread_cond_destroy(&(t->q_empty));
pthread_cond_destroy(&(t->q_not_empty));
return NULL;
}
}
return t;
}
void dispatch(threadpool *from_me, dispatch_fn dispatch_to_here, void *arg)
{
// lock the threadpool to insert new job safely
pthread_mutex_lock(&(from_me->qlock));
// check if shutdown flag is up
if (from_me->dont_accept)
return;
// if (!dispatch_to_here)
// return;
work_t *work = (work_t *)calloc(1, sizeof(work_t));
if (!work)
{
err(MEMORY_FAILED, NULL, NULL);
return;
}
// init work args
work->arg = arg;
work->next = NULL;
work->routine = dispatch_to_here;
// insert work into queue
if (!from_me->qhead)
{
from_me->qhead = work;
from_me->qtail = work;
}
else
{
from_me->qtail->next = work;
from_me->qtail = work;
}
// updating works queue size
from_me->qsize++;
// unlock threadpool for other thread
pthread_mutex_unlock(&(from_me->qlock));
// signal to sleeping thread to do_work
pthread_cond_signal(&(from_me->q_not_empty));
}
void destroy_threadpool(threadpool *destroyme)
{
// lock the threadpool to other
pthread_mutex_lock(&(destroyme->qlock));
// set dont accept flag up
destroyme->dont_accept = 1;
// there other jobs to do -> wait
if (destroyme->qsize)
pthread_cond_wait(&(destroyme->q_empty), &(destroyme->qlock));
// the queue jobs is empty, set shut down flag up
destroyme->shutdown = 1;
// wake up sleeping threads to finish
pthread_cond_broadcast(&destroyme->q_not_empty);
// unlock threadpool for threads
pthread_mutex_unlock(&(destroyme->qlock));
//
// pthread_cond_broadcast(&destroyme->q_empty);
// clear the threads by using pthread_join
for (int i = 0; i < destroyme->num_threads; i++)
pthread_join(destroyme->threads[i], NULL);
// free table args from memory
pthread_cond_destroy(&(destroyme->q_empty));
pthread_cond_destroy(&(destroyme->q_not_empty));
pthread_mutex_destroy(&(destroyme->qlock));
free(destroyme->threads);
free(destroyme);
}
void *do_work(void *p)
{
threadpool *t = (threadpool *)p;
while (1)
{
pthread_mutex_lock(&(t->qlock));
// if shutdown flag is up, dont accepet new work -> unlock mutex and kill thread
if (t->shutdown)
{
pthread_mutex_unlock(&(t->qlock));
return NULL;
}
// if threse no jobs to so -> go to sleep
while (t->qsize == 0)
{
pthread_cond_wait(&(t->q_not_empty), &(t->qlock));
// if threadpool shut down flag is up leave job and finish thread work
if (t->shutdown)
{
pthread_mutex_unlock(&(t->qlock));
return NULL;
}
}
work_t *cur_work = t->qhead;
t->qsize--;
if (t->qhead->next)
{
t->qhead = t->qhead->next;
}
else
{
// no other jobs in queue -> set head and tail to be NULL
t->qhead = NULL;
t->qtail = NULL;
// if threadpool dont_accept is up
if (t->dont_accept)
pthread_cond_signal(&(t->q_empty));
}
pthread_mutex_unlock(&(t->qlock));
cur_work->routine(cur_work->arg);
free(cur_work);
}
}