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