-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthreadpool.c
254 lines (222 loc) · 6.1 KB
/
pthreadpool.c
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
#include "pthreadpool.h"
struct task
{
void (*function)(void *);
void *arg;
};
struct pthreadpool
{
size_t queue_size;
struct task *queue;
size_t head;
size_t tail;
size_t tasks;
bool active;
pthread_mutex_t mutex;
pthread_cond_t write;
pthread_cond_t read;
pthread_t *threads;
size_t thread_count;
};
char error_buffer[256];
#define SET_ERROR(message) (void)snprintf( \
&error_buffer[0], \
256, \
"%s:%d: %s: " message, \
__FILE__, \
__LINE__, \
__FUNCTION__)
#define SET_ERRORF(format, ...) (void)snprintf( \
&error_buffer[0], \
256, \
"%s:%d: %s: " format, \
__FILE__, \
__LINE__, \
__FUNCTION__, \
__VA_ARGS__)
static inline size_t pthreadpool_next(pthreadpool_t threadpool, size_t index)
{
return (index + 1) % threadpool->queue_size;
}
static void *pthreadpool_worker(void *arg)
{
pthreadpool_t const threadpool = (pthreadpool_t)arg;
struct task *current;
while (threadpool->active)
{
current = NULL;
(void)pthread_mutex_lock(&threadpool->mutex);
while (threadpool->active && threadpool->tasks == 0)
{
(void)pthread_cond_wait(&threadpool->read, &threadpool->mutex);
}
if (threadpool->active)
{
current = threadpool->queue + threadpool->head;
threadpool->tasks--;
threadpool->head = pthreadpool_next(threadpool, threadpool->head);
}
pthread_mutex_unlock(&threadpool->mutex);
if (current != NULL)
{
current->function(current->arg);
pthread_cond_signal(&threadpool->write);
}
}
return NULL;
}
int pthreadpool_create(
pthreadpool_t *threadpool,
size_t thread_count,
size_t queue_size)
{
int status;
struct pthreadpool *new_threadpool;
status = PTHREADPOOL_SUCCESS;
new_threadpool = NULL;
if (threadpool == NULL)
{
SET_ERROR("NULL threadpool handle");
goto error;
}
if (thread_count == 0)
{
SET_ERRORF("invalid thread count: %lu", thread_count);
goto error;
}
if (queue_size == 0)
{
SET_ERRORF("invalid queue size: %lu", thread_count);
goto error;
}
status = PTHREADPOOL_SUCCESS;
new_threadpool = calloc(1, sizeof(struct pthreadpool));
new_threadpool->queue_size = queue_size;
new_threadpool->queue = calloc(queue_size, sizeof(struct task));
new_threadpool->head = 0;
new_threadpool->tail = 0;
new_threadpool->tasks = 0;
new_threadpool->active = true;
(void)pthread_mutex_init(&new_threadpool->mutex, NULL);
(void)pthread_cond_init(&new_threadpool->read, NULL);
(void)pthread_cond_init(&new_threadpool->write, NULL);
new_threadpool->thread_count = thread_count;
new_threadpool->threads = calloc(thread_count, sizeof(pthread_t));
for (size_t thread_index = 0; thread_index < thread_count; thread_index++)
{
(void)pthread_create(
new_threadpool->threads + thread_index,
NULL,
pthreadpool_worker,
new_threadpool);
}
*threadpool = new_threadpool;
goto done;
error:
status = PTHREADPOOL_ERROR;
if (new_threadpool != NULL)
{
pthreadpool_destroy(new_threadpool);
}
done:
return status;
}
int pthreadpool_destroy(
pthreadpool_t threadpool)
{
int status;
status = PTHREADPOOL_SUCCESS;
if (threadpool == NULL)
{
SET_ERROR("NULL threadpool");
goto error;
}
if (threadpool->active)
{
(void)pthreadpool_shutdown(threadpool);
}
free(threadpool->threads);
(void)pthread_cond_destroy(&threadpool->write);
(void)pthread_cond_destroy(&threadpool->read);
(void)pthread_mutex_destroy(&threadpool->mutex);
free(threadpool->queue);
free(threadpool);
goto done;
error:
status = PTHREADPOOL_ERROR;
done:
return status;
}
int pthreadpool_submit(
pthreadpool_t threadpool,
void (*function)(void *), void *arg)
{
int status;
status = PTHREADPOOL_SUCCESS;
if (threadpool == NULL)
{
SET_ERROR("NULL threadpool");
goto error;
}
if (function == NULL)
{
SET_ERROR("NULL function");
goto error;
}
(void)pthread_mutex_lock(&threadpool->mutex);
while (threadpool->active && threadpool->tasks == threadpool->queue_size)
{
(void)pthread_cond_wait(&threadpool->write, &threadpool->mutex);
}
if (!threadpool->active)
{
SET_ERROR("failed to submit task to inactive threadpool");
(void)pthread_mutex_unlock(&threadpool->mutex);
goto error;
}
(threadpool->queue + threadpool->tail)->function = function;
(threadpool->queue + threadpool->tail)->arg = arg;
threadpool->tasks++;
threadpool->tail = pthreadpool_next(threadpool, threadpool->tail);
(void)pthread_mutex_unlock(&threadpool->mutex);
(void)pthread_cond_signal(&threadpool->read);
goto done;
error:
status = PTHREADPOOL_ERROR;
done:
return status;
}
int pthreadpool_shutdown(
struct pthreadpool *threadpool)
{
int status;
status = PTHREADPOOL_SUCCESS;
if (threadpool == NULL)
{
SET_ERROR("NULL threadpool");
goto error;
}
if(!threadpool->active)
{
SET_ERROR("threadpool inactive");
goto error;
}
threadpool->active = false;
(void)pthread_cond_broadcast(&threadpool->write);
(void)pthread_cond_broadcast(&threadpool->read);
for (size_t thread_index = 0; thread_index < threadpool->thread_count; thread_index++)
{
(void)pthread_join(
*(threadpool->threads + thread_index),
NULL);
}
goto done;
error:
status = PTHREADPOOL_ERROR;
done:
return status;
}
const char *pthreadpool_error()
{
return &error_buffer[0];
}