-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.c
More file actions
296 lines (257 loc) · 8.81 KB
/
threadpool.c
File metadata and controls
296 lines (257 loc) · 8.81 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
#include "threadpool.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
threadpool *create_threadpool(int num_threads_in_pool) {
// Validate input
if (num_threads_in_pool < 1 || num_threads_in_pool > MAXT_IN_POOL) {
fprintf(stderr, "Error: Invalid pool size\n");
return NULL; // Invalid input
}
// Allocate memory for the thread pool structure
threadpool *pool = (threadpool *) malloc(sizeof(threadpool));
if (!pool) {
perror("Error: malloc failed for thread pool");
exit(EXIT_FAILURE);
}
// Initialize the thread pool attributes
pool->num_threads = num_threads_in_pool;
pool->qsize = 0;
pool->threads = (pthread_t *) malloc(sizeof(pthread_t) * num_threads_in_pool);
if (pool->threads == NULL) {
perror("Error: malloc failed for thread IDs");
free(pool); // Free previously allocated memory for the thread pool structure
exit(EXIT_FAILURE);
}
pool->qhead = pool->qtail = NULL;
pool->shutdown = 0;
pool->dont_accept = 0;
// Initialize mutex and condition variables
if (pthread_mutex_init(&(pool->qlock), NULL) != 0) {
perror("Error: pthread_mutex_init");
free(pool->threads); // Free previously allocated memory for thread IDs
free(pool); // Free previously allocated memory for the thread pool structure
exit(EXIT_FAILURE);
}
if (pthread_cond_init(&(pool->q_not_empty), NULL) != 0) {
perror("Error: pthread_cond_init");
pthread_mutex_destroy(&(pool->qlock)); // Destroy mutex
free(pool->threads); // Free previously allocated memory for thread IDs
free(pool); // Free previously allocated memory for the thread pool structure
exit(EXIT_FAILURE);
}
if (pthread_cond_init(&(pool->q_empty), NULL) != 0) {
perror("Error: pthread_cond_init");
pthread_mutex_destroy(&(pool->qlock)); // Destroy mutex
pthread_cond_destroy(&(pool->q_not_empty)); // Destroy condition variable
free(pool->threads); // Free previously allocated memory for thread IDs
free(pool); // Free previously allocated memory for the thread pool structure
exit(EXIT_FAILURE);
}
// Create worker threads
for (int i = 0; i < num_threads_in_pool; i++) {
if (pthread_create(&(pool->threads[i]), NULL, do_work, (void *) pool) != 0) {
perror("Error: pthread_create");
// Cleanup resources
for (int j = 0; j < i; j++) {
pthread_cancel(pool->threads[j]);
}
pthread_mutex_destroy(&(pool->qlock)); // Destroy mutex
pthread_cond_destroy(&(pool->q_not_empty)); // Destroy condition variable
pthread_cond_destroy(&(pool->q_empty)); // Destroy condition variable
free(pool->threads); // Free previously allocated memory for thread IDs
free(pool); // Free previously allocated memory for the thread pool structure
exit(EXIT_FAILURE);
}
}
return pool;
}
//threadpool *create_threadpool(int num_threads_in_pool) {
// if (num_threads_in_pool < 1 || num_threads_in_pool > MAXT_IN_POOL) {
// printf("Error: Invalid pool side\n");
// return NULL; // Invalid input
// }
//
// threadpool *pool = (threadpool *) malloc(sizeof(threadpool));
// if (!pool) {
// perror("pool malloc\n");
// exit(EXIT_FAILURE);
// }
//
// pool->num_threads = num_threads_in_pool;
// pool->qsize = 0;
// pool->threads = (pthread_t *) malloc(sizeof(pthread_t) * num_threads_in_pool);
// if(pool->threads == NULL){
// perror("pool->threads malloc\n");
// exit(EXIT_FAILURE);
// }
//
// pool->qhead = pool->qtail = NULL;
// pool->shutdown = 0;
// pool->dont_accept = 0;
//
// // Initialize mutex and condition variables
// if (pthread_mutex_init(&(pool->qlock), NULL) != 0){
// perror("Error: pthread_mutex_init\n;");
// exit(EXIT_FAILURE);
// }
//
// if (pthread_cond_init(&(pool->q_not_empty), NULL)!= 0){
// perror("Error: pthread_mutex_init\n;");
// exit(EXIT_FAILURE);
// }
//
// if (pthread_cond_init(&(pool->q_empty), NULL) != 0){
// perror("Error: pthread_mutex_init\n;");
// exit(EXIT_FAILURE);
// }
//
// // Create worker threads
// for (int i = 0; i < num_threads_in_pool; i++) {
// if (pthread_create(&(pool->threads[i]), NULL, do_work, (void *) pool) != 0){
// perror("Error: pthread_create\n");
// }
// }
//
// return pool;
//}
void dispatch(threadpool *from_me, dispatch_fn dispatch_to_here, void *arg) {
work_t *work = (work_t *) malloc(sizeof(work_t));
if (!work) {
return; // Memory allocation failed
}
/* create work structure */
work->routine = dispatch_to_here;
work->arg = arg;
work->next = NULL;
pthread_mutex_lock(&(from_me->qlock));
/* if we don't accept anymore works */
if (!from_me || from_me->dont_accept) {
pthread_mutex_unlock(&(from_me->qlock));
free(work); // No need to free here
return; // Pool is not valid or destruction has begun
}
/* if queue size == 0, add work to queue and broadcast that queue isn't empty */
if (from_me->qsize == 0) {
from_me->qhead = from_me->qtail = work;
pthread_cond_broadcast(&(from_me->q_not_empty));
}
/* add work to queue*/
else {
from_me->qtail->next = work;
from_me->qtail = work;
}
// increase work queue size
from_me->qsize++;
pthread_mutex_unlock(&(from_me->qlock));
}
//void dispatch(threadpool *from_me, dispatch_fn dispatch_to_here, void *arg) {
// work_t *work = (work_t *) malloc(sizeof(work_t));
// if (!work) {
// return; // Memory allocation failed
// }
//
// pthread_mutex_lock(&(from_me->qlock));
//
// /* if we don't accept anymore works */
// if (!from_me || from_me->dont_accept) {
// pthread_mutex_unlock(&(from_me->qlock));
// free(work);
// return; // Pool is not valid or destruction has begun
// }
//
// /* create work structure */
// work->routine = dispatch_to_here;
// work->arg = arg;
// work->next = NULL;
//
// /* if queue size == 0, add work to queue and broadcast that queue isn't empty */
// if (from_me->qsize == 0) {
// from_me->qhead = from_me->qtail = work;
// pthread_cond_broadcast(&(from_me->q_not_empty));
// }
//
// /* add work to queue*/
// else {
// from_me->qtail->next = work;
// from_me->qtail = work;
// }
//
// // increase work queue size
// from_me->qsize++;
//
// pthread_mutex_unlock(&(from_me->qlock));
//
// free(work);
//}
void *do_work(void *p) {
threadpool *pool = (threadpool *) p;
while (1) {
pthread_mutex_lock(&(pool->qlock));
while (pool->qsize == 0 && !pool->shutdown) {
pthread_cond_wait(&(pool->q_not_empty), &(pool->qlock));
}
if (pool->shutdown && pool->qsize == 0) {
pthread_mutex_unlock(&(pool->qlock));
pthread_exit(NULL);
}
work_t *work = pool->qhead;
pool->qhead = work->next;
if (pool->qsize == 1) {
pool->qtail = NULL;
}
pool->qsize--;
pthread_mutex_unlock(&(pool->qlock));
work->routine(work->arg);
free(work);
}
}
//void destroy_threadpool(threadpool *destroyme) {
// if (!destroyme || destroyme->dont_accept) {
// return; // Pool is not valid or destruction has begun
// }
//
// pthread_mutex_lock(&(destroyme->qlock));
//
// destroyme->dont_accept = 1;
//
// /* waiting for all works to be done */
// while (destroyme->qsize > 0) {
// printf("Waiting for %d works to be done...\n", destroyme->qsize);
// 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);
//
// destroyme = NULL;
//
// printf("Threadpool destroyed.\n");
//}
void destroy_threadpool(threadpool *destroyme) {
if (!destroyme || destroyme->dont_accept) {
return; // Pool is not valid or destruction has begun
}
pthread_mutex_lock(&(destroyme->qlock));
destroyme->dont_accept = 1;
/* Signal threads to exit */
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);
destroyme = NULL;
printf("Threadpool destroyed.\n");
}