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