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