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