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