Use urcu/tls-compat.h
[urcu.git] / urcu.c
CommitLineData
b257a10b
MD
1/*
2 * urcu.c
3 *
4 * Userspace RCU library
5 *
6982d6d7 6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
af02d47e 7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
b257a10b 8 *
af02d47e
MD
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
54843abc
PM
22 *
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
b257a10b
MD
24 */
25
fdf01eed 26#define _BSD_SOURCE
c1d2c60b 27#define _GNU_SOURCE
71c811bf 28#define _LGPL_SOURCE
27b012e2
MD
29#include <stdio.h>
30#include <pthread.h>
31#include <signal.h>
32#include <assert.h>
f69f195a 33#include <stdlib.h>
6d841bc2 34#include <stdint.h>
f69f195a 35#include <string.h>
09a9f986 36#include <errno.h>
e8043c1b 37#include <poll.h>
27b012e2 38
71c811bf 39#include "urcu/wfqueue.h"
57760d44 40#include "urcu/map/urcu.h"
af7c2dbe 41#include "urcu/static/urcu.h"
618b2595 42#include "urcu-pointer.h"
bd252a04 43#include "urcu/tls-compat.h"
71c811bf 44
121a5d44 45/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 46#undef _LGPL_SOURCE
27b012e2 47#include "urcu.h"
71c811bf 48#define _LGPL_SOURCE
27b012e2 49
3a71751e
PB
50/*
51 * If a reader is really non-cooperative and refuses to commit its
52 * rcu_active_readers count to memory (there is no barrier in the reader
53 * per-se), kick it after a few loops waiting for it.
54 */
55#define KICK_READER_LOOPS 10000
56
57/*
58 * Active attempts to check for reader Q.S. before calling futex().
59 */
60#define RCU_QS_ACTIVE_ATTEMPTS 100
61
fdf01eed 62#ifdef RCU_MEMBARRIER
834a45ba 63static int init_done;
fdf01eed 64int has_sys_membarrier;
834a45ba 65
02be5561 66void __attribute__((constructor)) rcu_init(void);
fdf01eed
MD
67#endif
68
69#ifdef RCU_MB
02be5561 70void rcu_init(void)
e90a6e9c
MD
71{
72}
73#endif
8a5fb4c9 74
fdf01eed
MD
75#ifdef RCU_SIGNAL
76static int init_done;
77
78void __attribute__((constructor)) rcu_init(void);
79void __attribute__((destructor)) rcu_exit(void);
80#endif
81
6abb4bd5 82static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
27b012e2 83
6d841bc2 84int32_t gp_futex;
bc6c15bb 85
128166c9
MD
86/*
87 * Global grace period counter.
02be5561 88 * Contains the current RCU_GP_CTR_PHASE.
afb8f2c9 89 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
b0d5e790 90 * Written to only by writer with mutex taken. Read by both writer and readers.
128166c9 91 */
27d65bc5 92unsigned long rcu_gp_ctr = RCU_GP_COUNT;
27b012e2 93
b0d5e790
MD
94/*
95 * Written to only by each individual reader. Read by both the reader and the
96 * writers.
97 */
bd252a04 98DEFINE_URCU_TLS(struct rcu_reader, rcu_reader);
27b012e2 99
cf380c2f 100#ifdef DEBUG_YIELD
9d335088 101unsigned int yield_active;
bd252a04 102DEFINE_URCU_TLS(unsigned int, rand_yield);
cf380c2f
MD
103#endif
104
16aa9ee8 105static CDS_LIST_HEAD(registry);
27b012e2 106
6abb4bd5 107static void mutex_lock(pthread_mutex_t *mutex)
41718ff9
MD
108{
109 int ret;
09a9f986
PM
110
111#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 112 ret = pthread_mutex_lock(mutex);
41718ff9
MD
113 if (ret) {
114 perror("Error in pthread mutex lock");
115 exit(-1);
116 }
09a9f986 117#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 118 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
09a9f986
PM
119 if (ret != EBUSY && ret != EINTR) {
120 printf("ret = %d, errno = %d\n", ret, errno);
121 perror("Error in pthread mutex lock");
122 exit(-1);
123 }
bd252a04 124 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) {
5481ddb3 125 cmm_smp_mb();
bd252a04 126 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
5481ddb3 127 cmm_smp_mb();
09a9f986
PM
128 }
129 poll(NULL,0,10);
130 }
131#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
41718ff9
MD
132}
133
6abb4bd5 134static void mutex_unlock(pthread_mutex_t *mutex)
41718ff9
MD
135{
136 int ret;
137
6abb4bd5 138 ret = pthread_mutex_unlock(mutex);
41718ff9
MD
139 if (ret) {
140 perror("Error in pthread mutex unlock");
141 exit(-1);
142 }
143}
144
fdf01eed 145#ifdef RCU_MEMBARRIER
25cc6d18 146static void smp_mb_master(int group)
fdf01eed 147{
a0b7f7ea 148 if (caa_likely(has_sys_membarrier))
f0708810 149 membarrier(MEMBARRIER_EXPEDITED);
fdf01eed 150 else
5481ddb3 151 cmm_smp_mb();
fdf01eed
MD
152}
153#endif
154
02be5561 155#ifdef RCU_MB
25cc6d18 156static void smp_mb_master(int group)
40e140c9 157{
5481ddb3 158 cmm_smp_mb();
40e140c9 159}
fdf01eed
MD
160#endif
161
162#ifdef RCU_SIGNAL
78ff9419 163static void force_mb_all_readers(void)
27b012e2 164{
02be5561 165 struct rcu_reader *index;
e3b0cef0 166
27b012e2 167 /*
5481ddb3 168 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
27b012e2
MD
169 * compiler barriers around rcu read lock as real memory barriers.
170 */
16aa9ee8 171 if (cds_list_empty(&registry))
27b012e2 172 return;
3a86deba 173 /*
5481ddb3 174 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
157dca95 175 * a cache flush on architectures with non-coherent cache. Let's play
5481ddb3 176 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
157dca95 177 * cache flush is enforced.
3a86deba 178 */
16aa9ee8 179 cds_list_for_each_entry(index, &registry, node) {
6cf3827c 180 CMM_STORE_SHARED(index->need_mb, 1);
02be5561 181 pthread_kill(index->tid, SIGRCU);
09a9f986 182 }
27b012e2
MD
183 /*
184 * Wait for sighandler (and thus mb()) to execute on every thread.
09a9f986
PM
185 *
186 * Note that the pthread_kill() will never be executed on systems
187 * that correctly deliver signals in a timely manner. However, it
188 * is not uncommon for kernels to have bugs that can result in
189 * lost or unduly delayed signals.
190 *
191 * If you are seeing the below pthread_kill() executing much at
192 * all, we suggest testing the underlying kernel and filing the
193 * relevant bug report. For Linux kernels, we recommend getting
194 * the Linux Test Project (LTP).
27b012e2 195 */
16aa9ee8 196 cds_list_for_each_entry(index, &registry, node) {
6cf3827c 197 while (CMM_LOAD_SHARED(index->need_mb)) {
02be5561 198 pthread_kill(index->tid, SIGRCU);
09a9f986
PM
199 poll(NULL, 0, 1);
200 }
201 }
5481ddb3 202 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
27b012e2 203}
9d7e3f89 204
25cc6d18 205static void smp_mb_master(int group)
9d7e3f89
MD
206{
207 force_mb_all_readers();
208}
fdf01eed 209#endif /* #ifdef RCU_SIGNAL */
27b012e2 210
bc6c15bb
MD
211/*
212 * synchronize_rcu() waiting. Single thread.
213 */
cfe78e25 214static void wait_gp(void)
bc6c15bb 215{
cfe78e25 216 /* Read reader_gp before read futex */
25cc6d18 217 smp_mb_master(RCU_MB_GROUP);
cfe78e25 218 if (uatomic_read(&gp_futex) == -1)
0854ccff 219 futex_async(&gp_futex, FUTEX_WAIT, -1,
cfe78e25 220 NULL, NULL, 0);
bc6c15bb
MD
221}
222
2dfb8b5e 223void update_counter_and_wait(void)
27b012e2 224{
16aa9ee8 225 CDS_LIST_HEAD(qsreaders);
cfe78e25 226 int wait_loops = 0;
02be5561 227 struct rcu_reader *index, *tmp;
27b012e2 228
32c15e4e 229 /* Switch parity: 0 -> 1, 1 -> 0 */
6cf3827c 230 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
2dfb8b5e
MD
231
232 /*
d40fde2c
MD
233 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
234 * state. Failure to do so could result in the writer waiting forever
235 * while new readers are always accessing data (no progress). Enforce
236 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
2dfb8b5e 237 */
5481ddb3 238 cmm_barrier();
2dfb8b5e
MD
239
240 /*
935b11ff 241 *
5481ddb3 242 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
243 * model easier to understand. It does not have a big performance impact
244 * anyway, given this is the write-side.
245 */
5481ddb3 246 cmm_smp_mb();
2dfb8b5e 247
40e140c9 248 /*
bd252a04 249 * Wait for each thread URCU_TLS(rcu_reader).ctr count to become 0.
27b012e2 250 */
cfe78e25
MD
251 for (;;) {
252 wait_loops++;
253 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
254 uatomic_dec(&gp_futex);
255 /* Write futex before read reader_gp */
25cc6d18 256 smp_mb_master(RCU_MB_GROUP);
cfe78e25
MD
257 }
258
16aa9ee8 259 cds_list_for_each_entry_safe(index, tmp, &registry, node) {
b95a001f 260 if (!rcu_gp_ongoing(&index->ctr))
16aa9ee8 261 cds_list_move(&index->node, &qsreaders);
cfe78e25
MD
262 }
263
e8043c1b 264#ifndef HAS_INCOHERENT_CACHES
16aa9ee8 265 if (cds_list_empty(&registry)) {
cfe78e25
MD
266 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
267 /* Read reader_gp before write futex */
25cc6d18 268 smp_mb_master(RCU_MB_GROUP);
cfe78e25 269 uatomic_set(&gp_futex, 0);
bc6c15bb 270 }
cfe78e25
MD
271 break;
272 } else {
273 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
274 wait_gp();
275 else
06f22bdb 276 caa_cpu_relax();
bc6c15bb 277 }
e8043c1b 278#else /* #ifndef HAS_INCOHERENT_CACHES */
27b012e2 279 /*
40e140c9 280 * BUSY-LOOP. Force the reader thread to commit its
bd252a04
MD
281 * URCU_TLS(rcu_reader).ctr update to memory if we wait
282 * for too long.
27b012e2 283 */
16aa9ee8 284 if (cds_list_empty(&registry)) {
cfe78e25
MD
285 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
286 /* Read reader_gp before write futex */
25cc6d18 287 smp_mb_master(RCU_MB_GROUP);
cfe78e25
MD
288 uatomic_set(&gp_futex, 0);
289 }
290 break;
291 } else {
292 switch (wait_loops) {
bc6c15bb 293 case RCU_QS_ACTIVE_ATTEMPTS:
cfe78e25
MD
294 wait_gp();
295 break; /* only escape switch */
bc6c15bb 296 case KICK_READER_LOOPS:
25cc6d18 297 smp_mb_master(RCU_MB_GROUP);
40e140c9 298 wait_loops = 0;
cfe78e25 299 break; /* only escape switch */
bc6c15bb 300 default:
06f22bdb 301 caa_cpu_relax();
40e140c9
MD
302 }
303 }
e8043c1b 304#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
27b012e2 305 }
cfe78e25 306 /* put back the reader list in the registry */
16aa9ee8 307 cds_list_splice(&qsreaders, &registry);
27b012e2
MD
308}
309
9598a481 310void synchronize_rcu(void)
2bc59bd7 311{
6abb4bd5 312 mutex_lock(&rcu_gp_lock);
135530fd 313
16aa9ee8 314 if (cds_list_empty(&registry))
2dfb8b5e
MD
315 goto out;
316
9598a481 317 /* All threads should read qparity before accessing data structure
6abb4bd5
MD
318 * where new ptr points to. Must be done within rcu_gp_lock because it
319 * iterates on reader threads.*/
9598a481 320 /* Write new ptr before changing the qparity */
25cc6d18 321 smp_mb_master(RCU_MB_GROUP);
9598a481 322
9598a481
MD
323 /*
324 * Wait for previous parity to be empty of readers.
325 */
2dfb8b5e 326 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
9598a481
MD
327
328 /*
329 * Must finish waiting for quiescent state for parity 0 before
d40fde2c
MD
330 * committing next rcu_gp_ctr update to memory. Failure to do so could
331 * result in the writer waiting forever while new readers are always
332 * accessing data (no progress). Enforce compiler-order of load
bd252a04 333 * URCU_TLS(rcu_reader).ctr before store to rcu_gp_ctr.
9598a481 334 */
5481ddb3 335 cmm_barrier();
9598a481 336
5dba80f9 337 /*
5481ddb3 338 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
5dba80f9
MD
339 * model easier to understand. It does not have a big performance impact
340 * anyway, given this is the write-side.
341 */
5481ddb3 342 cmm_smp_mb();
67c2d80b 343
9598a481
MD
344 /*
345 * Wait for previous parity to be empty of readers.
346 */
2dfb8b5e 347 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
9598a481 348
9598a481 349 /* Finish waiting for reader threads before letting the old ptr being
6abb4bd5
MD
350 * freed. Must be done within rcu_gp_lock because it iterates on reader
351 * threads. */
25cc6d18 352 smp_mb_master(RCU_MB_GROUP);
2dfb8b5e 353out:
6abb4bd5 354 mutex_unlock(&rcu_gp_lock);
2bc59bd7
PM
355}
356
121a5d44
MD
357/*
358 * library wrappers to be used by non-LGPL compatible source code.
359 */
360
361void rcu_read_lock(void)
362{
363 _rcu_read_lock();
364}
365
366void rcu_read_unlock(void)
367{
368 _rcu_read_unlock();
369}
370
121a5d44 371void rcu_register_thread(void)
27b012e2 372{
bd252a04
MD
373 URCU_TLS(rcu_reader).tid = pthread_self();
374 assert(URCU_TLS(rcu_reader).need_mb == 0);
375 assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK));
02be5561 376
6abb4bd5 377 mutex_lock(&rcu_gp_lock);
02be5561 378 rcu_init(); /* In case gcc does not support constructor attribute */
bd252a04 379 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
6abb4bd5 380 mutex_unlock(&rcu_gp_lock);
27b012e2
MD
381}
382
121a5d44 383void rcu_unregister_thread(void)
27b012e2 384{
6abb4bd5 385 mutex_lock(&rcu_gp_lock);
bd252a04 386 cds_list_del(&URCU_TLS(rcu_reader).node);
6abb4bd5 387 mutex_unlock(&rcu_gp_lock);
27b012e2
MD
388}
389
fdf01eed
MD
390#ifdef RCU_MEMBARRIER
391void rcu_init(void)
392{
393 if (init_done)
394 return;
395 init_done = 1;
cf5271ee 396 if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY))
fdf01eed
MD
397 has_sys_membarrier = 1;
398}
399#endif
400
401#ifdef RCU_SIGNAL
02be5561 402static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context)
27b012e2 403{
40e140c9 404 /*
5481ddb3
DG
405 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
406 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
40e140c9
MD
407 * executed on.
408 */
5481ddb3 409 cmm_smp_mb();
bd252a04 410 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
5481ddb3 411 cmm_smp_mb();
27b012e2
MD
412}
413
8a5fb4c9 414/*
02be5561 415 * rcu_init constructor. Called when the library is linked, but also when
8a5fb4c9
MD
416 * reader threads are calling rcu_register_thread().
417 * Should only be called by a single thread at a given time. This is ensured by
6abb4bd5
MD
418 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
419 * load time, which should not be executed by multiple threads nor concurrently
420 * with rcu_register_thread() anyway.
8a5fb4c9 421 */
02be5561 422void rcu_init(void)
27b012e2
MD
423{
424 struct sigaction act;
425 int ret;
426
8a5fb4c9
MD
427 if (init_done)
428 return;
429 init_done = 1;
430
02be5561 431 act.sa_sigaction = sigrcu_handler;
dd052bd3 432 act.sa_flags = SA_SIGINFO | SA_RESTART;
c297c21c 433 sigemptyset(&act.sa_mask);
02be5561 434 ret = sigaction(SIGRCU, &act, NULL);
f69f195a
MD
435 if (ret) {
436 perror("Error in sigaction");
27b012e2
MD
437 exit(-1);
438 }
439}
440
02be5561 441void rcu_exit(void)
27b012e2
MD
442{
443 struct sigaction act;
444 int ret;
445
02be5561 446 ret = sigaction(SIGRCU, NULL, &act);
f69f195a
MD
447 if (ret) {
448 perror("Error in sigaction");
27b012e2
MD
449 exit(-1);
450 }
02be5561 451 assert(act.sa_sigaction == sigrcu_handler);
16aa9ee8 452 assert(cds_list_empty(&registry));
27b012e2 453}
5e77fc1f 454
fdf01eed 455#endif /* #ifdef RCU_SIGNAL */
5e77fc1f 456
5e6b23a6 457DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 458
5e77fc1f 459#include "urcu-call-rcu-impl.h"
0376e7b2 460#include "urcu-defer-impl.h"
This page took 0.053957 seconds and 4 git commands to generate.