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