ba013d990b6f052da8e85f69af7192de72a4b799
[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
44 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
45 #undef _LGPL_SOURCE
46 #include "urcu.h"
47 #define _LGPL_SOURCE
48
49 /*
50 * If a reader is really non-cooperative and refuses to commit its
51 * rcu_active_readers count to memory (there is no barrier in the reader
52 * per-se), kick it after a few loops waiting for it.
53 */
54 #define KICK_READER_LOOPS 10000
55
56 /*
57 * Active attempts to check for reader Q.S. before calling futex().
58 */
59 #define RCU_QS_ACTIVE_ATTEMPTS 100
60
61 #ifdef RCU_MEMBARRIER
62 static int init_done;
63 int has_sys_membarrier;
64
65 void __attribute__((constructor)) rcu_init(void);
66 #endif
67
68 #ifdef RCU_MB
69 void rcu_init(void)
70 {
71 }
72 #endif
73
74 #ifdef RCU_SIGNAL
75 static int init_done;
76
77 void __attribute__((constructor)) rcu_init(void);
78 void __attribute__((destructor)) rcu_exit(void);
79 #endif
80
81 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
82
83 int32_t gp_futex;
84
85 /*
86 * Global grace period counter.
87 * Contains the current RCU_GP_CTR_PHASE.
88 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
89 * Written to only by writer with mutex taken. Read by both writer and readers.
90 */
91 unsigned long rcu_gp_ctr = RCU_GP_COUNT;
92
93 /*
94 * Written to only by each individual reader. Read by both the reader and the
95 * writers.
96 */
97 struct rcu_reader __thread rcu_reader;
98
99 #ifdef DEBUG_YIELD
100 unsigned int yield_active;
101 unsigned int __thread rand_yield;
102 #endif
103
104 static CDS_LIST_HEAD(registry);
105
106 static void mutex_lock(pthread_mutex_t *mutex)
107 {
108 int ret;
109
110 #ifndef DISTRUST_SIGNALS_EXTREME
111 ret = pthread_mutex_lock(mutex);
112 if (ret) {
113 perror("Error in pthread mutex lock");
114 exit(-1);
115 }
116 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
117 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
118 if (ret != EBUSY && ret != EINTR) {
119 printf("ret = %d, errno = %d\n", ret, errno);
120 perror("Error in pthread mutex lock");
121 exit(-1);
122 }
123 if (CMM_LOAD_SHARED(rcu_reader.need_mb)) {
124 cmm_smp_mb();
125 _CMM_STORE_SHARED(rcu_reader.need_mb, 0);
126 cmm_smp_mb();
127 }
128 poll(NULL,0,10);
129 }
130 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
131 }
132
133 static void mutex_unlock(pthread_mutex_t *mutex)
134 {
135 int ret;
136
137 ret = pthread_mutex_unlock(mutex);
138 if (ret) {
139 perror("Error in pthread mutex unlock");
140 exit(-1);
141 }
142 }
143
144 #ifdef RCU_MEMBARRIER
145 static void smp_mb_master(int group)
146 {
147 if (caa_likely(has_sys_membarrier))
148 membarrier(MEMBARRIER_EXPEDITED);
149 else
150 cmm_smp_mb();
151 }
152 #endif
153
154 #ifdef RCU_MB
155 static void smp_mb_master(int group)
156 {
157 cmm_smp_mb();
158 }
159 #endif
160
161 #ifdef RCU_SIGNAL
162 static void force_mb_all_readers(void)
163 {
164 struct rcu_reader *index;
165
166 /*
167 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
168 * compiler barriers around rcu read lock as real memory barriers.
169 */
170 if (cds_list_empty(&registry))
171 return;
172 /*
173 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
174 * a cache flush on architectures with non-coherent cache. Let's play
175 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
176 * cache flush is enforced.
177 */
178 cds_list_for_each_entry(index, &registry, node) {
179 CMM_STORE_SHARED(index->need_mb, 1);
180 pthread_kill(index->tid, SIGRCU);
181 }
182 /*
183 * Wait for sighandler (and thus mb()) to execute on every thread.
184 *
185 * Note that the pthread_kill() will never be executed on systems
186 * that correctly deliver signals in a timely manner. However, it
187 * is not uncommon for kernels to have bugs that can result in
188 * lost or unduly delayed signals.
189 *
190 * If you are seeing the below pthread_kill() executing much at
191 * all, we suggest testing the underlying kernel and filing the
192 * relevant bug report. For Linux kernels, we recommend getting
193 * the Linux Test Project (LTP).
194 */
195 cds_list_for_each_entry(index, &registry, node) {
196 while (CMM_LOAD_SHARED(index->need_mb)) {
197 pthread_kill(index->tid, SIGRCU);
198 poll(NULL, 0, 1);
199 }
200 }
201 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
202 }
203
204 static void smp_mb_master(int group)
205 {
206 force_mb_all_readers();
207 }
208 #endif /* #ifdef RCU_SIGNAL */
209
210 /*
211 * synchronize_rcu() waiting. Single thread.
212 */
213 static void wait_gp(void)
214 {
215 /* Read reader_gp before read futex */
216 smp_mb_master(RCU_MB_GROUP);
217 if (uatomic_read(&gp_futex) == -1)
218 futex_async(&gp_futex, FUTEX_WAIT, -1,
219 NULL, NULL, 0);
220 }
221
222 void update_counter_and_wait(void)
223 {
224 CDS_LIST_HEAD(qsreaders);
225 int wait_loops = 0;
226 struct rcu_reader *index, *tmp;
227
228 /* Switch parity: 0 -> 1, 1 -> 0 */
229 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
230
231 /*
232 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
233 * state. Failure to do so could result in the writer waiting forever
234 * while new readers are always accessing data (no progress). Enforce
235 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
236 */
237 cmm_barrier();
238
239 /*
240 *
241 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
242 * model easier to understand. It does not have a big performance impact
243 * anyway, given this is the write-side.
244 */
245 cmm_smp_mb();
246
247 /*
248 * Wait for each thread rcu_reader.ctr count to become 0.
249 */
250 for (;;) {
251 wait_loops++;
252 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
253 uatomic_dec(&gp_futex);
254 /* Write futex before read reader_gp */
255 smp_mb_master(RCU_MB_GROUP);
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 * rcu_reader.ctr update to memory if we wait for too long.
281 */
282 if (cds_list_empty(&registry)) {
283 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
284 /* Read reader_gp before write futex */
285 smp_mb_master(RCU_MB_GROUP);
286 uatomic_set(&gp_futex, 0);
287 }
288 break;
289 } else {
290 switch (wait_loops) {
291 case RCU_QS_ACTIVE_ATTEMPTS:
292 wait_gp();
293 break; /* only escape switch */
294 case KICK_READER_LOOPS:
295 smp_mb_master(RCU_MB_GROUP);
296 wait_loops = 0;
297 break; /* only escape switch */
298 default:
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 * 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 rcu_reader.tid = pthread_self();
372 assert(rcu_reader.need_mb == 0);
373 assert(!(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(&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(&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(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 perror("Error in sigaction");
435 exit(-1);
436 }
437 }
438
439 void rcu_exit(void)
440 {
441 struct sigaction act;
442 int ret;
443
444 ret = sigaction(SIGRCU, NULL, &act);
445 if (ret) {
446 perror("Error in sigaction");
447 exit(-1);
448 }
449 assert(act.sa_sigaction == sigrcu_handler);
450 assert(cds_list_empty(&registry));
451 }
452
453 #endif /* #ifdef RCU_SIGNAL */
454
455 #include "urcu-call-rcu-impl.h"
456 #include "urcu-defer-impl.h"
This page took 0.048244 seconds and 3 git commands to generate.