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