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