-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpool.c
248 lines (225 loc) · 6.4 KB
/
cpool.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
#include "cpool.h"
#include <threads.h>
#include <assert.h>
#include <stdlib.h>
struct cpool_future {
mtx_t mutex;
cnd_t cond;
int flag;
};
static cpool_future*
cpool_future_create(void)
{
cpool_future* ptr = malloc(sizeof(*ptr));
if (!ptr) goto end;
ptr->flag = 0;
if (mtx_init(&ptr->mutex, mtx_plain) != thrd_success) goto mutex_fail;
if (cnd_init(&ptr->cond) != thrd_success) goto cond_fail;
goto end;
cond_fail:
mtx_destroy(&ptr->mutex);
mutex_fail:
free(ptr);
ptr = NULL;
end:
return ptr;
}
static void
cpool_future_destroy(cpool_future* future)
{
cnd_destroy(&future->cond);
mtx_destroy(&future->mutex);
free(future);
}
typedef struct {
cpool_func_t func;
void* data;
// cpool_work_clean_func clean_func;
cpool_future* future; /* Worker-side reference to the allocated future object.
* The other side is hold by the user.
*/
} cpool_work;
struct cpool {
thrd_t* workers; /* Allocated array of thread identifiers. Joined on destruction. */
size_t nb_workers;
cpool_work* jobs; /* ring buffer of jobs */
size_t max_jobs; /* max size of the ring buffer */
size_t job_first, job_count;
mtx_t mutex;
cnd_t cond, cond_enqueue, cond_idle;
size_t nb_working;
int stop;
};
static int
thread_func(void* pool_ptr)
{
cpool* pool = pool_ptr;
for (;;) {
cpool_func_t job_func;
void* job_data;
cpool_future* future;
{
mtx_lock(&pool->mutex);
while (pool->job_count == 0 && !pool->stop) {
cnd_wait(&pool->cond, &pool->mutex);
}
if (pool->stop && pool->job_count == 0) {
mtx_unlock(&pool->mutex);
return 0;
}
/* get a job from front */
cpool_work* job_front = pool->jobs + pool->job_first;
job_func = job_front->func;
job_data = job_front->data;
future = job_front->future;
pool->job_first = (pool->job_first + 1) % pool->max_jobs;
pool->job_count -= 1;
pool->nb_working += 1;
mtx_unlock(&pool->mutex);
}
cnd_signal(&pool->cond_enqueue);
job_func(job_data);
if (future) {
mtx_lock(&future->mutex);
future->flag = 1;
mtx_unlock(&future->mutex);
cnd_signal(&future->cond); // only one thread is allowed to wait on the future...
}
{
mtx_lock(&pool->mutex);
if (--pool->nb_working == 0 && pool->job_count == 0) cnd_broadcast(&pool->cond_idle);
mtx_unlock(&pool->mutex);
}
}
}
cpool*
cpool_create(size_t nb_workers, size_t max_jobs)
{
cpool* pool = NULL;
if (!nb_workers || !max_jobs) goto end;
pool = malloc(sizeof(cpool));
if (!pool) goto end;
pool->nb_workers = nb_workers;
pool->max_jobs = max_jobs;
pool->job_first = 0;
pool->job_count = 0;
pool->nb_working = 0;
pool->stop = 0;
if (!(pool->workers = malloc(sizeof(thrd_t) * nb_workers))) goto workers_fail;
if (!(pool->jobs = malloc(sizeof(cpool_work) * max_jobs))) goto jobs_fail;
if (mtx_init(&pool->mutex, mtx_plain) != thrd_success) goto mutex_fail;
if (cnd_init(&pool->cond) != thrd_success) goto cond_fail;
if (cnd_init(&pool->cond_enqueue) != thrd_success) goto cond_enqueue_fail;
if (cnd_init(&pool->cond_idle) != thrd_success) goto cond_idle_fail;
/* launch workers */
size_t thread_success_count = 0;
for (size_t i = 0; i < nb_workers; ++i) {
if (thrd_create(pool->workers + i, thread_func, pool) == thrd_success) {
++thread_success_count;
}
else break;
}
/* clean up threads in case of failure */
if (thread_success_count == nb_workers) goto end;
{
mtx_lock(&pool->mutex);
pool->stop = 1;
mtx_unlock(&pool->mutex);
}
cnd_broadcast(&pool->cond);
for (size_t i = 0; i < thread_success_count; ++i) {
thrd_join(pool->workers[i], NULL);
}
cnd_destroy(&pool->cond_idle);
cond_idle_fail:
cnd_destroy(&pool->cond_enqueue);
cond_enqueue_fail:
cnd_destroy(&pool->cond);
cond_fail:
mtx_destroy(&pool->mutex);
mutex_fail:
free(pool->jobs);
jobs_fail:
free(pool->workers);
workers_fail:
free(pool);
pool = NULL;
end:
return pool;
}
void
cpool_destroy(cpool* pool)
{
cpool_stop(pool);
for (size_t i = 0; i < pool->nb_workers; ++i) {
thrd_join(pool->workers[i], NULL);
}
cnd_destroy(&pool->cond_idle);
cnd_destroy(&pool->cond_enqueue);
cnd_destroy(&pool->cond);
mtx_destroy(&pool->mutex);
free(pool->jobs);
free(pool->workers);
free(pool);
}
int
cpool_enqueue(cpool* pool, cpool_func_t func, void* data, cpool_future** future)
{
if (future) *future = cpool_future_create();
{
mtx_lock(&pool->mutex);
while (pool->job_count == pool->max_jobs && !pool->stop) {
cnd_wait(&pool->cond_enqueue, &pool->mutex);
}
if (pool->stop) {
mtx_unlock(&pool->mutex);
if (future && *future) {
cpool_future_destroy(*future);
*future = NULL;
}
return 1;
}
/* push back work */
cpool_work* job_new = pool->jobs + (pool->job_first + pool->job_count) % pool->max_jobs;
job_new->func = func;
job_new->data = data;
job_new->future = future? *future : NULL;
pool->job_count += 1;
mtx_unlock(&pool->mutex);
}
cnd_signal(&pool->cond);
return 0;
}
void
cpool_stop(cpool* pool)
{
{
mtx_lock(&pool->mutex);
pool->stop = 1;
mtx_unlock(&pool->mutex);
}
cnd_broadcast(&pool->cond);
cnd_broadcast(&pool->cond_enqueue);
}
void
cpool_wait(cpool* pool)
{
mtx_lock(&pool->mutex);
while (pool->nb_working > 0 || pool->job_count > 0) {
cnd_wait(&pool->cond_idle, &pool->mutex);
}
mtx_unlock(&pool->mutex);
}
void
cpool_wait_future(cpool_future* future)
{
{
mtx_lock(&future->mutex);
while (!future->flag) {
cnd_wait(&future->cond, &future->mutex);
}
mtx_unlock(&future->mutex);
}
/* Following our assumptions, this should be the last reference to the future, so destroy it. */
cpool_future_destroy(future);
}