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