cadcf874be6c76726dc1a3dd3e082c545b266e8b
[urcu.git] / src / workqueue.c
1 /*
2 * workqueue.c
3 *
4 * Userspace RCU library - Userspace workqeues
5 *
6 * Copyright (c) 2010 Paul E. McKenney <paulmck@linux.vnet.ibm.com>
7 * Copyright (c) 2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #define _LGPL_SOURCE
25 #include <stdio.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <poll.h>
34 #include <sys/time.h>
35 #include <unistd.h>
36 #include <sched.h>
37
38 #include "compat-getcpu.h"
39 #include <urcu/wfcqueue.h>
40 #include <urcu/pointer.h>
41 #include <urcu/list.h>
42 #include <urcu/futex.h>
43 #include <urcu/tls-compat.h>
44 #include <urcu/ref.h>
45 #include "urcu-die.h"
46
47 #include "workqueue.h"
48
49 #define SET_AFFINITY_CHECK_PERIOD (1U << 8) /* 256 */
50 #define SET_AFFINITY_CHECK_PERIOD_MASK (SET_AFFINITY_CHECK_PERIOD - 1)
51
52 /* Data structure that identifies a workqueue. */
53
54 struct urcu_workqueue {
55 /*
56 * We do not align head on a different cache-line than tail
57 * mainly because workqueue threads use batching ("splice") to
58 * get an entire list of callbacks, which effectively empties
59 * the queue, and requires to touch the tail anyway.
60 */
61 struct cds_wfcq_tail cbs_tail;
62 struct cds_wfcq_head cbs_head;
63 unsigned long flags;
64 int32_t futex;
65 unsigned long qlen; /* maintained for debugging. */
66 pthread_t tid;
67 int cpu_affinity;
68 unsigned long loop_count;
69 void *priv;
70 void (*grace_period_fct)(struct urcu_workqueue *workqueue, void *priv);
71 void (*initialize_worker_fct)(struct urcu_workqueue *workqueue, void *priv);
72 void (*finalize_worker_fct)(struct urcu_workqueue *workqueue, void *priv);
73 void (*worker_before_pause_fct)(struct urcu_workqueue *workqueue, void *priv);
74 void (*worker_after_resume_fct)(struct urcu_workqueue *workqueue, void *priv);
75 void (*worker_before_wait_fct)(struct urcu_workqueue *workqueue, void *priv);
76 void (*worker_after_wake_up_fct)(struct urcu_workqueue *workqueue, void *priv);
77 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
78
79 struct urcu_workqueue_completion {
80 int barrier_count;
81 int32_t futex;
82 struct urcu_ref ref;
83 };
84
85 struct urcu_workqueue_completion_work {
86 struct urcu_work work;
87 struct urcu_workqueue_completion *completion;
88 };
89
90 /*
91 * Periodically retry setting CPU affinity if we migrate.
92 * Losing affinity can be caused by CPU hotunplug/hotplug, or by
93 * cpuset(7).
94 */
95 #ifdef HAVE_SCHED_SETAFFINITY
96 static int set_thread_cpu_affinity(struct urcu_workqueue *workqueue)
97 {
98 cpu_set_t mask;
99 int ret;
100
101 if (workqueue->cpu_affinity < 0)
102 return 0;
103 if (++workqueue->loop_count & SET_AFFINITY_CHECK_PERIOD_MASK)
104 return 0;
105 if (urcu_sched_getcpu() == workqueue->cpu_affinity)
106 return 0;
107
108 CPU_ZERO(&mask);
109 CPU_SET(workqueue->cpu_affinity, &mask);
110 ret = sched_setaffinity(0, sizeof(mask), &mask);
111
112 /*
113 * EINVAL is fine: can be caused by hotunplugged CPUs, or by
114 * cpuset(7). This is why we should always retry if we detect
115 * migration.
116 */
117 if (ret && errno == EINVAL) {
118 ret = 0;
119 errno = 0;
120 }
121 return ret;
122 }
123 #else
124 static int set_thread_cpu_affinity(struct urcu_workqueue *workqueue __attribute__((unused)))
125 {
126 return 0;
127 }
128 #endif
129
130 static void futex_wait(int32_t *futex)
131 {
132 /* Read condition before read futex */
133 cmm_smp_mb();
134 if (uatomic_read(futex) != -1)
135 return;
136 while (futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
137 switch (errno) {
138 case EWOULDBLOCK:
139 /* Value already changed. */
140 return;
141 case EINTR:
142 /* Retry if interrupted by signal. */
143 break; /* Get out of switch. */
144 default:
145 /* Unexpected error. */
146 urcu_die(errno);
147 }
148 }
149 }
150
151 static void futex_wake_up(int32_t *futex)
152 {
153 /* Write to condition before reading/writing futex */
154 cmm_smp_mb();
155 if (caa_unlikely(uatomic_read(futex) == -1)) {
156 uatomic_set(futex, 0);
157 if (futex_async(futex, FUTEX_WAKE, 1,
158 NULL, NULL, 0) < 0)
159 urcu_die(errno);
160 }
161 }
162
163 /* This is the code run by each worker thread. */
164
165 static void *workqueue_thread(void *arg)
166 {
167 unsigned long cbcount;
168 struct urcu_workqueue *workqueue = (struct urcu_workqueue *) arg;
169 int rt = !!(uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_RT);
170
171 if (set_thread_cpu_affinity(workqueue))
172 urcu_die(errno);
173
174 if (workqueue->initialize_worker_fct)
175 workqueue->initialize_worker_fct(workqueue, workqueue->priv);
176
177 if (!rt) {
178 uatomic_dec(&workqueue->futex);
179 /* Decrement futex before reading workqueue */
180 cmm_smp_mb();
181 }
182 for (;;) {
183 struct cds_wfcq_head cbs_tmp_head;
184 struct cds_wfcq_tail cbs_tmp_tail;
185 struct cds_wfcq_node *cbs, *cbs_tmp_n;
186 enum cds_wfcq_ret splice_ret;
187
188 if (set_thread_cpu_affinity(workqueue))
189 urcu_die(errno);
190
191 if (uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSE) {
192 /*
193 * Pause requested. Become quiescent: remove
194 * ourself from all global lists, and don't
195 * process any callback. The callback lists may
196 * still be non-empty though.
197 */
198 if (workqueue->worker_before_pause_fct)
199 workqueue->worker_before_pause_fct(workqueue, workqueue->priv);
200 cmm_smp_mb__before_uatomic_or();
201 uatomic_or(&workqueue->flags, URCU_WORKQUEUE_PAUSED);
202 while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSE) != 0)
203 (void) poll(NULL, 0, 1);
204 uatomic_and(&workqueue->flags, ~URCU_WORKQUEUE_PAUSED);
205 cmm_smp_mb__after_uatomic_and();
206 if (workqueue->worker_after_resume_fct)
207 workqueue->worker_after_resume_fct(workqueue, workqueue->priv);
208 }
209
210 cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail);
211 splice_ret = __cds_wfcq_splice_blocking(&cbs_tmp_head,
212 &cbs_tmp_tail, &workqueue->cbs_head, &workqueue->cbs_tail);
213 assert(splice_ret != CDS_WFCQ_RET_WOULDBLOCK);
214 assert(splice_ret != CDS_WFCQ_RET_DEST_NON_EMPTY);
215 if (splice_ret != CDS_WFCQ_RET_SRC_EMPTY) {
216 if (workqueue->grace_period_fct)
217 workqueue->grace_period_fct(workqueue, workqueue->priv);
218 cbcount = 0;
219 __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head,
220 &cbs_tmp_tail, cbs, cbs_tmp_n) {
221 struct urcu_work *uwp;
222
223 uwp = caa_container_of(cbs,
224 struct urcu_work, next);
225 uwp->func(uwp);
226 cbcount++;
227 }
228 uatomic_sub(&workqueue->qlen, cbcount);
229 }
230 if (uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_STOP)
231 break;
232 if (workqueue->worker_before_wait_fct)
233 workqueue->worker_before_wait_fct(workqueue, workqueue->priv);
234 if (!rt) {
235 if (cds_wfcq_empty(&workqueue->cbs_head,
236 &workqueue->cbs_tail)) {
237 futex_wait(&workqueue->futex);
238 uatomic_dec(&workqueue->futex);
239 /*
240 * Decrement futex before reading
241 * urcu_work list.
242 */
243 cmm_smp_mb();
244 }
245 } else {
246 if (cds_wfcq_empty(&workqueue->cbs_head,
247 &workqueue->cbs_tail)) {
248 (void) poll(NULL, 0, 10);
249 }
250 }
251 if (workqueue->worker_after_wake_up_fct)
252 workqueue->worker_after_wake_up_fct(workqueue, workqueue->priv);
253 }
254 if (!rt) {
255 /*
256 * Read urcu_work list before write futex.
257 */
258 cmm_smp_mb();
259 uatomic_set(&workqueue->futex, 0);
260 }
261 if (workqueue->finalize_worker_fct)
262 workqueue->finalize_worker_fct(workqueue, workqueue->priv);
263 return NULL;
264 }
265
266 struct urcu_workqueue *urcu_workqueue_create(unsigned long flags,
267 int cpu_affinity, void *priv,
268 void (*grace_period_fct)(struct urcu_workqueue *workqueue, void *priv),
269 void (*initialize_worker_fct)(struct urcu_workqueue *workqueue, void *priv),
270 void (*finalize_worker_fct)(struct urcu_workqueue *workqueue, void *priv),
271 void (*worker_before_wait_fct)(struct urcu_workqueue *workqueue, void *priv),
272 void (*worker_after_wake_up_fct)(struct urcu_workqueue *workqueue, void *priv),
273 void (*worker_before_pause_fct)(struct urcu_workqueue *workqueue, void *priv),
274 void (*worker_after_resume_fct)(struct urcu_workqueue *workqueue, void *priv))
275 {
276 struct urcu_workqueue *workqueue;
277 int ret;
278
279 workqueue = malloc(sizeof(*workqueue));
280 if (workqueue == NULL)
281 urcu_die(errno);
282 memset(workqueue, '\0', sizeof(*workqueue));
283 cds_wfcq_init(&workqueue->cbs_head, &workqueue->cbs_tail);
284 workqueue->qlen = 0;
285 workqueue->futex = 0;
286 workqueue->flags = flags;
287 workqueue->priv = priv;
288 workqueue->grace_period_fct = grace_period_fct;
289 workqueue->initialize_worker_fct = initialize_worker_fct;
290 workqueue->finalize_worker_fct = finalize_worker_fct;
291 workqueue->worker_before_wait_fct = worker_before_wait_fct;
292 workqueue->worker_after_wake_up_fct = worker_after_wake_up_fct;
293 workqueue->worker_before_pause_fct = worker_before_pause_fct;
294 workqueue->worker_after_resume_fct = worker_after_resume_fct;
295 workqueue->cpu_affinity = cpu_affinity;
296 workqueue->loop_count = 0;
297 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
298 ret = pthread_create(&workqueue->tid, NULL, workqueue_thread, workqueue);
299 if (ret) {
300 urcu_die(ret);
301 }
302 return workqueue;
303 }
304
305 static void wake_worker_thread(struct urcu_workqueue *workqueue)
306 {
307 if (!(_CMM_LOAD_SHARED(workqueue->flags) & URCU_WORKQUEUE_RT))
308 futex_wake_up(&workqueue->futex);
309 }
310
311 static int urcu_workqueue_destroy_worker(struct urcu_workqueue *workqueue)
312 {
313 int ret;
314 void *retval;
315
316 uatomic_or(&workqueue->flags, URCU_WORKQUEUE_STOP);
317 wake_worker_thread(workqueue);
318
319 ret = pthread_join(workqueue->tid, &retval);
320 if (ret) {
321 urcu_die(ret);
322 }
323 if (retval != NULL) {
324 urcu_die(EINVAL);
325 }
326 workqueue->flags &= ~URCU_WORKQUEUE_STOP;
327 workqueue->tid = 0;
328 return 0;
329 }
330
331 void urcu_workqueue_destroy(struct urcu_workqueue *workqueue)
332 {
333 if (workqueue == NULL) {
334 return;
335 }
336 if (urcu_workqueue_destroy_worker(workqueue)) {
337 urcu_die(errno);
338 }
339 assert(cds_wfcq_empty(&workqueue->cbs_head, &workqueue->cbs_tail));
340 free(workqueue);
341 }
342
343 void urcu_workqueue_queue_work(struct urcu_workqueue *workqueue,
344 struct urcu_work *work,
345 void (*func)(struct urcu_work *work))
346 {
347 cds_wfcq_node_init(&work->next);
348 work->func = func;
349 cds_wfcq_enqueue(&workqueue->cbs_head, &workqueue->cbs_tail, &work->next);
350 uatomic_inc(&workqueue->qlen);
351 wake_worker_thread(workqueue);
352 }
353
354 static
355 void free_completion(struct urcu_ref *ref)
356 {
357 struct urcu_workqueue_completion *completion;
358
359 completion = caa_container_of(ref, struct urcu_workqueue_completion, ref);
360 free(completion);
361 }
362
363 static
364 void _urcu_workqueue_wait_complete(struct urcu_work *work)
365 {
366 struct urcu_workqueue_completion_work *completion_work;
367 struct urcu_workqueue_completion *completion;
368
369 completion_work = caa_container_of(work, struct urcu_workqueue_completion_work, work);
370 completion = completion_work->completion;
371 if (!uatomic_sub_return(&completion->barrier_count, 1))
372 futex_wake_up(&completion->futex);
373 urcu_ref_put(&completion->ref, free_completion);
374 free(completion_work);
375 }
376
377 struct urcu_workqueue_completion *urcu_workqueue_create_completion(void)
378 {
379 struct urcu_workqueue_completion *completion;
380
381 completion = calloc(sizeof(*completion), 1);
382 if (!completion)
383 urcu_die(errno);
384 urcu_ref_set(&completion->ref, 1);
385 completion->barrier_count = 0;
386 return completion;
387 }
388
389 void urcu_workqueue_destroy_completion(struct urcu_workqueue_completion *completion)
390 {
391 urcu_ref_put(&completion->ref, free_completion);
392 }
393
394 void urcu_workqueue_wait_completion(struct urcu_workqueue_completion *completion)
395 {
396 /* Wait for them */
397 for (;;) {
398 uatomic_dec(&completion->futex);
399 /* Decrement futex before reading barrier_count */
400 cmm_smp_mb();
401 if (!uatomic_read(&completion->barrier_count))
402 break;
403 futex_wait(&completion->futex);
404 }
405 }
406
407 void urcu_workqueue_queue_completion(struct urcu_workqueue *workqueue,
408 struct urcu_workqueue_completion *completion)
409 {
410 struct urcu_workqueue_completion_work *work;
411
412 work = calloc(sizeof(*work), 1);
413 if (!work)
414 urcu_die(errno);
415 work->completion = completion;
416 urcu_ref_get(&completion->ref);
417 uatomic_inc(&completion->barrier_count);
418 urcu_workqueue_queue_work(workqueue, &work->work, _urcu_workqueue_wait_complete);
419 }
420
421 /*
422 * Wait for all in-flight work to complete execution.
423 */
424 void urcu_workqueue_flush_queued_work(struct urcu_workqueue *workqueue)
425 {
426 struct urcu_workqueue_completion *completion;
427
428 completion = urcu_workqueue_create_completion();
429 if (!completion)
430 urcu_die(ENOMEM);
431 urcu_workqueue_queue_completion(workqueue, completion);
432 urcu_workqueue_wait_completion(completion);
433 urcu_workqueue_destroy_completion(completion);
434 }
435
436 /* To be used in before fork handler. */
437 void urcu_workqueue_pause_worker(struct urcu_workqueue *workqueue)
438 {
439 uatomic_or(&workqueue->flags, URCU_WORKQUEUE_PAUSE);
440 cmm_smp_mb__after_uatomic_or();
441 wake_worker_thread(workqueue);
442
443 while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSED) == 0)
444 (void) poll(NULL, 0, 1);
445 }
446
447 /* To be used in after fork parent handler. */
448 void urcu_workqueue_resume_worker(struct urcu_workqueue *workqueue)
449 {
450 uatomic_and(&workqueue->flags, ~URCU_WORKQUEUE_PAUSE);
451 while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSED) != 0)
452 (void) poll(NULL, 0, 1);
453 }
454
455 void urcu_workqueue_create_worker(struct urcu_workqueue *workqueue)
456 {
457 int ret;
458
459 /* Clear workqueue state from parent. */
460 workqueue->flags &= ~URCU_WORKQUEUE_PAUSED;
461 workqueue->flags &= ~URCU_WORKQUEUE_PAUSE;
462 workqueue->tid = 0;
463 ret = pthread_create(&workqueue->tid, NULL, workqueue_thread, workqueue);
464 if (ret) {
465 urcu_die(ret);
466 }
467 }
This page took 0.061319 seconds and 4 git commands to generate.