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