Fix: Remove include urcu/assert.h
[userspace-rcu.git] / src / workqueue.c
CommitLineData
ccacf27f
MD
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"
6cd23d47
MD
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>
ccacf27f
MD
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
54struct urcu_workqueue {
55 /*
56 * We do not align head on a different cache-line than tail
1b6fc173
MD
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.
ccacf27f
MD
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
79struct urcu_workqueue_completion {
80 int barrier_count;
81 int32_t futex;
82 struct urcu_ref ref;
83};
84
85struct 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 */
a9e31859 95#ifdef HAVE_SCHED_SETAFFINITY
ccacf27f
MD
96static 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#if SCHED_SETAFFINITY_ARGS == 2
111 ret = sched_setaffinity(0, &mask);
112#else
113 ret = sched_setaffinity(0, sizeof(mask), &mask);
114#endif
115 /*
116 * EINVAL is fine: can be caused by hotunplugged CPUs, or by
117 * cpuset(7). This is why we should always retry if we detect
118 * migration.
119 */
120 if (ret && errno == EINVAL) {
121 ret = 0;
122 errno = 0;
123 }
124 return ret;
125}
126#else
0d657320 127static int set_thread_cpu_affinity(struct urcu_workqueue *workqueue __attribute__((unused)))
ccacf27f
MD
128{
129 return 0;
130}
131#endif
132
133static void futex_wait(int32_t *futex)
134{
135 /* Read condition before read futex */
136 cmm_smp_mb();
998a67fe
MD
137 while (uatomic_read(futex) == -1) {
138 if (!futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
139 /*
140 * Prior queued wakeups queued by unrelated code
141 * using the same address can cause futex wait to
142 * return 0 even through the futex value is still
143 * -1 (spurious wakeups). Check the value again
144 * in user-space to validate whether it really
145 * differs from -1.
146 */
147 continue;
148 }
ccacf27f 149 switch (errno) {
998a67fe 150 case EAGAIN:
ccacf27f
MD
151 /* Value already changed. */
152 return;
153 case EINTR:
154 /* Retry if interrupted by signal. */
998a67fe 155 break; /* Get out of switch. Check again. */
ccacf27f
MD
156 default:
157 /* Unexpected error. */
158 urcu_die(errno);
159 }
160 }
161}
162
163static void futex_wake_up(int32_t *futex)
164{
165 /* Write to condition before reading/writing futex */
166 cmm_smp_mb();
167 if (caa_unlikely(uatomic_read(futex) == -1)) {
168 uatomic_set(futex, 0);
169 if (futex_async(futex, FUTEX_WAKE, 1,
170 NULL, NULL, 0) < 0)
171 urcu_die(errno);
172 }
173}
174
175/* This is the code run by each worker thread. */
176
177static void *workqueue_thread(void *arg)
178{
179 unsigned long cbcount;
180 struct urcu_workqueue *workqueue = (struct urcu_workqueue *) arg;
181 int rt = !!(uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_RT);
182
183 if (set_thread_cpu_affinity(workqueue))
184 urcu_die(errno);
185
186 if (workqueue->initialize_worker_fct)
187 workqueue->initialize_worker_fct(workqueue, workqueue->priv);
188
189 if (!rt) {
190 uatomic_dec(&workqueue->futex);
191 /* Decrement futex before reading workqueue */
192 cmm_smp_mb();
193 }
194 for (;;) {
195 struct cds_wfcq_head cbs_tmp_head;
196 struct cds_wfcq_tail cbs_tmp_tail;
197 struct cds_wfcq_node *cbs, *cbs_tmp_n;
198 enum cds_wfcq_ret splice_ret;
199
200 if (set_thread_cpu_affinity(workqueue))
201 urcu_die(errno);
202
203 if (uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSE) {
204 /*
205 * Pause requested. Become quiescent: remove
206 * ourself from all global lists, and don't
207 * process any callback. The callback lists may
208 * still be non-empty though.
209 */
210 if (workqueue->worker_before_pause_fct)
211 workqueue->worker_before_pause_fct(workqueue, workqueue->priv);
212 cmm_smp_mb__before_uatomic_or();
213 uatomic_or(&workqueue->flags, URCU_WORKQUEUE_PAUSED);
214 while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSE) != 0)
215 (void) poll(NULL, 0, 1);
216 uatomic_and(&workqueue->flags, ~URCU_WORKQUEUE_PAUSED);
217 cmm_smp_mb__after_uatomic_and();
218 if (workqueue->worker_after_resume_fct)
219 workqueue->worker_after_resume_fct(workqueue, workqueue->priv);
220 }
221
222 cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail);
223 splice_ret = __cds_wfcq_splice_blocking(&cbs_tmp_head,
224 &cbs_tmp_tail, &workqueue->cbs_head, &workqueue->cbs_tail);
225 assert(splice_ret != CDS_WFCQ_RET_WOULDBLOCK);
226 assert(splice_ret != CDS_WFCQ_RET_DEST_NON_EMPTY);
227 if (splice_ret != CDS_WFCQ_RET_SRC_EMPTY) {
228 if (workqueue->grace_period_fct)
229 workqueue->grace_period_fct(workqueue, workqueue->priv);
230 cbcount = 0;
231 __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head,
232 &cbs_tmp_tail, cbs, cbs_tmp_n) {
d25f25df 233 struct urcu_work *uwp;
ccacf27f 234
d25f25df
MD
235 uwp = caa_container_of(cbs,
236 struct urcu_work, next);
237 uwp->func(uwp);
ccacf27f
MD
238 cbcount++;
239 }
240 uatomic_sub(&workqueue->qlen, cbcount);
241 }
242 if (uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_STOP)
243 break;
244 if (workqueue->worker_before_wait_fct)
245 workqueue->worker_before_wait_fct(workqueue, workqueue->priv);
246 if (!rt) {
247 if (cds_wfcq_empty(&workqueue->cbs_head,
248 &workqueue->cbs_tail)) {
249 futex_wait(&workqueue->futex);
ccacf27f
MD
250 uatomic_dec(&workqueue->futex);
251 /*
252 * Decrement futex before reading
1b6fc173 253 * urcu_work list.
ccacf27f
MD
254 */
255 cmm_smp_mb();
ccacf27f
MD
256 }
257 } else {
5bbe95bb
JG
258 if (cds_wfcq_empty(&workqueue->cbs_head,
259 &workqueue->cbs_tail)) {
260 (void) poll(NULL, 0, 10);
261 }
ccacf27f
MD
262 }
263 if (workqueue->worker_after_wake_up_fct)
264 workqueue->worker_after_wake_up_fct(workqueue, workqueue->priv);
265 }
266 if (!rt) {
267 /*
1b6fc173 268 * Read urcu_work list before write futex.
ccacf27f
MD
269 */
270 cmm_smp_mb();
271 uatomic_set(&workqueue->futex, 0);
272 }
273 if (workqueue->finalize_worker_fct)
274 workqueue->finalize_worker_fct(workqueue, workqueue->priv);
275 return NULL;
276}
277
278struct urcu_workqueue *urcu_workqueue_create(unsigned long flags,
279 int cpu_affinity, void *priv,
280 void (*grace_period_fct)(struct urcu_workqueue *workqueue, void *priv),
281 void (*initialize_worker_fct)(struct urcu_workqueue *workqueue, void *priv),
282 void (*finalize_worker_fct)(struct urcu_workqueue *workqueue, void *priv),
283 void (*worker_before_wait_fct)(struct urcu_workqueue *workqueue, void *priv),
284 void (*worker_after_wake_up_fct)(struct urcu_workqueue *workqueue, void *priv),
285 void (*worker_before_pause_fct)(struct urcu_workqueue *workqueue, void *priv),
286 void (*worker_after_resume_fct)(struct urcu_workqueue *workqueue, void *priv))
287{
288 struct urcu_workqueue *workqueue;
289 int ret;
290
291 workqueue = malloc(sizeof(*workqueue));
292 if (workqueue == NULL)
293 urcu_die(errno);
294 memset(workqueue, '\0', sizeof(*workqueue));
295 cds_wfcq_init(&workqueue->cbs_head, &workqueue->cbs_tail);
296 workqueue->qlen = 0;
297 workqueue->futex = 0;
298 workqueue->flags = flags;
299 workqueue->priv = priv;
300 workqueue->grace_period_fct = grace_period_fct;
301 workqueue->initialize_worker_fct = initialize_worker_fct;
302 workqueue->finalize_worker_fct = finalize_worker_fct;
303 workqueue->worker_before_wait_fct = worker_before_wait_fct;
304 workqueue->worker_after_wake_up_fct = worker_after_wake_up_fct;
305 workqueue->worker_before_pause_fct = worker_before_pause_fct;
306 workqueue->worker_after_resume_fct = worker_after_resume_fct;
307 workqueue->cpu_affinity = cpu_affinity;
308 workqueue->loop_count = 0;
309 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
310 ret = pthread_create(&workqueue->tid, NULL, workqueue_thread, workqueue);
311 if (ret) {
312 urcu_die(ret);
313 }
314 return workqueue;
315}
316
317static void wake_worker_thread(struct urcu_workqueue *workqueue)
318{
9db9edcc 319 if (!(_CMM_LOAD_SHARED(workqueue->flags) & URCU_WORKQUEUE_RT))
ccacf27f
MD
320 futex_wake_up(&workqueue->futex);
321}
322
323static int urcu_workqueue_destroy_worker(struct urcu_workqueue *workqueue)
324{
325 int ret;
326 void *retval;
327
328 uatomic_or(&workqueue->flags, URCU_WORKQUEUE_STOP);
329 wake_worker_thread(workqueue);
330
331 ret = pthread_join(workqueue->tid, &retval);
332 if (ret) {
333 urcu_die(ret);
334 }
335 if (retval != NULL) {
336 urcu_die(EINVAL);
337 }
338 workqueue->flags &= ~URCU_WORKQUEUE_STOP;
339 workqueue->tid = 0;
340 return 0;
341}
342
343void urcu_workqueue_destroy(struct urcu_workqueue *workqueue)
344{
345 if (workqueue == NULL) {
346 return;
347 }
348 if (urcu_workqueue_destroy_worker(workqueue)) {
349 urcu_die(errno);
350 }
351 assert(cds_wfcq_empty(&workqueue->cbs_head, &workqueue->cbs_tail));
352 free(workqueue);
353}
354
355void urcu_workqueue_queue_work(struct urcu_workqueue *workqueue,
356 struct urcu_work *work,
357 void (*func)(struct urcu_work *work))
358{
359 cds_wfcq_node_init(&work->next);
360 work->func = func;
361 cds_wfcq_enqueue(&workqueue->cbs_head, &workqueue->cbs_tail, &work->next);
362 uatomic_inc(&workqueue->qlen);
363 wake_worker_thread(workqueue);
364}
365
366static
367void free_completion(struct urcu_ref *ref)
368{
369 struct urcu_workqueue_completion *completion;
370
371 completion = caa_container_of(ref, struct urcu_workqueue_completion, ref);
372 free(completion);
373}
374
375static
376void _urcu_workqueue_wait_complete(struct urcu_work *work)
377{
378 struct urcu_workqueue_completion_work *completion_work;
379 struct urcu_workqueue_completion *completion;
380
381 completion_work = caa_container_of(work, struct urcu_workqueue_completion_work, work);
382 completion = completion_work->completion;
383 if (!uatomic_sub_return(&completion->barrier_count, 1))
384 futex_wake_up(&completion->futex);
385 urcu_ref_put(&completion->ref, free_completion);
386 free(completion_work);
387}
388
389struct urcu_workqueue_completion *urcu_workqueue_create_completion(void)
390{
391 struct urcu_workqueue_completion *completion;
392
393 completion = calloc(sizeof(*completion), 1);
394 if (!completion)
395 urcu_die(errno);
396 urcu_ref_set(&completion->ref, 1);
397 completion->barrier_count = 0;
398 return completion;
399}
400
401void urcu_workqueue_destroy_completion(struct urcu_workqueue_completion *completion)
402{
403 urcu_ref_put(&completion->ref, free_completion);
404}
405
406void urcu_workqueue_wait_completion(struct urcu_workqueue_completion *completion)
407{
408 /* Wait for them */
409 for (;;) {
410 uatomic_dec(&completion->futex);
411 /* Decrement futex before reading barrier_count */
412 cmm_smp_mb();
413 if (!uatomic_read(&completion->barrier_count))
414 break;
415 futex_wait(&completion->futex);
416 }
417}
418
419void urcu_workqueue_queue_completion(struct urcu_workqueue *workqueue,
420 struct urcu_workqueue_completion *completion)
421{
422 struct urcu_workqueue_completion_work *work;
423
424 work = calloc(sizeof(*work), 1);
425 if (!work)
426 urcu_die(errno);
427 work->completion = completion;
428 urcu_ref_get(&completion->ref);
429 uatomic_inc(&completion->barrier_count);
430 urcu_workqueue_queue_work(workqueue, &work->work, _urcu_workqueue_wait_complete);
431}
432
433/*
434 * Wait for all in-flight work to complete execution.
435 */
436void urcu_workqueue_flush_queued_work(struct urcu_workqueue *workqueue)
437{
438 struct urcu_workqueue_completion *completion;
439
440 completion = urcu_workqueue_create_completion();
441 if (!completion)
442 urcu_die(ENOMEM);
443 urcu_workqueue_queue_completion(workqueue, completion);
444 urcu_workqueue_wait_completion(completion);
445 urcu_workqueue_destroy_completion(completion);
446}
447
448/* To be used in before fork handler. */
449void urcu_workqueue_pause_worker(struct urcu_workqueue *workqueue)
450{
451 uatomic_or(&workqueue->flags, URCU_WORKQUEUE_PAUSE);
452 cmm_smp_mb__after_uatomic_or();
453 wake_worker_thread(workqueue);
454
455 while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSED) == 0)
456 (void) poll(NULL, 0, 1);
457}
458
459/* To be used in after fork parent handler. */
460void urcu_workqueue_resume_worker(struct urcu_workqueue *workqueue)
461{
462 uatomic_and(&workqueue->flags, ~URCU_WORKQUEUE_PAUSE);
463 while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSED) != 0)
464 (void) poll(NULL, 0, 1);
465}
466
467void urcu_workqueue_create_worker(struct urcu_workqueue *workqueue)
468{
469 int ret;
470
471 /* Clear workqueue state from parent. */
472 workqueue->flags &= ~URCU_WORKQUEUE_PAUSED;
473 workqueue->flags &= ~URCU_WORKQUEUE_PAUSE;
474 workqueue->tid = 0;
475 ret = pthread_create(&workqueue->tid, NULL, workqueue_thread, workqueue);
476 if (ret) {
477 urcu_die(ret);
478 }
479}
This page took 0.063144 seconds and 4 git commands to generate.