a5178c0054bea5f0363b571d542b98b4d6803f9e
[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 #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 a few loops waiting for it.
56 */
57 #define KICK_READER_LOOPS 10000
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(struct rcu_reader, rcu_reader);
101
102 #ifdef DEBUG_YIELD
103 unsigned int yield_active;
104 DEFINE_URCU_TLS(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 int wait_loops = 0;
222 struct rcu_reader *index, *tmp;
223
224 /* Switch parity: 0 -> 1, 1 -> 0 */
225 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
226
227 /*
228 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
229 * state. Failure to do so could result in the writer waiting forever
230 * while new readers are always accessing data (no progress). Enforce
231 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
232 */
233 cmm_barrier();
234
235 /*
236 *
237 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
238 * model easier to understand. It does not have a big performance impact
239 * anyway, given this is the write-side.
240 */
241 cmm_smp_mb();
242
243 /*
244 * Wait for each thread URCU_TLS(rcu_reader).ctr count to become 0.
245 */
246 for (;;) {
247 wait_loops++;
248 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
249 uatomic_dec(&gp_futex);
250 /* Write futex before read reader_gp */
251 smp_mb_master(RCU_MB_GROUP);
252 }
253
254 cds_list_for_each_entry_safe(index, tmp, &registry, node) {
255 if (!rcu_gp_ongoing(&index->ctr))
256 cds_list_move(&index->node, &qsreaders);
257 }
258
259 #ifndef HAS_INCOHERENT_CACHES
260 if (cds_list_empty(&registry)) {
261 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
262 /* Read reader_gp before write futex */
263 smp_mb_master(RCU_MB_GROUP);
264 uatomic_set(&gp_futex, 0);
265 }
266 break;
267 } else {
268 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
269 wait_gp();
270 else
271 caa_cpu_relax();
272 }
273 #else /* #ifndef HAS_INCOHERENT_CACHES */
274 /*
275 * BUSY-LOOP. Force the reader thread to commit its
276 * URCU_TLS(rcu_reader).ctr update to memory if we wait
277 * for too long.
278 */
279 if (cds_list_empty(&registry)) {
280 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
281 /* Read reader_gp before write futex */
282 smp_mb_master(RCU_MB_GROUP);
283 uatomic_set(&gp_futex, 0);
284 }
285 break;
286 } else {
287 switch (wait_loops) {
288 case RCU_QS_ACTIVE_ATTEMPTS:
289 wait_gp();
290 break; /* only escape switch */
291 case KICK_READER_LOOPS:
292 smp_mb_master(RCU_MB_GROUP);
293 wait_loops = 0;
294 break; /* only escape switch */
295 default:
296 caa_cpu_relax();
297 }
298 }
299 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
300 }
301 /* put back the reader list in the registry */
302 cds_list_splice(&qsreaders, &registry);
303 }
304
305 void synchronize_rcu(void)
306 {
307 mutex_lock(&rcu_gp_lock);
308
309 if (cds_list_empty(&registry))
310 goto out;
311
312 /* All threads should read qparity before accessing data structure
313 * where new ptr points to. Must be done within rcu_gp_lock because it
314 * iterates on reader threads.*/
315 /* Write new ptr before changing the qparity */
316 smp_mb_master(RCU_MB_GROUP);
317
318 /*
319 * Wait for previous parity to be empty of readers.
320 */
321 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
322
323 /*
324 * Must finish waiting for quiescent state for parity 0 before
325 * committing next rcu_gp_ctr update to memory. Failure to do so could
326 * result in the writer waiting forever while new readers are always
327 * accessing data (no progress). Enforce compiler-order of load
328 * URCU_TLS(rcu_reader).ctr before store to rcu_gp_ctr.
329 */
330 cmm_barrier();
331
332 /*
333 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
334 * model easier to understand. It does not have a big performance impact
335 * anyway, given this is the write-side.
336 */
337 cmm_smp_mb();
338
339 /*
340 * Wait for previous parity to be empty of readers.
341 */
342 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
343
344 /* Finish waiting for reader threads before letting the old ptr being
345 * freed. Must be done within rcu_gp_lock because it iterates on reader
346 * threads. */
347 smp_mb_master(RCU_MB_GROUP);
348 out:
349 mutex_unlock(&rcu_gp_lock);
350 }
351
352 /*
353 * library wrappers to be used by non-LGPL compatible source code.
354 */
355
356 void rcu_read_lock(void)
357 {
358 _rcu_read_lock();
359 }
360
361 void rcu_read_unlock(void)
362 {
363 _rcu_read_unlock();
364 }
365
366 void rcu_register_thread(void)
367 {
368 URCU_TLS(rcu_reader).tid = pthread_self();
369 assert(URCU_TLS(rcu_reader).need_mb == 0);
370 assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK));
371
372 mutex_lock(&rcu_gp_lock);
373 rcu_init(); /* In case gcc does not support constructor attribute */
374 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
375 mutex_unlock(&rcu_gp_lock);
376 }
377
378 void rcu_unregister_thread(void)
379 {
380 mutex_lock(&rcu_gp_lock);
381 cds_list_del(&URCU_TLS(rcu_reader).node);
382 mutex_unlock(&rcu_gp_lock);
383 }
384
385 #ifdef RCU_MEMBARRIER
386 void rcu_init(void)
387 {
388 if (init_done)
389 return;
390 init_done = 1;
391 if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY))
392 has_sys_membarrier = 1;
393 }
394 #endif
395
396 #ifdef RCU_SIGNAL
397 static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context)
398 {
399 /*
400 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
401 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
402 * executed on.
403 */
404 cmm_smp_mb();
405 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
406 cmm_smp_mb();
407 }
408
409 /*
410 * rcu_init constructor. Called when the library is linked, but also when
411 * reader threads are calling rcu_register_thread().
412 * Should only be called by a single thread at a given time. This is ensured by
413 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
414 * load time, which should not be executed by multiple threads nor concurrently
415 * with rcu_register_thread() anyway.
416 */
417 void rcu_init(void)
418 {
419 struct sigaction act;
420 int ret;
421
422 if (init_done)
423 return;
424 init_done = 1;
425
426 act.sa_sigaction = sigrcu_handler;
427 act.sa_flags = SA_SIGINFO | SA_RESTART;
428 sigemptyset(&act.sa_mask);
429 ret = sigaction(SIGRCU, &act, NULL);
430 if (ret)
431 urcu_die(errno);
432 }
433
434 void rcu_exit(void)
435 {
436 struct sigaction act;
437 int ret;
438
439 ret = sigaction(SIGRCU, NULL, &act);
440 if (ret)
441 urcu_die(errno);
442 assert(act.sa_sigaction == sigrcu_handler);
443 assert(cds_list_empty(&registry));
444 }
445
446 #endif /* #ifdef RCU_SIGNAL */
447
448 DEFINE_RCU_FLAVOR(rcu_flavor);
449
450 #include "urcu-call-rcu-impl.h"
451 #include "urcu-defer-impl.h"
This page took 0.038055 seconds and 4 git commands to generate.