doc/examples: cds_lfht_add_unique
[urcu.git] / 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"
0376e7b2
PM
53
54/*
55 * Number of entries in the per-thread defer queue. Must be power of 2.
56 */
57#define DEFER_QUEUE_SIZE (1 << 12)
58#define DEFER_QUEUE_MASK (DEFER_QUEUE_SIZE - 1)
59
60/*
61 * Typically, data is aligned at least on the architecture size.
62 * Use lowest bit to indicate that the current callback is changing.
63 * Assumes that (void *)-2L is not used often. Used to encode non-aligned
64 * functions and non-aligned data using extra space.
65 * We encode the (void *)-2L fct as: -2L, fct, data.
2ef1bfb2
LJ
66 * We encode the (void *)-2L data as either:
67 * fct | DQ_FCT_BIT, data (if fct is aligned), or
68 * -2L, fct, data (if fct is not aligned).
0376e7b2
PM
69 * Here, DQ_FCT_MARK == ~DQ_FCT_BIT. Required for the test order.
70 */
71#define DQ_FCT_BIT (1 << 0)
72#define DQ_IS_FCT_BIT(x) ((unsigned long)(x) & DQ_FCT_BIT)
73#define DQ_SET_FCT_BIT(x) \
74 (x = (void *)((unsigned long)(x) | DQ_FCT_BIT))
75#define DQ_CLEAR_FCT_BIT(x) \
76 (x = (void *)((unsigned long)(x) & ~DQ_FCT_BIT))
77#define DQ_FCT_MARK ((void *)(~DQ_FCT_BIT))
78
79/*
80 * This code section can only be included in LGPL 2.1 compatible source code.
81 * See below for the function call wrappers which can be used in code meant to
82 * be only linked with the Userspace RCU library. This comes with a small
83 * performance degradation on the read-side due to the added function calls.
84 * This is required to permit relinking with newer versions of the library.
85 */
86
87#ifdef DEBUG_RCU
88#define rcu_assert(args...) assert(args)
89#else
90#define rcu_assert(args...)
91#endif
92
93/*
94 * defer queue.
95 * Contains pointers. Encoded to save space when same callback is often used.
96 * When looking up the next item:
97 * - if DQ_FCT_BIT is set, set the current callback to DQ_CLEAR_FCT_BIT(ptr)
98 * - next element contains pointer to data.
99 * - else if item == DQ_FCT_MARK
100 * - set the current callback to next element ptr
101 * - following next element contains pointer to data.
102 * - else current element contains data
103 */
104struct defer_queue {
105 unsigned long head; /* add element at head */
106 void *last_fct_in; /* last fct pointer encoded */
107 unsigned long tail; /* next element to remove at tail */
108 void *last_fct_out; /* last fct pointer encoded */
109 void **q;
110 /* registry information */
111 unsigned long last_head;
112 struct cds_list_head list; /* list of thread queues */
113};
114
786ee85b
MD
115/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
116#include "urcu-defer.h"
117
02be5561 118void __attribute__((destructor)) rcu_defer_exit(void);
786ee85b
MD
119
120extern void synchronize_rcu(void);
121
122/*
02be5561 123 * rcu_defer_mutex nests inside defer_thread_mutex.
786ee85b 124 */
02be5561 125static pthread_mutex_t rcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
786ee85b
MD
126static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
127
6d841bc2 128static int32_t defer_thread_futex;
d7ff6cee 129static int32_t defer_thread_stop;
4ce9e4f2 130
786ee85b
MD
131/*
132 * Written to only by each individual deferer. Read by both the deferer and
133 * the reclamation tread.
134 */
bd252a04 135static DEFINE_URCU_TLS(struct defer_queue, defer_queue);
0376e7b2 136static CDS_LIST_HEAD(registry_defer);
786ee85b 137static pthread_t tid_defer;
4ce9e4f2 138
0376e7b2 139static void mutex_lock_defer(pthread_mutex_t *mutex)
786ee85b
MD
140{
141 int ret;
142
143#ifndef DISTRUST_SIGNALS_EXTREME
144 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
145 if (ret)
146 urcu_die(ret);
786ee85b
MD
147#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
148 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
149 if (ret != EBUSY && ret != EINTR)
150 urcu_die(ret);
786ee85b
MD
151 poll(NULL,0,10);
152 }
153#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
154}
155
04eb9c4f
MD
156/*
157 * Wake-up any waiting defer thread. Called from many concurrent threads.
158 */
159static void wake_up_defer(void)
160{
a0b7f7ea 161 if (caa_unlikely(uatomic_read(&defer_thread_futex) == -1)) {
ec4e58a3 162 uatomic_set(&defer_thread_futex, 0);
0854ccff 163 futex_noasync(&defer_thread_futex, FUTEX_WAKE, 1,
04eb9c4f
MD
164 NULL, NULL, 0);
165 }
166}
167
168static unsigned long rcu_defer_num_callbacks(void)
169{
170 unsigned long num_items = 0, head;
dbc6128f 171 struct defer_queue *index;
04eb9c4f 172
0376e7b2
PM
173 mutex_lock_defer(&rcu_defer_mutex);
174 cds_list_for_each_entry(index, &registry_defer, list) {
6cf3827c 175 head = CMM_LOAD_SHARED(index->head);
dbc6128f 176 num_items += head - index->tail;
04eb9c4f 177 }
6abb4bd5 178 mutex_unlock(&rcu_defer_mutex);
04eb9c4f
MD
179 return num_items;
180}
181
182/*
183 * Defer thread waiting. Single thread.
184 */
185static void wait_defer(void)
186{
ec4e58a3 187 uatomic_dec(&defer_thread_futex);
d7ff6cee
MD
188 /* Write futex before read queue */
189 /* Write futex before read defer_thread_stop */
190 cmm_smp_mb();
191 if (_CMM_LOAD_SHARED(defer_thread_stop)) {
192 uatomic_set(&defer_thread_futex, 0);
193 pthread_exit(0);
194 }
04eb9c4f 195 if (rcu_defer_num_callbacks()) {
5481ddb3 196 cmm_smp_mb(); /* Read queue before write futex */
04eb9c4f 197 /* Callbacks are queued, don't wait. */
ec4e58a3 198 uatomic_set(&defer_thread_futex, 0);
04eb9c4f 199 } else {
5481ddb3 200 cmm_smp_rmb(); /* Read queue before read futex */
ec4e58a3 201 if (uatomic_read(&defer_thread_futex) == -1)
0854ccff 202 futex_noasync(&defer_thread_futex, FUTEX_WAIT, -1,
04eb9c4f
MD
203 NULL, NULL, 0);
204 }
205}
206
786ee85b
MD
207/*
208 * Must be called after Q.S. is reached.
209 */
210static void rcu_defer_barrier_queue(struct defer_queue *queue,
804b4375 211 unsigned long head)
786ee85b
MD
212{
213 unsigned long i;
804b4375
MD
214 void (*fct)(void *p);
215 void *p;
786ee85b
MD
216
217 /*
218 * Tail is only modified when lock is held.
219 * Head is only modified by owner thread.
220 */
221
804b4375 222 for (i = queue->tail; i != head;) {
5481ddb3 223 cmm_smp_rmb(); /* read head before q[]. */
6cf3827c 224 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
a0b7f7ea 225 if (caa_unlikely(DQ_IS_FCT_BIT(p))) {
804b4375
MD
226 DQ_CLEAR_FCT_BIT(p);
227 queue->last_fct_out = p;
6cf3827c 228 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
a0b7f7ea 229 } else if (caa_unlikely(p == DQ_FCT_MARK)) {
6cf3827c 230 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
804b4375 231 queue->last_fct_out = p;
6cf3827c 232 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
29cdb8d8 233 }
804b4375 234 fct = queue->last_fct_out;
804b4375 235 fct(p);
786ee85b 236 }
5481ddb3 237 cmm_smp_mb(); /* push tail after having used q[] */
6cf3827c 238 CMM_STORE_SHARED(queue->tail, i);
786ee85b
MD
239}
240
241static void _rcu_defer_barrier_thread(void)
242{
0d0e6c21 243 unsigned long head, num_items;
786ee85b 244
bd252a04
MD
245 head = URCU_TLS(defer_queue).head;
246 num_items = head - URCU_TLS(defer_queue).tail;
a0b7f7ea 247 if (caa_unlikely(!num_items))
0d0e6c21 248 return;
786ee85b 249 synchronize_rcu();
bd252a04 250 rcu_defer_barrier_queue(&URCU_TLS(defer_queue), head);
786ee85b
MD
251}
252
786ee85b
MD
253void rcu_defer_barrier_thread(void)
254{
0376e7b2 255 mutex_lock_defer(&rcu_defer_mutex);
786ee85b 256 _rcu_defer_barrier_thread();
6abb4bd5 257 mutex_unlock(&rcu_defer_mutex);
786ee85b
MD
258}
259
0d0e6c21
MD
260/*
261 * rcu_defer_barrier - Execute all queued rcu callbacks.
262 *
263 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
264 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
265 * are guaranteed to be executed.
266 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
267 * execution are not guaranteed to be executed in the current batch (could
268 * be left for the next batch). These callbacks queued by other threads are only
269 * guaranteed to be executed if there is explicit synchronization between
270 * the thread adding to the queue and the thread issuing the defer_barrier call.
271 */
272
786ee85b
MD
273void rcu_defer_barrier(void)
274{
dbc6128f 275 struct defer_queue *index;
0d0e6c21 276 unsigned long num_items = 0;
786ee85b 277
0376e7b2 278 if (cds_list_empty(&registry_defer))
786ee85b
MD
279 return;
280
0376e7b2
PM
281 mutex_lock_defer(&rcu_defer_mutex);
282 cds_list_for_each_entry(index, &registry_defer, list) {
6cf3827c 283 index->last_head = CMM_LOAD_SHARED(index->head);
dbc6128f 284 num_items += index->last_head - index->tail;
0d0e6c21 285 }
a0b7f7ea 286 if (caa_likely(!num_items)) {
0d0e6c21
MD
287 /*
288 * We skip the grace period because there are no queued
289 * callbacks to execute.
290 */
291 goto end;
292 }
786ee85b 293 synchronize_rcu();
0376e7b2 294 cds_list_for_each_entry(index, &registry_defer, list)
dbc6128f 295 rcu_defer_barrier_queue(index, index->last_head);
0d0e6c21 296end:
6abb4bd5 297 mutex_unlock(&rcu_defer_mutex);
786ee85b
MD
298}
299
2c22932b 300/*
b4f313b7 301 * _defer_rcu - Queue a RCU callback.
2c22932b 302 */
f50c11aa 303static void _defer_rcu(void (*fct)(void *p), void *p)
2c22932b
MD
304{
305 unsigned long head, tail;
306
307 /*
308 * Head is only modified by ourself. Tail can be modified by reclamation
309 * thread.
310 */
bd252a04
MD
311 head = URCU_TLS(defer_queue).head;
312 tail = CMM_LOAD_SHARED(URCU_TLS(defer_queue).tail);
2c22932b
MD
313
314 /*
ec8e44cf 315 * If queue is full, or reached threshold. Empty queue ourself.
2c22932b
MD
316 * Worse-case: must allow 2 supplementary entries for fct pointer.
317 */
a0b7f7ea 318 if (caa_unlikely(head - tail >= DEFER_QUEUE_SIZE - 2)) {
2c22932b
MD
319 assert(head - tail <= DEFER_QUEUE_SIZE);
320 rcu_defer_barrier_thread();
bd252a04 321 assert(head - CMM_LOAD_SHARED(URCU_TLS(defer_queue).tail) == 0);
2c22932b
MD
322 }
323
2ef1bfb2
LJ
324 /*
325 * Encode:
326 * if the function is not changed and the data is aligned and it is
327 * not the marker:
328 * store the data
329 * otherwise if the function is aligned and its not the marker:
330 * store the function with DQ_FCT_BIT
331 * store the data
332 * otherwise:
333 * store the marker (DQ_FCT_MARK)
334 * store the function
335 * store the data
336 *
337 * Decode: see the comments before 'struct defer_queue'
338 * or the code in rcu_defer_barrier_queue().
339 */
bd252a04 340 if (caa_unlikely(URCU_TLS(defer_queue).last_fct_in != fct
2ef1bfb2
LJ
341 || DQ_IS_FCT_BIT(p)
342 || p == DQ_FCT_MARK)) {
bd252a04 343 URCU_TLS(defer_queue).last_fct_in = fct;
a0b7f7ea 344 if (caa_unlikely(DQ_IS_FCT_BIT(fct) || fct == DQ_FCT_MARK)) {
bd252a04 345 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
2c22932b 346 DQ_FCT_MARK);
bd252a04 347 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
2c22932b
MD
348 fct);
349 } else {
350 DQ_SET_FCT_BIT(fct);
bd252a04 351 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
2c22932b
MD
352 fct);
353 }
2c22932b 354 }
bd252a04 355 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK], p);
5481ddb3 356 cmm_smp_wmb(); /* Publish new pointer before head */
2c22932b 357 /* Write q[] before head. */
bd252a04 358 CMM_STORE_SHARED(URCU_TLS(defer_queue).head, head);
5481ddb3 359 cmm_smp_mb(); /* Write queue head before read futex */
2c22932b
MD
360 /*
361 * Wake-up any waiting defer thread.
362 */
363 wake_up_defer();
364}
365
c961f4f3 366static void *thr_defer(void *args)
786ee85b
MD
367{
368 for (;;) {
4ce9e4f2
MD
369 /*
370 * "Be green". Don't wake up the CPU if there is no RCU work
371 * to perform whatsoever. Aims at saving laptop battery life by
372 * leaving the processor in sleep state when idle.
373 */
4ce9e4f2 374 wait_defer();
4ce9e4f2 375 /* Sleeping after wait_defer to let many callbacks enqueue */
71df5ef4 376 poll(NULL,0,100); /* wait for 100ms */
786ee85b
MD
377 rcu_defer_barrier();
378 }
379
380 return NULL;
381}
382
383/*
384 * library wrappers to be used by non-LGPL compatible source code.
385 */
386
3614f13c 387void defer_rcu(void (*fct)(void *p), void *p)
786ee85b 388{
3614f13c 389 _defer_rcu(fct, p);
786ee85b
MD
390}
391
786ee85b
MD
392static void start_defer_thread(void)
393{
394 int ret;
395
dbc6128f 396 ret = pthread_create(&tid_defer, NULL, thr_defer, NULL);
786ee85b
MD
397 assert(!ret);
398}
399
400static void stop_defer_thread(void)
401{
402 int ret;
403 void *tret;
404
d7ff6cee
MD
405 _CMM_STORE_SHARED(defer_thread_stop, 1);
406 /* Store defer_thread_stop before testing futex */
407 cmm_smp_mb();
4ce9e4f2 408 wake_up_defer();
d7ff6cee 409
786ee85b
MD
410 ret = pthread_join(tid_defer, &tret);
411 assert(!ret);
d7ff6cee
MD
412
413 CMM_STORE_SHARED(defer_thread_stop, 0);
414 /* defer thread should always exit when futex value is 0 */
415 assert(uatomic_read(&defer_thread_futex) == 0);
786ee85b
MD
416}
417
7fdbbd61 418int rcu_defer_register_thread(void)
786ee85b 419{
dbc6128f
MD
420 int was_empty;
421
bd252a04
MD
422 assert(URCU_TLS(defer_queue).last_head == 0);
423 assert(URCU_TLS(defer_queue).q == NULL);
424 URCU_TLS(defer_queue).q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE);
425 if (!URCU_TLS(defer_queue).q)
7fdbbd61 426 return -ENOMEM;
786ee85b 427
0376e7b2
PM
428 mutex_lock_defer(&defer_thread_mutex);
429 mutex_lock_defer(&rcu_defer_mutex);
430 was_empty = cds_list_empty(&registry_defer);
bd252a04 431 cds_list_add(&URCU_TLS(defer_queue).list, &registry_defer);
6abb4bd5 432 mutex_unlock(&rcu_defer_mutex);
786ee85b 433
dbc6128f 434 if (was_empty)
786ee85b 435 start_defer_thread();
6abb4bd5 436 mutex_unlock(&defer_thread_mutex);
7fdbbd61 437 return 0;
786ee85b
MD
438}
439
440void rcu_defer_unregister_thread(void)
441{
dbc6128f 442 int is_empty;
786ee85b 443
0376e7b2
PM
444 mutex_lock_defer(&defer_thread_mutex);
445 mutex_lock_defer(&rcu_defer_mutex);
bd252a04 446 cds_list_del(&URCU_TLS(defer_queue).list);
786ee85b 447 _rcu_defer_barrier_thread();
bd252a04
MD
448 free(URCU_TLS(defer_queue).q);
449 URCU_TLS(defer_queue).q = NULL;
0376e7b2 450 is_empty = cds_list_empty(&registry_defer);
6abb4bd5 451 mutex_unlock(&rcu_defer_mutex);
786ee85b 452
dbc6128f 453 if (is_empty)
786ee85b 454 stop_defer_thread();
6abb4bd5 455 mutex_unlock(&defer_thread_mutex);
786ee85b
MD
456}
457
02be5561 458void rcu_defer_exit(void)
786ee85b 459{
0376e7b2 460 assert(cds_list_empty(&registry_defer));
786ee85b 461}
0376e7b2
PM
462
463#endif /* _URCU_DEFER_IMPL_H */
This page took 0.051246 seconds and 4 git commands to generate.