Add SA_RESTART flag to signal handler
[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
e90a6e9c 39#ifndef DEBUG_FULL_MB
8a5fb4c9
MD
40void __attribute__((constructor)) urcu_init(void);
41void __attribute__((destructor)) urcu_exit(void);
e90a6e9c 42#else
ebc107f8 43static inline void urcu_init(void)
e90a6e9c
MD
44{
45}
46#endif
8a5fb4c9
MD
47
48int init_done;
49
27b012e2
MD
50pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
51
128166c9
MD
52/*
53 * Global grace period counter.
54 * Contains the current RCU_GP_CTR_BIT.
55 * Also has a RCU_GP_CTR_BIT 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
c265818b 84void 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
c265818b 111void 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
bb488185 130#ifdef DEBUG_FULL_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}
e8043c1b
MD
142#else /* #ifdef DEBUG_FULL_MB */
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}
e8043c1b 209#endif /* #else #ifdef DEBUG_FULL_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
350void *rcu_publish_content_sym(void **p, void *v)
351{
352 void *oldptr;
353
354 oldptr = _rcu_xchg_pointer(p, v);
355 synchronize_rcu();
356 return oldptr;
357}
358
359static void rcu_add_reader(pthread_t id)
27b012e2 360{
0a52082b 361 struct reader_registry *oldarray;
f69f195a 362
0a52082b 363 if (!registry) {
27b012e2 364 alloc_readers = INIT_NUM_THREADS;
f69f195a 365 num_readers = 0;
0a52082b
MD
366 registry =
367 malloc(sizeof(struct reader_registry) * alloc_readers);
27b012e2
MD
368 }
369 if (alloc_readers < num_readers + 1) {
0a52082b
MD
370 oldarray = registry;
371 registry = malloc(sizeof(struct reader_registry)
27b012e2 372 * (alloc_readers << 1));
0a52082b
MD
373 memcpy(registry, oldarray,
374 sizeof(struct reader_registry) * alloc_readers);
27b012e2
MD
375 alloc_readers <<= 1;
376 free(oldarray);
377 }
0a52082b 378 registry[num_readers].tid = id;
27b012e2 379 /* reference to the TLS of _this_ reader thread. */
0a52082b 380 registry[num_readers].urcu_active_readers = &urcu_active_readers;
09a9f986 381 registry[num_readers].need_mb = &need_mb;
27b012e2
MD
382 num_readers++;
383}
384
385/*
386 * Never shrink (implementation limitation).
387 * This is O(nb threads). Eventually use a hash table.
388 */
121a5d44 389static void rcu_remove_reader(pthread_t id)
27b012e2 390{
0a52082b 391 struct reader_registry *index;
27b012e2 392
0a52082b
MD
393 assert(registry != NULL);
394 for (index = registry; index < registry + num_readers; index++) {
e6d6e2dc 395 if (pthread_equal(index->tid, id)) {
0a52082b
MD
396 memcpy(index, &registry[num_readers - 1],
397 sizeof(struct reader_registry));
398 registry[num_readers - 1].tid = 0;
399 registry[num_readers - 1].urcu_active_readers = NULL;
27b012e2
MD
400 num_readers--;
401 return;
402 }
403 }
404 /* Hrm not found, forgot to register ? */
405 assert(0);
406}
407
121a5d44 408void rcu_register_thread(void)
27b012e2 409{
c265818b 410 internal_urcu_lock();
8a5fb4c9 411 urcu_init(); /* In case gcc does not support constructor attribute */
121a5d44 412 rcu_add_reader(pthread_self());
c265818b 413 internal_urcu_unlock();
27b012e2
MD
414}
415
121a5d44 416void rcu_unregister_thread(void)
27b012e2 417{
c265818b 418 internal_urcu_lock();
121a5d44 419 rcu_remove_reader(pthread_self());
c265818b 420 internal_urcu_unlock();
27b012e2
MD
421}
422
bb488185 423#ifndef DEBUG_FULL_MB
121a5d44 424static void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
27b012e2 425{
40e140c9
MD
426 /*
427 * Executing this smp_mb() is the only purpose of this signal handler.
428 * It punctually promotes barrier() into smp_mb() on every thread it is
429 * executed on.
430 */
b715b99e 431 smp_mb();
09a9f986
PM
432 need_mb = 0;
433 smp_mb();
27b012e2
MD
434}
435
8a5fb4c9
MD
436/*
437 * urcu_init constructor. Called when the library is linked, but also when
438 * reader threads are calling rcu_register_thread().
439 * Should only be called by a single thread at a given time. This is ensured by
440 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
441 * library load time, which should not be executed by multiple threads nor
442 * concurrently with rcu_register_thread() anyway.
443 */
444void urcu_init(void)
27b012e2
MD
445{
446 struct sigaction act;
447 int ret;
448
8a5fb4c9
MD
449 if (init_done)
450 return;
451 init_done = 1;
452
27b012e2 453 act.sa_sigaction = sigurcu_handler;
dd052bd3 454 act.sa_flags = SA_SIGINFO | SA_RESTART;
c297c21c 455 sigemptyset(&act.sa_mask);
27b012e2 456 ret = sigaction(SIGURCU, &act, NULL);
f69f195a
MD
457 if (ret) {
458 perror("Error in sigaction");
27b012e2
MD
459 exit(-1);
460 }
461}
462
8a5fb4c9 463void urcu_exit(void)
27b012e2
MD
464{
465 struct sigaction act;
466 int ret;
467
468 ret = sigaction(SIGURCU, NULL, &act);
f69f195a
MD
469 if (ret) {
470 perror("Error in sigaction");
27b012e2
MD
471 exit(-1);
472 }
473 assert(act.sa_sigaction == sigurcu_handler);
0a52082b 474 free(registry);
27b012e2 475}
e8043c1b 476#endif /* #ifndef DEBUG_FULL_MB */
This page took 0.046004 seconds and 4 git commands to generate.