Don't mark reader thread as being waited for if non-contended
[userspace-rcu.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 struct urcu_reader_status __thread urcu_reader_status;
65
66 /* Thread IDs of registered readers */
67 #define INIT_NUM_THREADS 4
68
69 struct reader_registry {
70 pthread_t tid;
71 struct urcu_reader_status *urcu_reader_status;
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 active_readers count to become 0.
219 */
220 for (index = registry; index < registry + num_readers; index++) {
221 int wait_loops = 0;
222
223 if (likely(!rcu_old_gp_ongoing(
224 &index->urcu_reader_status->active_readers)))
225 continue;
226
227 index->urcu_reader_status->gp_waiting = 1;
228 #ifndef HAS_INCOHERENT_CACHES
229 while (rcu_old_gp_ongoing(
230 &index->urcu_reader_status->active_readers)) {
231 if (wait_loops++ == RCU_QS_ACTIVE_ATTEMPTS) {
232 sched_yield(); /* ideally sched_yield_to() */
233 } else {
234 cpu_relax();
235 }
236 }
237 #else /* #ifndef HAS_INCOHERENT_CACHES */
238 /*
239 * BUSY-LOOP. Force the reader thread to commit its
240 * active_readers update to memory if we wait for too long.
241 */
242 while (rcu_old_gp_ongoing(
243 &index->urcu_reader_status->active_readers)) {
244 switch (wait_loops++) {
245 case RCU_QS_ACTIVE_ATTEMPTS:
246 sched_yield(); /* ideally sched_yield_to() */
247 break;
248 case KICK_READER_LOOPS:
249 force_mb_single_thread(index);
250 wait_loops = 0;
251 break;
252 default:
253 cpu_relax();
254 }
255 }
256 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
257 index->urcu_reader_status->gp_waiting = 0;
258 }
259 }
260
261 void synchronize_rcu(void)
262 {
263 internal_urcu_lock();
264
265 /* All threads should read qparity before accessing data structure
266 * where new ptr points to. Must be done within internal_urcu_lock
267 * because it iterates on reader threads.*/
268 /* Write new ptr before changing the qparity */
269 force_mb_all_threads();
270
271 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
272
273 switch_next_urcu_qparity(); /* 0 -> 1 */
274
275 /*
276 * Must commit qparity update to memory before waiting for parity
277 * 0 quiescent state. Failure to do so could result in the writer
278 * waiting forever while new readers are always accessing data (no
279 * progress).
280 * Ensured by STORE_SHARED and LOAD_SHARED.
281 */
282
283 /*
284 * Adding a smp_mb() which is _not_ formally required, but makes the
285 * model easier to understand. It does not have a big performance impact
286 * anyway, given this is the write-side.
287 */
288 smp_mb();
289
290 /*
291 * Wait for previous parity to be empty of readers.
292 */
293 wait_for_quiescent_state(); /* Wait readers in parity 0 */
294
295 /*
296 * Must finish waiting for quiescent state for parity 0 before
297 * committing qparity update to memory. Failure to do so could result in
298 * the writer waiting forever while new readers are always accessing
299 * data (no progress).
300 * Ensured by STORE_SHARED and LOAD_SHARED.
301 */
302
303 /*
304 * Adding a smp_mb() which is _not_ formally required, but makes the
305 * model easier to understand. It does not have a big performance impact
306 * anyway, given this is the write-side.
307 */
308 smp_mb();
309
310 switch_next_urcu_qparity(); /* 1 -> 0 */
311
312 /*
313 * Must commit qparity update to memory before waiting for parity
314 * 1 quiescent state. Failure to do so could result in the writer
315 * waiting forever while new readers are always accessing data (no
316 * progress).
317 * Ensured by STORE_SHARED and LOAD_SHARED.
318 */
319
320 /*
321 * Adding a smp_mb() which is _not_ formally required, but makes the
322 * model easier to understand. It does not have a big performance impact
323 * anyway, given this is the write-side.
324 */
325 smp_mb();
326
327 /*
328 * Wait for previous parity to be empty of readers.
329 */
330 wait_for_quiescent_state(); /* Wait readers in parity 1 */
331
332 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
333
334 /* Finish waiting for reader threads before letting the old ptr being
335 * freed. Must be done within internal_urcu_lock because it iterates on
336 * reader threads. */
337 force_mb_all_threads();
338
339 internal_urcu_unlock();
340 }
341
342 /*
343 * library wrappers to be used by non-LGPL compatible source code.
344 */
345
346 void rcu_read_lock(void)
347 {
348 _rcu_read_lock();
349 }
350
351 void rcu_read_unlock(void)
352 {
353 _rcu_read_unlock();
354 }
355
356 void *rcu_dereference(void *p)
357 {
358 return _rcu_dereference(p);
359 }
360
361 void *rcu_assign_pointer_sym(void **p, void *v)
362 {
363 wmb();
364 return STORE_SHARED(p, v);
365 }
366
367 void *rcu_xchg_pointer_sym(void **p, void *v)
368 {
369 wmb();
370 return xchg(p, v);
371 }
372
373 void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new)
374 {
375 wmb();
376 return cmpxchg(p, old, _new);
377 }
378
379 void *rcu_publish_content_sym(void **p, void *v)
380 {
381 void *oldptr;
382
383 oldptr = _rcu_xchg_pointer(p, v);
384 synchronize_rcu();
385 return oldptr;
386 }
387
388 static void rcu_add_reader(pthread_t id)
389 {
390 struct reader_registry *oldarray;
391
392 if (!registry) {
393 alloc_readers = INIT_NUM_THREADS;
394 num_readers = 0;
395 registry =
396 malloc(sizeof(struct reader_registry) * alloc_readers);
397 }
398 if (alloc_readers < num_readers + 1) {
399 oldarray = registry;
400 registry = malloc(sizeof(struct reader_registry)
401 * (alloc_readers << 1));
402 memcpy(registry, oldarray,
403 sizeof(struct reader_registry) * alloc_readers);
404 alloc_readers <<= 1;
405 free(oldarray);
406 }
407 registry[num_readers].tid = id;
408 /* reference to the TLS of _this_ reader thread. */
409 registry[num_readers].urcu_reader_status = &urcu_reader_status;
410 registry[num_readers].need_mb = &need_mb;
411 num_readers++;
412 }
413
414 /*
415 * Never shrink (implementation limitation).
416 * This is O(nb threads). Eventually use a hash table.
417 */
418 static void rcu_remove_reader(pthread_t id)
419 {
420 struct reader_registry *index;
421
422 assert(registry != NULL);
423 for (index = registry; index < registry + num_readers; index++) {
424 if (pthread_equal(index->tid, id)) {
425 memcpy(index, &registry[num_readers - 1],
426 sizeof(struct reader_registry));
427 registry[num_readers - 1].tid = 0;
428 registry[num_readers - 1].urcu_reader_status = NULL;
429 num_readers--;
430 return;
431 }
432 }
433 /* Hrm not found, forgot to register ? */
434 assert(0);
435 }
436
437 void rcu_register_thread(void)
438 {
439 internal_urcu_lock();
440 urcu_init(); /* In case gcc does not support constructor attribute */
441 rcu_add_reader(pthread_self());
442 internal_urcu_unlock();
443 }
444
445 void rcu_unregister_thread(void)
446 {
447 internal_urcu_lock();
448 rcu_remove_reader(pthread_self());
449 internal_urcu_unlock();
450 }
451
452 #ifndef URCU_MB
453 static void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
454 {
455 /*
456 * Executing this smp_mb() is the only purpose of this signal handler.
457 * It punctually promotes barrier() into smp_mb() on every thread it is
458 * executed on.
459 */
460 smp_mb();
461 need_mb = 0;
462 smp_mb();
463 }
464
465 /*
466 * urcu_init constructor. Called when the library is linked, but also when
467 * reader threads are calling rcu_register_thread().
468 * Should only be called by a single thread at a given time. This is ensured by
469 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
470 * library load time, which should not be executed by multiple threads nor
471 * concurrently with rcu_register_thread() anyway.
472 */
473 void urcu_init(void)
474 {
475 struct sigaction act;
476 int ret;
477
478 if (init_done)
479 return;
480 init_done = 1;
481
482 act.sa_sigaction = sigurcu_handler;
483 act.sa_flags = SA_SIGINFO | SA_RESTART;
484 sigemptyset(&act.sa_mask);
485 ret = sigaction(SIGURCU, &act, NULL);
486 if (ret) {
487 perror("Error in sigaction");
488 exit(-1);
489 }
490 }
491
492 void urcu_exit(void)
493 {
494 struct sigaction act;
495 int ret;
496
497 ret = sigaction(SIGURCU, NULL, &act);
498 if (ret) {
499 perror("Error in sigaction");
500 exit(-1);
501 }
502 assert(act.sa_sigaction == sigurcu_handler);
503 free(registry);
504 }
505 #endif /* #ifndef URCU_MB */
This page took 0.041573 seconds and 5 git commands to generate.