Fix: urcu-wait: futex wait: handle spurious futex wakeups
[userspace-rcu.git] / src / urcu-defer-impl.h
CommitLineData
0376e7b2
PM
1#ifndef _URCU_DEFER_IMPL_H
2#define _URCU_DEFER_IMPL_H
3
786ee85b 4/*
0376e7b2
PM
5 * urcu-defer-impl.h
6 *
7 * Userspace RCU header - memory reclamation.
786ee85b 8 *
0376e7b2
PM
9 * TO BE INCLUDED ONLY FROM URCU LIBRARY CODE. See urcu-defer.h for linking
10 * dynamically with the userspace rcu reclamation library.
786ee85b 11 *
6982d6d7 12 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
0376e7b2 13 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
786ee85b
MD
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0376e7b2
PM
28 *
29 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
786ee85b
MD
30 */
31
0376e7b2 32#include <stdlib.h>
786ee85b 33#include <pthread.h>
0376e7b2 34#include <stdio.h>
786ee85b
MD
35#include <signal.h>
36#include <assert.h>
786ee85b
MD
37#include <string.h>
38#include <errno.h>
39#include <poll.h>
4ce9e4f2 40#include <sys/time.h>
4ce9e4f2 41#include <unistd.h>
6d841bc2 42#include <stdint.h>
786ee85b 43
41849996 44#include "urcu/futex.h"
0376e7b2
PM
45
46#include <urcu/compiler.h>
47#include <urcu/arch.h>
a2e7bf9c 48#include <urcu/uatomic.h>
0376e7b2
PM
49#include <urcu/list.h>
50#include <urcu/system.h>
bd252a04 51#include <urcu/tls-compat.h>
4a6d7378 52#include "urcu-die.h"
4477a870 53#include "urcu-utils.h"
0376e7b2
PM
54
55/*
56 * Number of entries in the per-thread defer queue. Must be power of 2.
57 */
58#define DEFER_QUEUE_SIZE (1 << 12)
59#define DEFER_QUEUE_MASK (DEFER_QUEUE_SIZE - 1)
60
61/*
62 * Typically, data is aligned at least on the architecture size.
63 * Use lowest bit to indicate that the current callback is changing.
64 * Assumes that (void *)-2L is not used often. Used to encode non-aligned
65 * functions and non-aligned data using extra space.
66 * We encode the (void *)-2L fct as: -2L, fct, data.
2ef1bfb2
LJ
67 * We encode the (void *)-2L data as either:
68 * fct | DQ_FCT_BIT, data (if fct is aligned), or
69 * -2L, fct, data (if fct is not aligned).
0376e7b2
PM
70 * Here, DQ_FCT_MARK == ~DQ_FCT_BIT. Required for the test order.
71 */
72#define DQ_FCT_BIT (1 << 0)
73#define DQ_IS_FCT_BIT(x) ((unsigned long)(x) & DQ_FCT_BIT)
74#define DQ_SET_FCT_BIT(x) \
75 (x = (void *)((unsigned long)(x) | DQ_FCT_BIT))
76#define DQ_CLEAR_FCT_BIT(x) \
77 (x = (void *)((unsigned long)(x) & ~DQ_FCT_BIT))
78#define DQ_FCT_MARK ((void *)(~DQ_FCT_BIT))
79
80/*
81 * This code section can only be included in LGPL 2.1 compatible source code.
82 * See below for the function call wrappers which can be used in code meant to
83 * be only linked with the Userspace RCU library. This comes with a small
84 * performance degradation on the read-side due to the added function calls.
85 * This is required to permit relinking with newer versions of the library.
86 */
87
0376e7b2
PM
88/*
89 * defer queue.
90 * Contains pointers. Encoded to save space when same callback is often used.
91 * When looking up the next item:
92 * - if DQ_FCT_BIT is set, set the current callback to DQ_CLEAR_FCT_BIT(ptr)
93 * - next element contains pointer to data.
94 * - else if item == DQ_FCT_MARK
95 * - set the current callback to next element ptr
96 * - following next element contains pointer to data.
97 * - else current element contains data
98 */
99struct defer_queue {
100 unsigned long head; /* add element at head */
101 void *last_fct_in; /* last fct pointer encoded */
102 unsigned long tail; /* next element to remove at tail */
103 void *last_fct_out; /* last fct pointer encoded */
104 void **q;
105 /* registry information */
106 unsigned long last_head;
107 struct cds_list_head list; /* list of thread queues */
108};
109
786ee85b 110/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
4477a870 111#include <urcu/defer.h>
786ee85b 112
02be5561 113void __attribute__((destructor)) rcu_defer_exit(void);
786ee85b
MD
114
115extern void synchronize_rcu(void);
116
117/*
02be5561 118 * rcu_defer_mutex nests inside defer_thread_mutex.
786ee85b 119 */
02be5561 120static pthread_mutex_t rcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
786ee85b
MD
121static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
122
6d841bc2 123static int32_t defer_thread_futex;
d7ff6cee 124static int32_t defer_thread_stop;
4ce9e4f2 125
786ee85b
MD
126/*
127 * Written to only by each individual deferer. Read by both the deferer and
128 * the reclamation tread.
129 */
2f661865 130static DEFINE_URCU_TLS(struct defer_queue, defer_queue);
0376e7b2 131static CDS_LIST_HEAD(registry_defer);
786ee85b 132static pthread_t tid_defer;
4ce9e4f2 133
0376e7b2 134static void mutex_lock_defer(pthread_mutex_t *mutex)
786ee85b
MD
135{
136 int ret;
137
138#ifndef DISTRUST_SIGNALS_EXTREME
139 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
140 if (ret)
141 urcu_die(ret);
786ee85b
MD
142#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
143 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
144 if (ret != EBUSY && ret != EINTR)
145 urcu_die(ret);
c060776f 146 (void) poll(NULL,0,10);
786ee85b
MD
147 }
148#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
149}
150
04eb9c4f
MD
151/*
152 * Wake-up any waiting defer thread. Called from many concurrent threads.
153 */
154static void wake_up_defer(void)
155{
a0b7f7ea 156 if (caa_unlikely(uatomic_read(&defer_thread_futex) == -1)) {
ec4e58a3 157 uatomic_set(&defer_thread_futex, 0);
b0a841b4
MD
158 if (futex_noasync(&defer_thread_futex, FUTEX_WAKE, 1,
159 NULL, NULL, 0) < 0)
160 urcu_die(errno);
04eb9c4f
MD
161 }
162}
163
164static unsigned long rcu_defer_num_callbacks(void)
165{
166 unsigned long num_items = 0, head;
dbc6128f 167 struct defer_queue *index;
04eb9c4f 168
0376e7b2
PM
169 mutex_lock_defer(&rcu_defer_mutex);
170 cds_list_for_each_entry(index, &registry_defer, list) {
6cf3827c 171 head = CMM_LOAD_SHARED(index->head);
dbc6128f 172 num_items += head - index->tail;
04eb9c4f 173 }
6abb4bd5 174 mutex_unlock(&rcu_defer_mutex);
04eb9c4f
MD
175 return num_items;
176}
177
178/*
179 * Defer thread waiting. Single thread.
180 */
181static void wait_defer(void)
182{
ec4e58a3 183 uatomic_dec(&defer_thread_futex);
d7ff6cee
MD
184 /* Write futex before read queue */
185 /* Write futex before read defer_thread_stop */
186 cmm_smp_mb();
187 if (_CMM_LOAD_SHARED(defer_thread_stop)) {
188 uatomic_set(&defer_thread_futex, 0);
189 pthread_exit(0);
190 }
04eb9c4f 191 if (rcu_defer_num_callbacks()) {
5481ddb3 192 cmm_smp_mb(); /* Read queue before write futex */
04eb9c4f 193 /* Callbacks are queued, don't wait. */
ec4e58a3 194 uatomic_set(&defer_thread_futex, 0);
04eb9c4f 195 } else {
5481ddb3 196 cmm_smp_rmb(); /* Read queue before read futex */
feb97797
MD
197 while (uatomic_read(&defer_thread_futex) == -1) {
198 if (!futex_noasync(&defer_thread_futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
199 /*
200 * Prior queued wakeups queued by unrelated code
201 * using the same address can cause futex wait to
202 * return 0 even through the futex value is still
203 * -1 (spurious wakeups). Check the value again
204 * in user-space to validate whether it really
205 * differs from -1.
206 */
207 continue;
208 }
b0a841b4 209 switch (errno) {
feb97797 210 case EAGAIN:
b0a841b4
MD
211 /* Value already changed. */
212 return;
213 case EINTR:
214 /* Retry if interrupted by signal. */
feb97797 215 break; /* Get out of switch. Check again. */
b0a841b4
MD
216 default:
217 /* Unexpected error. */
218 urcu_die(errno);
219 }
220 }
04eb9c4f
MD
221 }
222}
223
786ee85b
MD
224/*
225 * Must be called after Q.S. is reached.
226 */
227static void rcu_defer_barrier_queue(struct defer_queue *queue,
804b4375 228 unsigned long head)
786ee85b
MD
229{
230 unsigned long i;
804b4375
MD
231 void (*fct)(void *p);
232 void *p;
786ee85b
MD
233
234 /*
235 * Tail is only modified when lock is held.
236 * Head is only modified by owner thread.
237 */
238
804b4375 239 for (i = queue->tail; i != head;) {
5481ddb3 240 cmm_smp_rmb(); /* read head before q[]. */
6cf3827c 241 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
a0b7f7ea 242 if (caa_unlikely(DQ_IS_FCT_BIT(p))) {
804b4375
MD
243 DQ_CLEAR_FCT_BIT(p);
244 queue->last_fct_out = p;
6cf3827c 245 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
a0b7f7ea 246 } else if (caa_unlikely(p == DQ_FCT_MARK)) {
6cf3827c 247 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
804b4375 248 queue->last_fct_out = p;
6cf3827c 249 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
29cdb8d8 250 }
804b4375 251 fct = queue->last_fct_out;
804b4375 252 fct(p);
786ee85b 253 }
5481ddb3 254 cmm_smp_mb(); /* push tail after having used q[] */
6cf3827c 255 CMM_STORE_SHARED(queue->tail, i);
786ee85b
MD
256}
257
258static void _rcu_defer_barrier_thread(void)
259{
0d0e6c21 260 unsigned long head, num_items;
786ee85b 261
bd252a04
MD
262 head = URCU_TLS(defer_queue).head;
263 num_items = head - URCU_TLS(defer_queue).tail;
a0b7f7ea 264 if (caa_unlikely(!num_items))
0d0e6c21 265 return;
786ee85b 266 synchronize_rcu();
bd252a04 267 rcu_defer_barrier_queue(&URCU_TLS(defer_queue), head);
786ee85b
MD
268}
269
786ee85b
MD
270void rcu_defer_barrier_thread(void)
271{
0376e7b2 272 mutex_lock_defer(&rcu_defer_mutex);
786ee85b 273 _rcu_defer_barrier_thread();
6abb4bd5 274 mutex_unlock(&rcu_defer_mutex);
786ee85b 275}
ce28e67a 276URCU_ATTR_ALIAS(urcu_stringify(rcu_defer_barrier_thread))
4477a870 277void alias_rcu_defer_barrier_thread();
786ee85b 278
0d0e6c21
MD
279/*
280 * rcu_defer_barrier - Execute all queued rcu callbacks.
281 *
282 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
283 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
284 * are guaranteed to be executed.
285 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
286 * execution are not guaranteed to be executed in the current batch (could
287 * be left for the next batch). These callbacks queued by other threads are only
288 * guaranteed to be executed if there is explicit synchronization between
289 * the thread adding to the queue and the thread issuing the defer_barrier call.
290 */
291
786ee85b
MD
292void rcu_defer_barrier(void)
293{
dbc6128f 294 struct defer_queue *index;
0d0e6c21 295 unsigned long num_items = 0;
786ee85b 296
0376e7b2 297 if (cds_list_empty(&registry_defer))
786ee85b
MD
298 return;
299
0376e7b2
PM
300 mutex_lock_defer(&rcu_defer_mutex);
301 cds_list_for_each_entry(index, &registry_defer, list) {
6cf3827c 302 index->last_head = CMM_LOAD_SHARED(index->head);
dbc6128f 303 num_items += index->last_head - index->tail;
0d0e6c21 304 }
a0b7f7ea 305 if (caa_likely(!num_items)) {
0d0e6c21
MD
306 /*
307 * We skip the grace period because there are no queued
308 * callbacks to execute.
309 */
310 goto end;
311 }
786ee85b 312 synchronize_rcu();
0376e7b2 313 cds_list_for_each_entry(index, &registry_defer, list)
dbc6128f 314 rcu_defer_barrier_queue(index, index->last_head);
0d0e6c21 315end:
6abb4bd5 316 mutex_unlock(&rcu_defer_mutex);
786ee85b 317}
ce28e67a 318URCU_ATTR_ALIAS(urcu_stringify(rcu_defer_barrier))
4477a870 319void alias_rcu_defer_barrier();
786ee85b 320
2c22932b 321/*
b4f313b7 322 * _defer_rcu - Queue a RCU callback.
2c22932b 323 */
f50c11aa 324static void _defer_rcu(void (*fct)(void *p), void *p)
2c22932b
MD
325{
326 unsigned long head, tail;
327
328 /*
329 * Head is only modified by ourself. Tail can be modified by reclamation
330 * thread.
331 */
bd252a04
MD
332 head = URCU_TLS(defer_queue).head;
333 tail = CMM_LOAD_SHARED(URCU_TLS(defer_queue).tail);
2c22932b
MD
334
335 /*
ec8e44cf 336 * If queue is full, or reached threshold. Empty queue ourself.
2c22932b
MD
337 * Worse-case: must allow 2 supplementary entries for fct pointer.
338 */
a0b7f7ea 339 if (caa_unlikely(head - tail >= DEFER_QUEUE_SIZE - 2)) {
2c22932b
MD
340 assert(head - tail <= DEFER_QUEUE_SIZE);
341 rcu_defer_barrier_thread();
bd252a04 342 assert(head - CMM_LOAD_SHARED(URCU_TLS(defer_queue).tail) == 0);
2c22932b
MD
343 }
344
2ef1bfb2
LJ
345 /*
346 * Encode:
347 * if the function is not changed and the data is aligned and it is
348 * not the marker:
349 * store the data
350 * otherwise if the function is aligned and its not the marker:
351 * store the function with DQ_FCT_BIT
352 * store the data
353 * otherwise:
354 * store the marker (DQ_FCT_MARK)
355 * store the function
356 * store the data
357 *
358 * Decode: see the comments before 'struct defer_queue'
359 * or the code in rcu_defer_barrier_queue().
360 */
bd252a04 361 if (caa_unlikely(URCU_TLS(defer_queue).last_fct_in != fct
2ef1bfb2
LJ
362 || DQ_IS_FCT_BIT(p)
363 || p == DQ_FCT_MARK)) {
bd252a04 364 URCU_TLS(defer_queue).last_fct_in = fct;
a0b7f7ea 365 if (caa_unlikely(DQ_IS_FCT_BIT(fct) || fct == DQ_FCT_MARK)) {
bd252a04 366 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
2c22932b 367 DQ_FCT_MARK);
bd252a04 368 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
2c22932b
MD
369 fct);
370 } else {
371 DQ_SET_FCT_BIT(fct);
bd252a04 372 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
2c22932b
MD
373 fct);
374 }
2c22932b 375 }
bd252a04 376 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK], p);
5481ddb3 377 cmm_smp_wmb(); /* Publish new pointer before head */
2c22932b 378 /* Write q[] before head. */
bd252a04 379 CMM_STORE_SHARED(URCU_TLS(defer_queue).head, head);
5481ddb3 380 cmm_smp_mb(); /* Write queue head before read futex */
2c22932b
MD
381 /*
382 * Wake-up any waiting defer thread.
383 */
384 wake_up_defer();
385}
386
17a8f206 387static void *thr_defer(void *args __attribute__((unused)))
786ee85b
MD
388{
389 for (;;) {
4ce9e4f2
MD
390 /*
391 * "Be green". Don't wake up the CPU if there is no RCU work
392 * to perform whatsoever. Aims at saving laptop battery life by
393 * leaving the processor in sleep state when idle.
394 */
4ce9e4f2 395 wait_defer();
4ce9e4f2 396 /* Sleeping after wait_defer to let many callbacks enqueue */
c060776f 397 (void) poll(NULL,0,100); /* wait for 100ms */
786ee85b
MD
398 rcu_defer_barrier();
399 }
400
401 return NULL;
402}
403
404/*
405 * library wrappers to be used by non-LGPL compatible source code.
406 */
407
3614f13c 408void defer_rcu(void (*fct)(void *p), void *p)
786ee85b 409{
3614f13c 410 _defer_rcu(fct, p);
786ee85b 411}
ce28e67a 412URCU_ATTR_ALIAS(urcu_stringify(defer_rcu)) void alias_defer_rcu();
786ee85b 413
786ee85b
MD
414static void start_defer_thread(void)
415{
416 int ret;
417
dbc6128f 418 ret = pthread_create(&tid_defer, NULL, thr_defer, NULL);
786ee85b
MD
419 assert(!ret);
420}
421
422static void stop_defer_thread(void)
423{
424 int ret;
425 void *tret;
426
d7ff6cee
MD
427 _CMM_STORE_SHARED(defer_thread_stop, 1);
428 /* Store defer_thread_stop before testing futex */
429 cmm_smp_mb();
4ce9e4f2 430 wake_up_defer();
d7ff6cee 431
786ee85b
MD
432 ret = pthread_join(tid_defer, &tret);
433 assert(!ret);
d7ff6cee
MD
434
435 CMM_STORE_SHARED(defer_thread_stop, 0);
436 /* defer thread should always exit when futex value is 0 */
437 assert(uatomic_read(&defer_thread_futex) == 0);
786ee85b
MD
438}
439
7fdbbd61 440int rcu_defer_register_thread(void)
786ee85b 441{
dbc6128f
MD
442 int was_empty;
443
bd252a04
MD
444 assert(URCU_TLS(defer_queue).last_head == 0);
445 assert(URCU_TLS(defer_queue).q == NULL);
446 URCU_TLS(defer_queue).q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE);
447 if (!URCU_TLS(defer_queue).q)
7fdbbd61 448 return -ENOMEM;
786ee85b 449
0376e7b2
PM
450 mutex_lock_defer(&defer_thread_mutex);
451 mutex_lock_defer(&rcu_defer_mutex);
452 was_empty = cds_list_empty(&registry_defer);
bd252a04 453 cds_list_add(&URCU_TLS(defer_queue).list, &registry_defer);
6abb4bd5 454 mutex_unlock(&rcu_defer_mutex);
786ee85b 455
dbc6128f 456 if (was_empty)
786ee85b 457 start_defer_thread();
6abb4bd5 458 mutex_unlock(&defer_thread_mutex);
7fdbbd61 459 return 0;
786ee85b 460}
ce28e67a 461URCU_ATTR_ALIAS(urcu_stringify(rcu_defer_register_thread))
4477a870 462int alias_rcu_defer_register_thread();
786ee85b
MD
463
464void rcu_defer_unregister_thread(void)
465{
dbc6128f 466 int is_empty;
786ee85b 467
0376e7b2
PM
468 mutex_lock_defer(&defer_thread_mutex);
469 mutex_lock_defer(&rcu_defer_mutex);
bd252a04 470 cds_list_del(&URCU_TLS(defer_queue).list);
786ee85b 471 _rcu_defer_barrier_thread();
bd252a04
MD
472 free(URCU_TLS(defer_queue).q);
473 URCU_TLS(defer_queue).q = NULL;
0376e7b2 474 is_empty = cds_list_empty(&registry_defer);
6abb4bd5 475 mutex_unlock(&rcu_defer_mutex);
786ee85b 476
dbc6128f 477 if (is_empty)
786ee85b 478 stop_defer_thread();
6abb4bd5 479 mutex_unlock(&defer_thread_mutex);
786ee85b 480}
ce28e67a 481URCU_ATTR_ALIAS(urcu_stringify(rcu_defer_unregister_thread))
4477a870 482void alias_rcu_defer_unregister_thread();
786ee85b 483
02be5561 484void rcu_defer_exit(void)
786ee85b 485{
0376e7b2 486 assert(cds_list_empty(&registry_defer));
786ee85b 487}
ce28e67a 488URCU_ATTR_ALIAS(urcu_stringify(rcu_defer_exit))
4477a870 489void alias_rcu_defer_exit();
0376e7b2
PM
490
491#endif /* _URCU_DEFER_IMPL_H */
This page took 0.083822 seconds and 4 git commands to generate.