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