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