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