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