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