Repository navigation
Expand file tree
/
Copy paththreadpool.c
More file actions
240 lines (202 loc) · 5.52 KB
/
Copy paththreadpool.c
File metadata and controls
240 lines (202 loc) · 5.52 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
#include <bits/types.h>
#include <pthread.h>
#include <stdbool.h>
#include "threadpool.h"
#include <stdlib.h>
#include <stdio.h>
ThreadPool_t *ThreadPool_create(unsigned int num) {
// allocate memory for ThreadPool_t
ThreadPool_t *tp = malloc(sizeof(ThreadPool_t));
if (tp == NULL) {
printf("Failed malloc");
goto clean0;
}
// init jobs list (empty)
tp->jobs.size = 0;
tp->jobs.head = NULL;
// init thread pool state
tp->num_threads = num;
tp->active_threads = 0;
tp->shutdown = false;
// allocate threads array
tp->threads = malloc(sizeof(pthread_t) * num);
if (tp->threads == NULL) {
printf("Failed malloc");
goto clean1;
}
// init mutex
if (pthread_mutex_init(&tp->queue_mutex, NULL)) {
printf("failed to init mutex");
goto clean2;
}
// init mutex condition variables
if (pthread_cond_init(&tp->job_avaliable, NULL)) {
printf("failed to init job_avaliable condition variable");
goto clean3;
}
if (pthread_cond_init(&tp->idle, NULL)) {
printf("failed to init idle condition variable");
goto clean4;
}
// create worker threads
for (unsigned i = 0; i < num; i++) {
if (pthread_create(&tp->threads[i], NULL, (void *(*) (void *)) Thread_run, tp)) {
printf("failed to create thread %u", i);
// set shutdown flag and clean
tp->shutdown = true;
pthread_cond_broadcast(&tp->job_avaliable);
// wait for created threads to exit
for (unsigned j = 0; j < i; j++) {
pthread_join(tp->threads[j], NULL);
}
goto clean5;
}
}
// no errors - return tp
return tp;
// errors - clean and return null
clean5:
pthread_cond_destroy(&tp->idle);
clean4:
pthread_cond_destroy(&tp->job_avaliable);
clean3:
pthread_mutex_destroy(&tp->queue_mutex);
clean2:
free(tp->threads);
clean1:
free(tp);
clean0:
return NULL;
}
void ThreadPool_destroy(ThreadPool_t *tp) {
if (tp == NULL) {
return;
}
// set shutdown flag and broadcast
pthread_mutex_lock(&tp->queue_mutex);
{
tp->shutdown = true;
pthread_cond_broadcast(&tp->job_avaliable);
}
pthread_mutex_unlock(&tp->queue_mutex);
// wait for all threads to finish
for (unsigned i = 0; i < tp->num_threads; i++) {
pthread_join(tp->threads[i], NULL);
}
// free any jobs left in queue (shouldnt be any but to be safe)
pthread_mutex_lock(&tp->queue_mutex);
{
ThreadPool_job_t *curr = tp->jobs.head;
while (curr != NULL) {
ThreadPool_job_t *next = curr->next;
free(curr);
curr = next;
}
}
pthread_mutex_unlock(&tp->queue_mutex);
// clean
pthread_mutex_destroy(&tp->queue_mutex);
pthread_cond_destroy(&tp->job_avaliable);
pthread_cond_destroy(&tp->idle);
free(tp->threads);
free(tp);
return;
}
bool ThreadPool_add_job(ThreadPool_t *tp, thread_func_t func, void *arg) {
if (tp == NULL || func == NULL) {
return false;
}
ThreadPool_job_t *job = malloc(sizeof(ThreadPool_job_t));
if (job == NULL) {
printf("failed malloc");
return false;
}
job->func = func;
job->arg = arg;
job->next = NULL;
job->job_size = 0;
// lock for modifying queue
pthread_mutex_lock(&tp->queue_mutex);
{
// sjf
// if queue is empty or new job is smaller than head then insert at head
if (tp->jobs.head == NULL || job->job_size < tp->jobs.head->job_size) {
job->next = tp->jobs.head;
tp->jobs.head = job;
}
else {
// find correct position and insert
ThreadPool_job_t *curr = tp->jobs.head;
while(curr->next != NULL && curr->next->job_size <= job->job_size) {
curr = curr->next;
}
job->next = curr->next;
curr->next = job;
}
tp->jobs.size++;
// signal one thread that a job is available
pthread_cond_signal(&tp->job_avaliable);
}
pthread_mutex_unlock(&tp->queue_mutex);
return true;
}
ThreadPool_job_t *ThreadPool_get_job(ThreadPool_t *tp) {
if (tp->jobs.head == NULL || !tp->jobs.size) {
return NULL;
}
ThreadPool_job_t *job = tp->jobs.head;
tp->jobs.head = job->next;
tp->jobs.size--;
job->next = NULL;
return job;
}
void *Thread_run(ThreadPool_t *tp) {
ThreadPool_job_t *job = NULL;
// run forever until broken by shutdown (tp->shutdown flag)
while (1) {
// lock mutex to access shared data
pthread_mutex_lock(&tp->queue_mutex);
{
// wait while no jobs (and not shutdown)
while (!tp->jobs.size && !tp->shutdown) {
// thread is idle - wait for job
pthread_cond_wait(&tp->job_avaliable, &tp->queue_mutex);
}
// check if we need to exit (shutdown and no more jobs)
if (!tp->jobs.size && tp->shutdown) {
pthread_mutex_unlock(&tp->queue_mutex);
break;
}
// get a j*b
job = ThreadPool_get_job(tp);
tp->active_threads++;
}
pthread_mutex_unlock(&tp->queue_mutex);
// no lock held during execution
if (job != NULL) {
job->func(job->arg);
free(job);
}
// lock again to update state
pthread_mutex_lock(&tp->queue_mutex);
{
tp->active_threads--;
if (!tp->active_threads && !tp->jobs.size) {
pthread_cond_signal(&tp->idle);
}
}
pthread_mutex_unlock(&tp->queue_mutex);
}
return NULL;
}
void ThreadPool_check(ThreadPool_t *tp) {
// lock mutex and wait for idle condition to be signaled
// while theres active threads or jobs in queue
pthread_mutex_lock(&tp->queue_mutex);
{
while (tp->active_threads > 0 || tp->jobs.size > 0) {
pthread_cond_wait(&tp->idle, &tp->queue_mutex);
}
}
pthread_mutex_unlock(&tp->queue_mutex);
}