-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathworkqueue.c
342 lines (236 loc) · 7.29 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
vdev: a virtual device manager for *nix
Copyright (C) 2015 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.GPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "libvdev/util.h"
#include "workqueue.h"
#include "os/common.h"
#include "vdev.h"
// wait for the queue to be drained of coldplug events
static void vdev_wq_wait_for_empty( struct vdev_wq* wq ) {
pthread_mutex_lock( &wq->waiter_lock );
wq->num_waiters++;
pthread_mutex_unlock( &wq->waiter_lock );
sem_wait( &wq->end_sem );
}
// signal any waiting threads that the queue has been drained of coldplug events
static void vdev_wq_signal_empty( struct vdev_wq* wq ) {
volatile int num_waiters = 0;
pthread_mutex_lock( &wq->waiter_lock );
num_waiters = wq->num_waiters;
wq->num_waiters = 0;
pthread_mutex_unlock( &wq->waiter_lock );
for( int i = 0; i < num_waiters; i++ ) {
sem_post( &wq->end_sem );
}
}
// work queue main method
// always succeeds
static void* vdev_wq_main( void* cls ) {
struct vdev_wq* wq = (struct vdev_wq*)cls;
struct vdev_wreq* work_itr = NULL;
struct vdev_wreq* next = NULL;
struct vdev_state* state = wq->state;
bool coldplug_finished = false;
int rc = 0;
while( wq->running ) {
// is there work?
rc = sem_trywait( &wq->work_sem );
if( rc != 0 ) {
rc = -errno;
if( rc == -EAGAIN ) {
// wait for work
sem_wait( &wq->work_sem );
}
else {
// some other fatal error
vdev_error("FATAL: sem_trywait rc = %d\n", rc );
break;
}
}
// cancelled?
if( !wq->running ) {
break;
}
// we have work to do
pthread_mutex_lock( &wq->work_lock );
work_itr = wq->work;
wq->work = NULL;
wq->tail = NULL;
pthread_mutex_unlock( &wq->work_lock );
if( !coldplug_finished ) {
coldplug_finished = vdev_os_context_is_coldplug_finished( state->os );
}
if( work_itr == NULL ) {
vdev_wq_signal_empty( wq );
if( coldplug_finished ) {
// coldplug has finished! signal the parent to exit.
vdev_signal_coldplug_finished( state, 0 );
}
continue;
}
// safe to use work buffer (we'll clear it out in the process)
while( work_itr != NULL ) {
// carry out work
rc = (*work_itr->work)( work_itr, work_itr->work_data );
if( rc != 0 ) {
vdev_warn("work %p rc = %d\n", work_itr->work, rc );
}
next = work_itr->next;
vdev_wreq_free( work_itr );
free( work_itr );
work_itr = next;
}
}
return NULL;
}
// set up a work queue, but don't start it.
// return 0 on success
// return negative on failure:
// * -ENOMEM if OOM
int vdev_wq_init( struct vdev_wq* wq, struct vdev_state* state ) {
int rc = 0;
memset( wq, 0, sizeof(struct vdev_wq) );
rc = pthread_mutex_init( &wq->work_lock, NULL );
if( rc != 0 ) {
return -abs(rc);
}
rc = pthread_mutex_init( &wq->waiter_lock, NULL );
if( rc != 0 ) {
pthread_mutex_destroy( &wq->work_lock );
return -abs(rc);
}
sem_init( &wq->work_sem, 0, 0 );
sem_init( &wq->end_sem, 0, 0 );
wq->state = state;
return rc;
}
// start a work queue
// return 0 on success
// return negative on error:
// * -EINVAL if already started
// * whatever pthread_create errors on
int vdev_wq_start( struct vdev_wq* wq ) {
if( wq->running ) {
return -EINVAL;
}
int rc = 0;
pthread_attr_t attrs;
memset( &attrs, 0, sizeof(pthread_attr_t) );
wq->running = true;
rc = pthread_create( &wq->thread, &attrs, vdev_wq_main, wq );
if( rc != 0 ) {
wq->running = false;
rc = -errno;
vdev_error("pthread_create errno = %d\n", rc );
return rc;
}
return 0;
}
// stop a work queue
// return 0 on success
// return negative on error:
// * -EINVAL if not running
int vdev_wq_stop( struct vdev_wq* wq, bool wait ) {
if( !wq->running ) {
return -EINVAL;
}
if( wait ) {
// wait for the queue to be empty
vdev_wq_wait_for_empty( wq );
}
wq->running = false;
// wake up the work queue so it cancels
sem_post( &wq->work_sem );
pthread_cancel( wq->thread );
pthread_join( wq->thread, NULL );
return 0;
}
// free a work request queue
// always succeeds
static int vdev_wq_queue_free( struct vdev_wreq* wqueue ) {
struct vdev_wreq* next = NULL;
while( wqueue != NULL ) {
next = wqueue->next;
vdev_wreq_free( wqueue );
free( wqueue );
wqueue = next;
}
return 0;
}
// free up a work queue
// return 0 on success
// return negative on error:
// * -EINVAL if running
int vdev_wq_free( struct vdev_wq* wq ) {
if( wq->running ) {
return -EINVAL;
}
// free all
vdev_wq_queue_free( wq->work );
pthread_mutex_destroy( &wq->work_lock );
pthread_mutex_destroy( &wq->waiter_lock );
sem_destroy( &wq->work_sem );
sem_destroy( &wq->end_sem );
memset( wq, 0, sizeof(struct vdev_wq) );
return 0;
}
// create a work request
// always succeeds
int vdev_wreq_init( struct vdev_wreq* wreq, vdev_wq_func_t work, void* work_data ) {
memset( wreq, 0, sizeof(struct vdev_wreq) );
wreq->work = work;
wreq->work_data = work_data;
return 0;
}
// free a work request
// always succeeds
int vdev_wreq_free( struct vdev_wreq* wreq ) {
memset( wreq, 0, sizeof(struct vdev_wreq) );
return 0;
}
// enqueue work
// return 0 on success
// return -ENOMEM if OOM
int vdev_wq_add( struct vdev_wq* wq, struct vdev_wreq* wreq ) {
int rc = 0;
struct vdev_wreq* next = NULL;
// duplicate this work item
next = VDEV_CALLOC( struct vdev_wreq, 1 );
if( next == NULL ) {
return -ENOMEM;
}
next->work = wreq->work;
next->work_data = wreq->work_data;
next->next = NULL;
pthread_mutex_lock( &wq->work_lock );
if( wq->work == NULL ) {
// head
wq->work = next;
wq->tail = next;
}
else {
// append
wq->tail->next = next;
wq->tail = next;
}
pthread_mutex_unlock( &wq->work_lock );
if( rc == 0 ) {
// have work
sem_post( &wq->work_sem );
}
return rc;
}