f219e7632f50d232cbb3f73c75b097f56246215f
[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 DEBUG_FULL_MB
40 void __attribute__((constructor)) urcu_init(void);
41 void __attribute__((destructor)) urcu_exit(void);
42 #else
43 static inline void urcu_init(void)
44 {
45 }
46 #endif
47
48 int init_done;
49
50 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_CTR_BIT 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 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 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 DEBUG_FULL_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 DEBUG_FULL_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 DEBUG_FULL_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_publish_content_sym(void **p, void *v)
351 {
352 void *oldptr;
353
354 oldptr = _rcu_xchg_pointer(p, v);
355 synchronize_rcu();
356 return oldptr;
357 }
358
359 static void rcu_add_reader(pthread_t id)
360 {
361 struct reader_registry *oldarray;
362
363 if (!registry) {
364 alloc_readers = INIT_NUM_THREADS;
365 num_readers = 0;
366 registry =
367 malloc(sizeof(struct reader_registry) * alloc_readers);
368 }
369 if (alloc_readers < num_readers + 1) {
370 oldarray = registry;
371 registry = malloc(sizeof(struct reader_registry)
372 * (alloc_readers << 1));
373 memcpy(registry, oldarray,
374 sizeof(struct reader_registry) * alloc_readers);
375 alloc_readers <<= 1;
376 free(oldarray);
377 }
378 registry[num_readers].tid = id;
379 /* reference to the TLS of _this_ reader thread. */
380 registry[num_readers].urcu_active_readers = &urcu_active_readers;
381 registry[num_readers].need_mb = &need_mb;
382 num_readers++;
383 }
384
385 /*
386 * Never shrink (implementation limitation).
387 * This is O(nb threads). Eventually use a hash table.
388 */
389 static void rcu_remove_reader(pthread_t id)
390 {
391 struct reader_registry *index;
392
393 assert(registry != NULL);
394 for (index = registry; index < registry + num_readers; index++) {
395 if (pthread_equal(index->tid, id)) {
396 memcpy(index, &registry[num_readers - 1],
397 sizeof(struct reader_registry));
398 registry[num_readers - 1].tid = 0;
399 registry[num_readers - 1].urcu_active_readers = NULL;
400 num_readers--;
401 return;
402 }
403 }
404 /* Hrm not found, forgot to register ? */
405 assert(0);
406 }
407
408 void rcu_register_thread(void)
409 {
410 internal_urcu_lock();
411 urcu_init(); /* In case gcc does not support constructor attribute */
412 rcu_add_reader(pthread_self());
413 internal_urcu_unlock();
414 }
415
416 void rcu_unregister_thread(void)
417 {
418 internal_urcu_lock();
419 rcu_remove_reader(pthread_self());
420 internal_urcu_unlock();
421 }
422
423 #ifndef DEBUG_FULL_MB
424 static void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
425 {
426 /*
427 * Executing this smp_mb() is the only purpose of this signal handler.
428 * It punctually promotes barrier() into smp_mb() on every thread it is
429 * executed on.
430 */
431 smp_mb();
432 need_mb = 0;
433 smp_mb();
434 }
435
436 /*
437 * urcu_init constructor. Called when the library is linked, but also when
438 * reader threads are calling rcu_register_thread().
439 * Should only be called by a single thread at a given time. This is ensured by
440 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
441 * library load time, which should not be executed by multiple threads nor
442 * concurrently with rcu_register_thread() anyway.
443 */
444 void urcu_init(void)
445 {
446 struct sigaction act;
447 int ret;
448
449 if (init_done)
450 return;
451 init_done = 1;
452
453 act.sa_sigaction = sigurcu_handler;
454 act.sa_flags = SA_SIGINFO;
455 sigemptyset(&act.sa_mask);
456 ret = sigaction(SIGURCU, &act, NULL);
457 if (ret) {
458 perror("Error in sigaction");
459 exit(-1);
460 }
461 }
462
463 void urcu_exit(void)
464 {
465 struct sigaction act;
466 int ret;
467
468 ret = sigaction(SIGURCU, NULL, &act);
469 if (ret) {
470 perror("Error in sigaction");
471 exit(-1);
472 }
473 assert(act.sa_sigaction == sigurcu_handler);
474 free(registry);
475 }
476 #endif /* #ifndef DEBUG_FULL_MB */
This page took 0.038087 seconds and 4 git commands to generate.