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