-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwq.c
261 lines (182 loc) · 5.73 KB
/
wq.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
/*
eventfs: a self-cleaning filesystem for event queues.
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.LGPLv3+ 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 "wq.h"
// work queue main method
static void* eventfs_wq_main( void* cls ) {
struct eventfs_wq* wq = (struct eventfs_wq*)cls;
struct eventfs_wreq* work_itr = NULL;
struct eventfs_wreq* next = NULL;
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
eventfs_error("FATAL: sem_trywait rc = %d\n", rc );
break;
}
}
// cancelled?
if( !wq->running ) {
break;
}
// exchange buffers--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 );
// safe to use work buffer (we'll clear it out in the process)
while( work_itr != NULL ) {
// carry out work
eventfs_debug("begin work %p\n", work_itr->work_data);
rc = (*work_itr->work)( work_itr, work_itr->work_data );
eventfs_debug("end work %p\n", work_itr->work_data);
if( rc != 0 ) {
eventfs_error("work %p rc = %d\n", work_itr->work, rc );
}
next = work_itr->next;
eventfs_wreq_free( work_itr );
eventfs_safe_free( work_itr );
work_itr = next;
}
}
return NULL;
}
// make a work queue
struct eventfs_wq* eventfs_wq_new() {
return EVENTFS_CALLOC( struct eventfs_wq, 1 );
}
// set up a work queue, but don't start it.
// return 0 on success
// return negative on failure:
// * -ENOMEM if OOM
int eventfs_wq_init( struct eventfs_wq* wq ) {
int rc = 0;
memset( wq, 0, sizeof(struct eventfs_wq) );
rc = pthread_mutex_init( &wq->work_lock, NULL );
if( rc != 0 ) {
return -abs(rc);
}
sem_init( &wq->work_sem, 0, 0 );
return rc;
}
// start a work queue
// return 0 on success
// return negative on error:
// * -EINVAL if already started
// * whatever pthread_create errors on
int eventfs_wq_start( struct eventfs_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, eventfs_wq_main, wq );
if( rc != 0 ) {
wq->running = false;
rc = -errno;
eventfs_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 eventfs_wq_stop( struct eventfs_wq* wq ) {
if( !wq->running ) {
return -EINVAL;
}
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
static int eventfs_wq_queue_free( struct eventfs_wreq* wqueue ) {
struct eventfs_wreq* next = NULL;
while( wqueue != NULL ) {
next = wqueue->next;
eventfs_wreq_free( wqueue );
eventfs_safe_free( wqueue );
wqueue = next;
}
return 0;
}
// free up a work queue
// return 0 on success
// return negative on error:
// * -EINVAL if running
int eventfs_wq_free( struct eventfs_wq* wq ) {
if( wq->running ) {
return -EINVAL;
}
// free all
eventfs_wq_queue_free( wq->work );
pthread_mutex_destroy( &wq->work_lock );
sem_destroy( &wq->work_sem );
memset( wq, 0, sizeof(struct eventfs_wq) );
return 0;
}
// create a work request
// always succeeds
int eventfs_wreq_init( struct eventfs_wreq* wreq, eventfs_wq_func_t work, void* work_data ) {
memset( wreq, 0, sizeof(struct eventfs_wreq) );
wreq->work = work;
wreq->work_data = work_data;
return 0;
}
// free a work request
int eventfs_wreq_free( struct eventfs_wreq* wreq ) {
memset( wreq, 0, sizeof(struct eventfs_wreq) );
return 0;
}
// enqueue work. The work queue takes onwership of the wreq, so it must be malloc'ed
// always succeeds
int eventfs_wq_add( struct eventfs_wq* wq, struct eventfs_wreq* wreq ) {
int rc = 0;
pthread_mutex_lock( &wq->work_lock );
if( wq->work == NULL ) {
// head
wq->work = wreq;
wq->tail = wreq;
}
else {
// append
wq->tail->next = wreq;
wq->tail = wreq;
}
pthread_mutex_unlock( &wq->work_lock );
if( rc == 0 ) {
// have work
sem_post( &wq->work_sem );
}
return rc;
}