Add missing urcu-qsbr
[urcu.git] / urcu-qsbr.c
... / ...
CommitLineData
1/*
2 * urcu.c
3 *
4 * Userspace RCU library
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
8 *
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
22 *
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
24 */
25
26#include <stdio.h>
27#include <pthread.h>
28#include <signal.h>
29#include <assert.h>
30#include <stdlib.h>
31#include <string.h>
32#include <errno.h>
33#include <poll.h>
34
35#include "urcu-static.h"
36/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
37#include "urcu.h"
38
39void __attribute__((constructor)) urcu_init(void);
40void __attribute__((destructor)) urcu_exit(void);
41
42int init_done;
43
44pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
45
46/*
47 * Global grace period counter.
48 */
49long urcu_gp_ctr = 0;
50
51/*
52 * Written to only by each individual reader. Read by both the reader and the
53 * writers.
54 */
55long __thread urcu_active_readers;
56
57/* Thread IDs of registered readers */
58#define INIT_NUM_THREADS 4
59
60struct reader_registry {
61 pthread_t tid;
62 long *urcu_active_readers;
63 char *need_mb;
64};
65
66#ifdef DEBUG_YIELD
67unsigned int yield_active;
68unsigned int __thread rand_yield;
69#endif
70
71static struct reader_registry *registry;
72static char __thread need_mb;
73static int num_readers, alloc_readers;
74
75void internal_urcu_lock(void)
76{
77 int ret;
78
79#ifndef DISTRUST_SIGNALS_EXTREME
80 ret = pthread_mutex_lock(&urcu_mutex);
81 if (ret) {
82 perror("Error in pthread mutex lock");
83 exit(-1);
84 }
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 }
92 if (need_mb) {
93 smp_mb();
94 need_mb = 0;
95 smp_mb();
96 }
97 poll(NULL,0,10);
98 }
99#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
100}
101
102void internal_urcu_unlock(void)
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
113#ifdef DEBUG_FULL_MB
114#ifdef HAS_INCOHERENT_CACHES
115static void force_mb_single_thread(struct reader_registry *index)
116{
117 smp_mb();
118}
119#endif /* #ifdef HAS_INCOHERENT_CACHES */
120
121static void force_mb_all_threads(void)
122{
123 smp_mb();
124}
125#else /* #ifdef DEBUG_FULL_MB */
126#ifdef HAS_INCOHERENT_CACHES
127static void force_mb_single_thread(struct reader_registry *index)
128{
129 assert(registry);
130 /*
131 * pthread_kill has a smp_mb(). But beware, we assume it performs
132 * a cache flush on architectures with non-coherent cache. Let's play
133 * safe and don't assume anything : we use smp_mc() to make sure the
134 * cache flush is enforced.
135 */
136 *index->need_mb = 1;
137 smp_mc(); /* write ->need_mb before sending the signals */
138 pthread_kill(index->tid, SIGURCU);
139 smp_mb();
140 /*
141 * Wait for sighandler (and thus mb()) to execute on every thread.
142 * BUSY-LOOP.
143 */
144 while (*index->need_mb) {
145 poll(NULL, 0, 1);
146 }
147 smp_mb(); /* read ->need_mb before ending the barrier */
148}
149#endif /* #ifdef HAS_INCOHERENT_CACHES */
150
151static void force_mb_all_threads(void)
152{
153 struct reader_registry *index;
154 /*
155 * Ask for each threads to execute a smp_mb() so we can consider the
156 * compiler barriers around rcu read lock as real memory barriers.
157 */
158 if (!registry)
159 return;
160 /*
161 * pthread_kill has a smp_mb(). But beware, we assume it performs
162 * a cache flush on architectures with non-coherent cache. Let's play
163 * safe and don't assume anything : we use smp_mc() to make sure the
164 * cache flush is enforced.
165 */
166 for (index = registry; index < registry + num_readers; index++) {
167 *index->need_mb = 1;
168 smp_mc(); /* write need_mb before sending the signal */
169 pthread_kill(index->tid, SIGURCU);
170 }
171 /*
172 * Wait for sighandler (and thus mb()) to execute on every thread.
173 *
174 * Note that the pthread_kill() will never be executed on systems
175 * that correctly deliver signals in a timely manner. However, it
176 * is not uncommon for kernels to have bugs that can result in
177 * lost or unduly delayed signals.
178 *
179 * If you are seeing the below pthread_kill() executing much at
180 * all, we suggest testing the underlying kernel and filing the
181 * relevant bug report. For Linux kernels, we recommend getting
182 * the Linux Test Project (LTP).
183 */
184 for (index = registry; index < registry + num_readers; index++) {
185 while (*index->need_mb) {
186 pthread_kill(index->tid, SIGURCU);
187 poll(NULL, 0, 1);
188 }
189 }
190 smp_mb(); /* read ->need_mb before ending the barrier */
191}
192#endif /* #else #ifdef DEBUG_FULL_MB */
193
194void wait_for_quiescent_state(void)
195{
196 struct reader_registry *index;
197
198 if (!registry)
199 return;
200 /*
201 * Wait for each thread urcu_active_readers count to become 0.
202 */
203 for (index = registry; index < registry + num_readers; index++) {
204#ifndef HAS_INCOHERENT_CACHES
205 while (rcu_old_gp_ongoing(index->urcu_active_readers))
206 cpu_relax();
207#else /* #ifndef HAS_INCOHERENT_CACHES */
208 int wait_loops = 0;
209 /*
210 * BUSY-LOOP. Force the reader thread to commit its
211 * urcu_active_readers update to memory if we wait for too long.
212 */
213 while (rcu_old_gp_ongoing(index->urcu_active_readers)) {
214 if (wait_loops++ == KICK_READER_LOOPS) {
215 force_mb_single_thread(index);
216 wait_loops = 0;
217 } else {
218 cpu_relax();
219 }
220 }
221#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
222 }
223}
224
225void synchronize_rcu(void)
226{
227 internal_urcu_lock();
228 force_mb_all_threads();
229 urcu_gp_ctr += 2;
230 wait_for_quiescent_state();
231 force_mb_all_threads();
232 internal_urcu_unlock();
233}
234
235/*
236 * library wrappers to be used by non-LGPL compatible source code.
237 */
238
239void rcu_read_lock(void)
240{
241 _rcu_read_lock();
242}
243
244void rcu_read_unlock(void)
245{
246 _rcu_read_unlock();
247}
248
249void *rcu_dereference(void *p)
250{
251 return _rcu_dereference(p);
252}
253
254void *rcu_assign_pointer_sym(void **p, void *v)
255{
256 wmb();
257 return STORE_SHARED(p, v);
258}
259
260void *rcu_xchg_pointer_sym(void **p, void *v)
261{
262 wmb();
263 return xchg(p, v);
264}
265
266void *rcu_publish_content_sym(void **p, void *v)
267{
268 void *oldptr;
269
270 oldptr = _rcu_xchg_pointer(p, v);
271 synchronize_rcu();
272 return oldptr;
273}
274
275static void rcu_add_reader(pthread_t id)
276{
277 struct reader_registry *oldarray;
278
279 if (!registry) {
280 alloc_readers = INIT_NUM_THREADS;
281 num_readers = 0;
282 registry =
283 malloc(sizeof(struct reader_registry) * alloc_readers);
284 }
285 if (alloc_readers < num_readers + 1) {
286 oldarray = registry;
287 registry = malloc(sizeof(struct reader_registry)
288 * (alloc_readers << 1));
289 memcpy(registry, oldarray,
290 sizeof(struct reader_registry) * alloc_readers);
291 alloc_readers <<= 1;
292 free(oldarray);
293 }
294 registry[num_readers].tid = id;
295 /* reference to the TLS of _this_ reader thread. */
296 registry[num_readers].urcu_active_readers = &urcu_active_readers;
297 registry[num_readers].need_mb = &need_mb;
298 num_readers++;
299}
300
301/*
302 * Never shrink (implementation limitation).
303 * This is O(nb threads). Eventually use a hash table.
304 */
305static void rcu_remove_reader(pthread_t id)
306{
307 struct reader_registry *index;
308
309 assert(registry != NULL);
310 for (index = registry; index < registry + num_readers; index++) {
311 if (pthread_equal(index->tid, id)) {
312 memcpy(index, &registry[num_readers - 1],
313 sizeof(struct reader_registry));
314 registry[num_readers - 1].tid = 0;
315 registry[num_readers - 1].urcu_active_readers = NULL;
316 num_readers--;
317 return;
318 }
319 }
320 /* Hrm not found, forgot to register ? */
321 assert(0);
322}
323
324void rcu_register_thread(void)
325{
326 internal_urcu_lock();
327 urcu_init(); /* In case gcc does not support constructor attribute */
328 rcu_add_reader(pthread_self());
329 internal_urcu_unlock();
330}
331
332void rcu_unregister_thread(void)
333{
334 internal_urcu_lock();
335 rcu_remove_reader(pthread_self());
336 internal_urcu_unlock();
337}
338
339#ifndef DEBUG_FULL_MB
340static void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
341{
342 /*
343 * Executing this smp_mb() is the only purpose of this signal handler.
344 * It punctually promotes barrier() into smp_mb() on every thread it is
345 * executed on.
346 */
347 smp_mb();
348 need_mb = 0;
349 smp_mb();
350}
351
352/*
353 * urcu_init constructor. Called when the library is linked, but also when
354 * reader threads are calling rcu_register_thread().
355 * Should only be called by a single thread at a given time. This is ensured by
356 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
357 * library load time, which should not be executed by multiple threads nor
358 * concurrently with rcu_register_thread() anyway.
359 */
360void urcu_init(void)
361{
362 struct sigaction act;
363 int ret;
364
365 if (init_done)
366 return;
367 init_done = 1;
368
369 act.sa_sigaction = sigurcu_handler;
370 ret = sigaction(SIGURCU, &act, NULL);
371 if (ret) {
372 perror("Error in sigaction");
373 exit(-1);
374 }
375}
376
377void urcu_exit(void)
378{
379 struct sigaction act;
380 int ret;
381
382 ret = sigaction(SIGURCU, NULL, &act);
383 if (ret) {
384 perror("Error in sigaction");
385 exit(-1);
386 }
387 assert(act.sa_sigaction == sigurcu_handler);
388 free(registry);
389}
390#endif /* #ifndef DEBUG_FULL_MB */
This page took 0.022701 seconds and 4 git commands to generate.