32f74880492ce40527c8b5b40560249eac485b90
[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 #include <stdio.h>
27 #include <pthread.h>
28 #include <signal.h>
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <poll.h>
34
35 #include "urcu-static.h"
36 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
37 #include "urcu.h"
38
39 #ifndef URCU_MB
40 static int init_done;
41
42 void __attribute__((constructor)) urcu_init(void);
43 void __attribute__((destructor)) urcu_exit(void);
44 #else
45 void urcu_init(void)
46 {
47 }
48 #endif
49
50 static pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
51
52 /*
53 * Global grace period counter.
54 * Contains the current RCU_GP_CTR_BIT.
55 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
56 * Written to only by writer with mutex taken. Read by both writer and readers.
57 */
58 long urcu_gp_ctr = RCU_GP_COUNT;
59
60 /*
61 * Written to only by each individual reader. Read by both the reader and the
62 * writers.
63 */
64 long __thread urcu_active_readers;
65
66 /* Thread IDs of registered readers */
67 #define INIT_NUM_THREADS 4
68
69 struct reader_registry {
70 pthread_t tid;
71 long *urcu_active_readers;
72 char *need_mb;
73 };
74
75 #ifdef DEBUG_YIELD
76 unsigned int yield_active;
77 unsigned int __thread rand_yield;
78 #endif
79
80 static struct reader_registry *registry;
81 static char __thread need_mb;
82 static int num_readers, alloc_readers;
83
84 static void internal_urcu_lock(void)
85 {
86 int ret;
87
88 #ifndef DISTRUST_SIGNALS_EXTREME
89 ret = pthread_mutex_lock(&urcu_mutex);
90 if (ret) {
91 perror("Error in pthread mutex lock");
92 exit(-1);
93 }
94 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
95 while ((ret = pthread_mutex_trylock(&urcu_mutex)) != 0) {
96 if (ret != EBUSY && ret != EINTR) {
97 printf("ret = %d, errno = %d\n", ret, errno);
98 perror("Error in pthread mutex lock");
99 exit(-1);
100 }
101 if (need_mb) {
102 smp_mb();
103 need_mb = 0;
104 smp_mb();
105 }
106 poll(NULL,0,10);
107 }
108 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
109 }
110
111 static void internal_urcu_unlock(void)
112 {
113 int ret;
114
115 ret = pthread_mutex_unlock(&urcu_mutex);
116 if (ret) {
117 perror("Error in pthread mutex unlock");
118 exit(-1);
119 }
120 }
121
122 /*
123 * called with urcu_mutex held.
124 */
125 static void switch_next_urcu_qparity(void)
126 {
127 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR_BIT);
128 }
129
130 #ifdef URCU_MB
131 #ifdef HAS_INCOHERENT_CACHES
132 static void force_mb_single_thread(struct reader_registry *index)
133 {
134 smp_mb();
135 }
136 #endif /* #ifdef HAS_INCOHERENT_CACHES */
137
138 static void force_mb_all_threads(void)
139 {
140 smp_mb();
141 }
142 #else /* #ifdef URCU_MB */
143 #ifdef HAS_INCOHERENT_CACHES
144 static void force_mb_single_thread(struct reader_registry *index)
145 {
146 assert(registry);
147 /*
148 * pthread_kill has a smp_mb(). But beware, we assume it performs
149 * a cache flush on architectures with non-coherent cache. Let's play
150 * safe and don't assume anything : we use smp_mc() to make sure the
151 * cache flush is enforced.
152 */
153 *index->need_mb = 1;
154 smp_mc(); /* write ->need_mb before sending the signals */
155 pthread_kill(index->tid, SIGURCU);
156 smp_mb();
157 /*
158 * Wait for sighandler (and thus mb()) to execute on every thread.
159 * BUSY-LOOP.
160 */
161 while (*index->need_mb) {
162 poll(NULL, 0, 1);
163 }
164 smp_mb(); /* read ->need_mb before ending the barrier */
165 }
166 #endif /* #ifdef HAS_INCOHERENT_CACHES */
167
168 static void force_mb_all_threads(void)
169 {
170 struct reader_registry *index;
171 /*
172 * Ask for each threads to execute a smp_mb() so we can consider the
173 * compiler barriers around rcu read lock as real memory barriers.
174 */
175 if (!registry)
176 return;
177 /*
178 * pthread_kill has a smp_mb(). But beware, we assume it performs
179 * a cache flush on architectures with non-coherent cache. Let's play
180 * safe and don't assume anything : we use smp_mc() to make sure the
181 * cache flush is enforced.
182 */
183 for (index = registry; index < registry + num_readers; index++) {
184 *index->need_mb = 1;
185 smp_mc(); /* write need_mb before sending the signal */
186 pthread_kill(index->tid, SIGURCU);
187 }
188 /*
189 * Wait for sighandler (and thus mb()) to execute on every thread.
190 *
191 * Note that the pthread_kill() will never be executed on systems
192 * that correctly deliver signals in a timely manner. However, it
193 * is not uncommon for kernels to have bugs that can result in
194 * lost or unduly delayed signals.
195 *
196 * If you are seeing the below pthread_kill() executing much at
197 * all, we suggest testing the underlying kernel and filing the
198 * relevant bug report. For Linux kernels, we recommend getting
199 * the Linux Test Project (LTP).
200 */
201 for (index = registry; index < registry + num_readers; index++) {
202 while (*index->need_mb) {
203 pthread_kill(index->tid, SIGURCU);
204 poll(NULL, 0, 1);
205 }
206 }
207 smp_mb(); /* read ->need_mb before ending the barrier */
208 }
209 #endif /* #else #ifdef URCU_MB */
210
211 void wait_for_quiescent_state(void)
212 {
213 struct reader_registry *index;
214
215 if (!registry)
216 return;
217 /*
218 * Wait for each thread urcu_active_readers count to become 0.
219 */
220 for (index = registry; index < registry + num_readers; index++) {
221 #ifndef HAS_INCOHERENT_CACHES
222 while (rcu_old_gp_ongoing(index->urcu_active_readers))
223 cpu_relax();
224 #else /* #ifndef HAS_INCOHERENT_CACHES */
225 int wait_loops = 0;
226 /*
227 * BUSY-LOOP. Force the reader thread to commit its
228 * urcu_active_readers update to memory if we wait for too long.
229 */
230 while (rcu_old_gp_ongoing(index->urcu_active_readers)) {
231 if (wait_loops++ == KICK_READER_LOOPS) {
232 force_mb_single_thread(index);
233 wait_loops = 0;
234 } else {
235 cpu_relax();
236 }
237 }
238 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
239 }
240 }
241
242 void synchronize_rcu(void)
243 {
244 internal_urcu_lock();
245
246 /* All threads should read qparity before accessing data structure
247 * where new ptr points to. Must be done within internal_urcu_lock
248 * because it iterates on reader threads.*/
249 /* Write new ptr before changing the qparity */
250 force_mb_all_threads();
251
252 switch_next_urcu_qparity(); /* 0 -> 1 */
253
254 /*
255 * Must commit qparity update to memory before waiting for parity
256 * 0 quiescent state. Failure to do so could result in the writer
257 * waiting forever while new readers are always accessing data (no
258 * progress).
259 * Ensured by STORE_SHARED and LOAD_SHARED.
260 */
261
262 /*
263 * Adding a smp_mb() which is _not_ formally required, but makes the
264 * model easier to understand. It does not have a big performance impact
265 * anyway, given this is the write-side.
266 */
267 smp_mb();
268
269 /*
270 * Wait for previous parity to be empty of readers.
271 */
272 wait_for_quiescent_state(); /* Wait readers in parity 0 */
273
274 /*
275 * Must finish waiting for quiescent state for parity 0 before
276 * committing qparity update to memory. Failure to do so could result in
277 * the writer waiting forever while new readers are always accessing
278 * data (no progress).
279 * Ensured by STORE_SHARED and LOAD_SHARED.
280 */
281
282 /*
283 * Adding a smp_mb() which is _not_ formally required, but makes the
284 * model easier to understand. It does not have a big performance impact
285 * anyway, given this is the write-side.
286 */
287 smp_mb();
288
289 switch_next_urcu_qparity(); /* 1 -> 0 */
290
291 /*
292 * Must commit qparity update to memory before waiting for parity
293 * 1 quiescent state. Failure to do so could result in the writer
294 * waiting forever while new readers are always accessing data (no
295 * progress).
296 * Ensured by STORE_SHARED and LOAD_SHARED.
297 */
298
299 /*
300 * Adding a smp_mb() which is _not_ formally required, but makes the
301 * model easier to understand. It does not have a big performance impact
302 * anyway, given this is the write-side.
303 */
304 smp_mb();
305
306 /*
307 * Wait for previous parity to be empty of readers.
308 */
309 wait_for_quiescent_state(); /* Wait readers in parity 1 */
310
311 /* Finish waiting for reader threads before letting the old ptr being
312 * freed. Must be done within internal_urcu_lock because it iterates on
313 * reader threads. */
314 force_mb_all_threads();
315
316 internal_urcu_unlock();
317 }
318
319 /*
320 * library wrappers to be used by non-LGPL compatible source code.
321 */
322
323 void rcu_read_lock(void)
324 {
325 _rcu_read_lock();
326 }
327
328 void rcu_read_unlock(void)
329 {
330 _rcu_read_unlock();
331 }
332
333 void *rcu_dereference(void *p)
334 {
335 return _rcu_dereference(p);
336 }
337
338 void *rcu_assign_pointer_sym(void **p, void *v)
339 {
340 wmb();
341 return STORE_SHARED(p, v);
342 }
343
344 void *rcu_xchg_pointer_sym(void **p, void *v)
345 {
346 wmb();
347 return xchg(p, v);
348 }
349
350 void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new)
351 {
352 wmb();
353 return cmpxchg(p, old, _new);
354 }
355
356 void *rcu_publish_content_sym(void **p, void *v)
357 {
358 void *oldptr;
359
360 oldptr = _rcu_xchg_pointer(p, v);
361 synchronize_rcu();
362 return oldptr;
363 }
364
365 static void rcu_add_reader(pthread_t id)
366 {
367 struct reader_registry *oldarray;
368
369 if (!registry) {
370 alloc_readers = INIT_NUM_THREADS;
371 num_readers = 0;
372 registry =
373 malloc(sizeof(struct reader_registry) * alloc_readers);
374 }
375 if (alloc_readers < num_readers + 1) {
376 oldarray = registry;
377 registry = malloc(sizeof(struct reader_registry)
378 * (alloc_readers << 1));
379 memcpy(registry, oldarray,
380 sizeof(struct reader_registry) * alloc_readers);
381 alloc_readers <<= 1;
382 free(oldarray);
383 }
384 registry[num_readers].tid = id;
385 /* reference to the TLS of _this_ reader thread. */
386 registry[num_readers].urcu_active_readers = &urcu_active_readers;
387 registry[num_readers].need_mb = &need_mb;
388 num_readers++;
389 }
390
391 /*
392 * Never shrink (implementation limitation).
393 * This is O(nb threads). Eventually use a hash table.
394 */
395 static void rcu_remove_reader(pthread_t id)
396 {
397 struct reader_registry *index;
398
399 assert(registry != NULL);
400 for (index = registry; index < registry + num_readers; index++) {
401 if (pthread_equal(index->tid, id)) {
402 memcpy(index, &registry[num_readers - 1],
403 sizeof(struct reader_registry));
404 registry[num_readers - 1].tid = 0;
405 registry[num_readers - 1].urcu_active_readers = NULL;
406 num_readers--;
407 return;
408 }
409 }
410 /* Hrm not found, forgot to register ? */
411 assert(0);
412 }
413
414 void rcu_register_thread(void)
415 {
416 internal_urcu_lock();
417 urcu_init(); /* In case gcc does not support constructor attribute */
418 rcu_add_reader(pthread_self());
419 internal_urcu_unlock();
420 }
421
422 void rcu_unregister_thread(void)
423 {
424 internal_urcu_lock();
425 rcu_remove_reader(pthread_self());
426 internal_urcu_unlock();
427 }
428
429 #ifndef URCU_MB
430 static void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
431 {
432 /*
433 * Executing this smp_mb() is the only purpose of this signal handler.
434 * It punctually promotes barrier() into smp_mb() on every thread it is
435 * executed on.
436 */
437 smp_mb();
438 need_mb = 0;
439 smp_mb();
440 }
441
442 /*
443 * urcu_init constructor. Called when the library is linked, but also when
444 * reader threads are calling rcu_register_thread().
445 * Should only be called by a single thread at a given time. This is ensured by
446 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
447 * library load time, which should not be executed by multiple threads nor
448 * concurrently with rcu_register_thread() anyway.
449 */
450 void urcu_init(void)
451 {
452 struct sigaction act;
453 int ret;
454
455 if (init_done)
456 return;
457 init_done = 1;
458
459 act.sa_sigaction = sigurcu_handler;
460 act.sa_flags = SA_SIGINFO | SA_RESTART;
461 sigemptyset(&act.sa_mask);
462 ret = sigaction(SIGURCU, &act, NULL);
463 if (ret) {
464 perror("Error in sigaction");
465 exit(-1);
466 }
467 }
468
469 void urcu_exit(void)
470 {
471 struct sigaction act;
472 int ret;
473
474 ret = sigaction(SIGURCU, NULL, &act);
475 if (ret) {
476 perror("Error in sigaction");
477 exit(-1);
478 }
479 assert(act.sa_sigaction == sigurcu_handler);
480 free(registry);
481 }
482 #endif /* #ifndef URCU_MB */
This page took 0.042295 seconds and 4 git commands to generate.