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