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