-
Notifications
You must be signed in to change notification settings - Fork 2
/
workqueue.c
266 lines (208 loc) · 5.81 KB
/
workqueue.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
255
256
257
258
259
260
261
262
263
264
265
266
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include "sllist.h"
#include "helpers.h"
#include "notification.h"
#include "workqueue.h"
struct workqueue_desc {
pthread_mutex_t mutex;
struct notification notification;
unsigned int num;
unsigned int stopping:1;
pthread_t threads[0];
};
static void *
worker(struct workqueue *wq)
{
struct sllist *e, *p, *t;
struct work *w;
for (;;) {
pthread_mutex_lock(&wq->desc->mutex);
if (wq->desc->stopping) {
pthread_mutex_unlock(&wq->desc->mutex);
return NULL;
}
pthread_mutex_unlock(&wq->desc->mutex);
wait_for_notification(&wq->desc->notification);
pthread_mutex_lock(&wq->desc->mutex);
if (wq->desc->stopping) {
pthread_mutex_unlock(&wq->desc->mutex);
return NULL;
}
retry:
sllist_for_each_safe_prev(&wq->works, e, p, t) {
w = container_of(e, struct work, list);
sllist_detach(e, p);
pthread_mutex_unlock(&wq->desc->mutex);
w->state = WORK_RUNNING;
w->fn(w->data);
w->state = WORK_FINISHED;
if (w->notification_fn)
w->notification_fn(w->notification_data);
pthread_mutex_lock(&wq->desc->mutex);
goto retry;
}
pthread_mutex_unlock(&wq->desc->mutex);
}
return NULL;
}
static void
wake_up_all_workers(struct workqueue *wq)
{
notify_all(&wq->desc->notification);
}
static void
wake_up_one_worker(struct workqueue *wq)
{
notify(&wq->desc->notification);
}
int
init_workqueue(struct workqueue *wq, unsigned int threads)
{
wq->desc = malloc(sizeof(struct workqueue_desc) + threads*sizeof(pthread_t));
if (!wq->desc) {
printf("Unable to allocate wq descriptor\n");
return -1;
}
if (init_notification(&wq->desc->notification) != 0) {
printf("Unable to init notification\n");
free(wq->desc);
return -1;
}
if (pthread_mutex_init(&wq->desc->mutex, NULL) != 0) {
printf("Unable to init wq lock\n");
free(wq->desc);
return -1;
}
wq->desc->stopping = 0;
for (wq->desc->num=0; wq->desc->num<threads;++wq->desc->num) {
if (pthread_create(&wq->desc->threads[wq->desc->num], NULL, (void *(*)(void*))worker, wq) != 0) {
printf("Unable to create wq thread\n");
wq->desc->stopping = 1;
wake_up_all_workers(wq);
for (;wq->desc->num!=0;--wq->desc->num) {
pthread_join(wq->desc->threads[wq->desc->num-1], (void **)NULL);
}
free(wq->desc);
return -1;
}
}
sllist_init(&wq->works);
return 0;
}
void
flush_workqueue(struct workqueue *wq)
{
for (;;) {
pthread_mutex_lock(&wq->desc->mutex);
if (sllist_empty(&wq->works)) {
pthread_mutex_unlock(&wq->desc->mutex);
return;
}
pthread_mutex_unlock(&wq->desc->mutex);
wake_up_all_workers(wq);
}
}
void
destroy_workqueue(struct workqueue *wq)
{
struct sllist *e, *p, *t;
struct work *w;
pthread_mutex_lock(&wq->desc->mutex);
wq->desc->stopping = 1;
sllist_for_each_safe_prev(&wq->works, e, p, t) {
w = container_of(e, struct work, list);
w->state = WORK_PURGED;
if (w->notification_fn)
w->notification_fn(w->notification_data);
sllist_detach(e, p);
}
pthread_mutex_unlock(&wq->desc->mutex);
wake_up_all_workers(wq);
for (;wq->desc->num!=0;--wq->desc->num) {
pthread_join(wq->desc->threads[wq->desc->num-1], (void **)NULL);
}
pthread_mutex_destroy(&wq->desc->mutex);
destroy_notification(&wq->desc->notification);
free(wq->desc);
}
void
add_work(struct workqueue *wq, struct work *work)
{
work->state = WORK_RUNNING;
pthread_mutex_lock(&wq->desc->mutex);
sllist_add(&wq->works, &work->list);
pthread_mutex_unlock(&wq->desc->mutex);
wake_up_one_worker(wq);
}
#ifdef WORKQUEUE_MAIN
void __worker(void *data)
{
printf("work %z\n", (size_t)data);
}
void __notification(void *data)
{
printf("finished %u\n", (size_t)data);
}
int
usage(const char *prog)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s [--threads|-t <num>] [--works|-w <num>]\n", prog);
return 1;
}
int main(unsigned int argc, char **argv)
{
struct timeval tb, ta;
unsigned long secs, msecs, usecs;
unsigned long works = 0;
unsigned int threads = 0;
struct workqueue wq;
struct work *ws;
int opt;
const struct option options[] = {
{"threads", required_argument, NULL, 't'},
{"works", required_argument, NULL, 'w'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "t:w:", options, NULL)) != -1) {
switch (opt) {
case 't':
threads = atoi(optarg);
break;
case 'w':
works = atoi(optarg);
break;
case 0:
break;
default:
return usage(argv[0]);
}
}
ws = malloc(sizeof(struct work)*works);
if (!ws) {
printf("Unable to allocate %u bytes for works\n", sizeof(struct work)*works);
return 1;
}
if (init_workqueue(&wq, threads) != 0) {
printf("Unable to init workqueue\n");
free(ws);
return 1;
}
for (;works!=0;--works) {
struct work *w = &ws[works-1];
w->fn = __worker;
w->data = (void *)works;
w->notification_fn = __notification;
w->notification_data = (void *)works;
add_work(&wq, w);
}
flush_workqueue(&wq);
destroy_workqueue(&wq);
free(ws);
return 0;
}
#endif