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