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