Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * urcu.c | |
3 | * | |
4 | * Userspace RCU library | |
5 | * | |
af02d47e MD |
6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
7 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. | |
b257a10b | 8 | * |
af02d47e MD |
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 | |
54843abc PM |
22 | * |
23 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. | |
b257a10b MD |
24 | */ |
25 | ||
27b012e2 MD |
26 | #include <stdio.h> |
27 | #include <pthread.h> | |
28 | #include <signal.h> | |
29 | #include <assert.h> | |
f69f195a MD |
30 | #include <stdlib.h> |
31 | #include <string.h> | |
09a9f986 | 32 | #include <errno.h> |
e8043c1b | 33 | #include <poll.h> |
27b012e2 | 34 | |
121a5d44 MD |
35 | #include "urcu-static.h" |
36 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ | |
27b012e2 MD |
37 | #include "urcu.h" |
38 | ||
0a1d290b | 39 | #ifndef URCU_MB |
834a45ba MD |
40 | static int init_done; |
41 | ||
8a5fb4c9 MD |
42 | void __attribute__((constructor)) urcu_init(void); |
43 | void __attribute__((destructor)) urcu_exit(void); | |
e90a6e9c | 44 | #else |
538d7df5 | 45 | void urcu_init(void) |
e90a6e9c MD |
46 | { |
47 | } | |
48 | #endif | |
8a5fb4c9 | 49 | |
81d1f1f5 | 50 | static pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
27b012e2 | 51 | |
128166c9 MD |
52 | /* |
53 | * Global grace period counter. | |
54 | * Contains the current RCU_GP_CTR_BIT. | |
afb8f2c9 | 55 | * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path. |
b0d5e790 | 56 | * Written to only by writer with mutex taken. Read by both writer and readers. |
128166c9 MD |
57 | */ |
58 | long urcu_gp_ctr = RCU_GP_COUNT; | |
27b012e2 | 59 | |
b0d5e790 MD |
60 | /* |
61 | * Written to only by each individual reader. Read by both the reader and the | |
62 | * writers. | |
63 | */ | |
8b25e300 | 64 | struct urcu_reader_status __thread urcu_reader_status; |
27b012e2 MD |
65 | |
66 | /* Thread IDs of registered readers */ | |
67 | #define INIT_NUM_THREADS 4 | |
68 | ||
0a52082b | 69 | struct reader_registry { |
27b012e2 | 70 | pthread_t tid; |
8b25e300 | 71 | struct urcu_reader_status *urcu_reader_status; |
09a9f986 | 72 | char *need_mb; |
27b012e2 MD |
73 | }; |
74 | ||
cf380c2f | 75 | #ifdef DEBUG_YIELD |
9d335088 MD |
76 | unsigned int yield_active; |
77 | unsigned int __thread rand_yield; | |
cf380c2f MD |
78 | #endif |
79 | ||
0a52082b | 80 | static struct reader_registry *registry; |
09a9f986 | 81 | static char __thread need_mb; |
27b012e2 | 82 | static int num_readers, alloc_readers; |
27b012e2 | 83 | |
81d1f1f5 | 84 | static void internal_urcu_lock(void) |
41718ff9 MD |
85 | { |
86 | int ret; | |
09a9f986 PM |
87 | |
88 | #ifndef DISTRUST_SIGNALS_EXTREME | |
41718ff9 MD |
89 | ret = pthread_mutex_lock(&urcu_mutex); |
90 | if (ret) { | |
91 | perror("Error in pthread mutex lock"); | |
92 | exit(-1); | |
93 | } | |
09a9f986 PM |
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 */ | |
41718ff9 MD |
109 | } |
110 | ||
81d1f1f5 | 111 | static void internal_urcu_unlock(void) |
41718ff9 MD |
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 | ||
27b012e2 MD |
122 | /* |
123 | * called with urcu_mutex held. | |
124 | */ | |
1430ee0b | 125 | static void switch_next_urcu_qparity(void) |
27b012e2 | 126 | { |
b0d5e790 | 127 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR_BIT); |
27b012e2 MD |
128 | } |
129 | ||
0a1d290b | 130 | #ifdef URCU_MB |
ae62b5e8 | 131 | #ifdef HAS_INCOHERENT_CACHES |
09a9f986 | 132 | static void force_mb_single_thread(struct reader_registry *index) |
40e140c9 MD |
133 | { |
134 | smp_mb(); | |
135 | } | |
ae62b5e8 | 136 | #endif /* #ifdef HAS_INCOHERENT_CACHES */ |
40e140c9 | 137 | |
bb488185 MD |
138 | static void force_mb_all_threads(void) |
139 | { | |
b715b99e | 140 | smp_mb(); |
bb488185 | 141 | } |
0a1d290b | 142 | #else /* #ifdef URCU_MB */ |
ae62b5e8 | 143 | #ifdef HAS_INCOHERENT_CACHES |
09a9f986 | 144 | static void force_mb_single_thread(struct reader_registry *index) |
40e140c9 | 145 | { |
0a52082b | 146 | assert(registry); |
157dca95 MD |
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. | |
157dca95 | 152 | */ |
09a9f986 PM |
153 | *index->need_mb = 1; |
154 | smp_mc(); /* write ->need_mb before sending the signals */ | |
155 | pthread_kill(index->tid, SIGURCU); | |
156 | smp_mb(); | |
40e140c9 MD |
157 | /* |
158 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
159 | * BUSY-LOOP. | |
160 | */ | |
09a9f986 PM |
161 | while (*index->need_mb) { |
162 | poll(NULL, 0, 1); | |
163 | } | |
164 | smp_mb(); /* read ->need_mb before ending the barrier */ | |
40e140c9 | 165 | } |
ae62b5e8 | 166 | #endif /* #ifdef HAS_INCOHERENT_CACHES */ |
40e140c9 | 167 | |
27b012e2 MD |
168 | static void force_mb_all_threads(void) |
169 | { | |
0a52082b | 170 | struct reader_registry *index; |
27b012e2 | 171 | /* |
b715b99e | 172 | * Ask for each threads to execute a smp_mb() so we can consider the |
27b012e2 MD |
173 | * compiler barriers around rcu read lock as real memory barriers. |
174 | */ | |
0a52082b | 175 | if (!registry) |
27b012e2 | 176 | return; |
3a86deba MD |
177 | /* |
178 | * pthread_kill has a smp_mb(). But beware, we assume it performs | |
157dca95 MD |
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. | |
3a86deba | 182 | */ |
09a9f986 PM |
183 | for (index = registry; index < registry + num_readers; index++) { |
184 | *index->need_mb = 1; | |
185 | smp_mc(); /* write need_mb before sending the signal */ | |
f69f195a | 186 | pthread_kill(index->tid, SIGURCU); |
09a9f986 | 187 | } |
27b012e2 MD |
188 | /* |
189 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
09a9f986 PM |
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). | |
27b012e2 | 200 | */ |
09a9f986 PM |
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 */ | |
27b012e2 | 208 | } |
0a1d290b | 209 | #endif /* #else #ifdef URCU_MB */ |
27b012e2 | 210 | |
1430ee0b | 211 | void wait_for_quiescent_state(void) |
27b012e2 | 212 | { |
0a52082b | 213 | struct reader_registry *index; |
27b012e2 | 214 | |
0a52082b | 215 | if (!registry) |
27b012e2 | 216 | return; |
40e140c9 | 217 | /* |
8b25e300 | 218 | * Wait for each thread active_readers count to become 0. |
27b012e2 | 219 | */ |
0a52082b | 220 | for (index = registry; index < registry + num_readers; index++) { |
bc6c15bb | 221 | int wait_loops = 0; |
8b25e300 MD |
222 | |
223 | index->urcu_reader_status->gp_waiting = 1; | |
e8043c1b | 224 | #ifndef HAS_INCOHERENT_CACHES |
8b25e300 MD |
225 | while (rcu_old_gp_ongoing( |
226 | &index->urcu_reader_status->active_readers)) { | |
bc6c15bb | 227 | if (wait_loops++ == RCU_QS_ACTIVE_ATTEMPTS) { |
ae62b5e8 | 228 | sched_yield(); /* ideally sched_yield_to() */ |
bc6c15bb MD |
229 | } else { |
230 | cpu_relax(); | |
231 | } | |
232 | } | |
e8043c1b | 233 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 234 | /* |
40e140c9 | 235 | * BUSY-LOOP. Force the reader thread to commit its |
8b25e300 | 236 | * active_readers update to memory if we wait for too long. |
27b012e2 | 237 | */ |
8b25e300 MD |
238 | while (rcu_old_gp_ongoing( |
239 | &index->urcu_reader_status->active_readers)) { | |
bc6c15bb MD |
240 | switch (wait_loops++) { |
241 | case RCU_QS_ACTIVE_ATTEMPTS: | |
ae62b5e8 | 242 | sched_yield(); /* ideally sched_yield_to() */ |
bc6c15bb MD |
243 | break; |
244 | case KICK_READER_LOOPS: | |
09a9f986 | 245 | force_mb_single_thread(index); |
40e140c9 | 246 | wait_loops = 0; |
bc6c15bb MD |
247 | break; |
248 | default: | |
3b55dbf4 | 249 | cpu_relax(); |
40e140c9 MD |
250 | } |
251 | } | |
e8043c1b | 252 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
8b25e300 | 253 | index->urcu_reader_status->gp_waiting = 0; |
27b012e2 | 254 | } |
27b012e2 MD |
255 | } |
256 | ||
9598a481 | 257 | void synchronize_rcu(void) |
2bc59bd7 | 258 | { |
135530fd MD |
259 | internal_urcu_lock(); |
260 | ||
9598a481 | 261 | /* All threads should read qparity before accessing data structure |
135530fd MD |
262 | * where new ptr points to. Must be done within internal_urcu_lock |
263 | * because it iterates on reader threads.*/ | |
9598a481 | 264 | /* Write new ptr before changing the qparity */ |
2bc59bd7 | 265 | force_mb_all_threads(); |
9598a481 | 266 | |
ae62b5e8 MD |
267 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
268 | ||
9598a481 | 269 | switch_next_urcu_qparity(); /* 0 -> 1 */ |
2bc59bd7 PM |
270 | |
271 | /* | |
9598a481 MD |
272 | * Must commit qparity update to memory before waiting for parity |
273 | * 0 quiescent state. Failure to do so could result in the writer | |
274 | * waiting forever while new readers are always accessing data (no | |
275 | * progress). | |
b0d5e790 | 276 | * Ensured by STORE_SHARED and LOAD_SHARED. |
2bc59bd7 | 277 | */ |
2bc59bd7 | 278 | |
67c2d80b | 279 | /* |
5dba80f9 MD |
280 | * Adding a smp_mb() which is _not_ formally required, but makes the |
281 | * model easier to understand. It does not have a big performance impact | |
282 | * anyway, given this is the write-side. | |
67c2d80b | 283 | */ |
5dba80f9 | 284 | smp_mb(); |
67c2d80b | 285 | |
9598a481 MD |
286 | /* |
287 | * Wait for previous parity to be empty of readers. | |
288 | */ | |
289 | wait_for_quiescent_state(); /* Wait readers in parity 0 */ | |
9598a481 MD |
290 | |
291 | /* | |
292 | * Must finish waiting for quiescent state for parity 0 before | |
293 | * committing qparity update to memory. Failure to do so could result in | |
294 | * the writer waiting forever while new readers are always accessing | |
295 | * data (no progress). | |
b0d5e790 | 296 | * Ensured by STORE_SHARED and LOAD_SHARED. |
9598a481 | 297 | */ |
9598a481 | 298 | |
5dba80f9 MD |
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(); | |
67c2d80b | 305 | |
9598a481 | 306 | switch_next_urcu_qparity(); /* 1 -> 0 */ |
9598a481 MD |
307 | |
308 | /* | |
309 | * Must commit qparity update to memory before waiting for parity | |
310 | * 1 quiescent state. Failure to do so could result in the writer | |
311 | * waiting forever while new readers are always accessing data (no | |
312 | * progress). | |
b0d5e790 | 313 | * Ensured by STORE_SHARED and LOAD_SHARED. |
9598a481 | 314 | */ |
9598a481 | 315 | |
5dba80f9 MD |
316 | /* |
317 | * Adding a smp_mb() which is _not_ formally required, but makes the | |
318 | * model easier to understand. It does not have a big performance impact | |
319 | * anyway, given this is the write-side. | |
320 | */ | |
321 | smp_mb(); | |
67c2d80b | 322 | |
9598a481 MD |
323 | /* |
324 | * Wait for previous parity to be empty of readers. | |
325 | */ | |
326 | wait_for_quiescent_state(); /* Wait readers in parity 1 */ | |
9598a481 | 327 | |
ae62b5e8 MD |
328 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
329 | ||
9598a481 | 330 | /* Finish waiting for reader threads before letting the old ptr being |
135530fd MD |
331 | * freed. Must be done within internal_urcu_lock because it iterates on |
332 | * reader threads. */ | |
9598a481 | 333 | force_mb_all_threads(); |
135530fd MD |
334 | |
335 | internal_urcu_unlock(); | |
2bc59bd7 PM |
336 | } |
337 | ||
121a5d44 MD |
338 | /* |
339 | * library wrappers to be used by non-LGPL compatible source code. | |
340 | */ | |
341 | ||
342 | void rcu_read_lock(void) | |
343 | { | |
344 | _rcu_read_lock(); | |
345 | } | |
346 | ||
347 | void rcu_read_unlock(void) | |
348 | { | |
349 | _rcu_read_unlock(); | |
350 | } | |
351 | ||
352 | void *rcu_dereference(void *p) | |
353 | { | |
354 | return _rcu_dereference(p); | |
355 | } | |
356 | ||
357 | void *rcu_assign_pointer_sym(void **p, void *v) | |
358 | { | |
359 | wmb(); | |
360 | return STORE_SHARED(p, v); | |
361 | } | |
362 | ||
363 | void *rcu_xchg_pointer_sym(void **p, void *v) | |
364 | { | |
365 | wmb(); | |
366 | return xchg(p, v); | |
367 | } | |
368 | ||
4d1ce26f MD |
369 | void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new) |
370 | { | |
371 | wmb(); | |
372 | return cmpxchg(p, old, _new); | |
373 | } | |
374 | ||
121a5d44 MD |
375 | void *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 | ||
384 | static void rcu_add_reader(pthread_t id) | |
27b012e2 | 385 | { |
0a52082b | 386 | struct reader_registry *oldarray; |
f69f195a | 387 | |
0a52082b | 388 | if (!registry) { |
27b012e2 | 389 | alloc_readers = INIT_NUM_THREADS; |
f69f195a | 390 | num_readers = 0; |
0a52082b MD |
391 | registry = |
392 | malloc(sizeof(struct reader_registry) * alloc_readers); | |
27b012e2 MD |
393 | } |
394 | if (alloc_readers < num_readers + 1) { | |
0a52082b MD |
395 | oldarray = registry; |
396 | registry = malloc(sizeof(struct reader_registry) | |
27b012e2 | 397 | * (alloc_readers << 1)); |
0a52082b MD |
398 | memcpy(registry, oldarray, |
399 | sizeof(struct reader_registry) * alloc_readers); | |
27b012e2 MD |
400 | alloc_readers <<= 1; |
401 | free(oldarray); | |
402 | } | |
0a52082b | 403 | registry[num_readers].tid = id; |
27b012e2 | 404 | /* reference to the TLS of _this_ reader thread. */ |
8b25e300 | 405 | registry[num_readers].urcu_reader_status = &urcu_reader_status; |
09a9f986 | 406 | registry[num_readers].need_mb = &need_mb; |
27b012e2 MD |
407 | num_readers++; |
408 | } | |
409 | ||
410 | /* | |
411 | * Never shrink (implementation limitation). | |
412 | * This is O(nb threads). Eventually use a hash table. | |
413 | */ | |
121a5d44 | 414 | static void rcu_remove_reader(pthread_t id) |
27b012e2 | 415 | { |
0a52082b | 416 | struct reader_registry *index; |
27b012e2 | 417 | |
0a52082b MD |
418 | assert(registry != NULL); |
419 | for (index = registry; index < registry + num_readers; index++) { | |
e6d6e2dc | 420 | if (pthread_equal(index->tid, id)) { |
0a52082b MD |
421 | memcpy(index, ®istry[num_readers - 1], |
422 | sizeof(struct reader_registry)); | |
423 | registry[num_readers - 1].tid = 0; | |
8b25e300 | 424 | registry[num_readers - 1].urcu_reader_status = NULL; |
27b012e2 MD |
425 | num_readers--; |
426 | return; | |
427 | } | |
428 | } | |
429 | /* Hrm not found, forgot to register ? */ | |
430 | assert(0); | |
431 | } | |
432 | ||
121a5d44 | 433 | void rcu_register_thread(void) |
27b012e2 | 434 | { |
c265818b | 435 | internal_urcu_lock(); |
8a5fb4c9 | 436 | urcu_init(); /* In case gcc does not support constructor attribute */ |
121a5d44 | 437 | rcu_add_reader(pthread_self()); |
c265818b | 438 | internal_urcu_unlock(); |
27b012e2 MD |
439 | } |
440 | ||
121a5d44 | 441 | void rcu_unregister_thread(void) |
27b012e2 | 442 | { |
c265818b | 443 | internal_urcu_lock(); |
121a5d44 | 444 | rcu_remove_reader(pthread_self()); |
c265818b | 445 | internal_urcu_unlock(); |
27b012e2 MD |
446 | } |
447 | ||
0a1d290b | 448 | #ifndef URCU_MB |
121a5d44 | 449 | static void sigurcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 | 450 | { |
40e140c9 MD |
451 | /* |
452 | * Executing this smp_mb() is the only purpose of this signal handler. | |
453 | * It punctually promotes barrier() into smp_mb() on every thread it is | |
454 | * executed on. | |
455 | */ | |
b715b99e | 456 | smp_mb(); |
09a9f986 PM |
457 | need_mb = 0; |
458 | smp_mb(); | |
27b012e2 MD |
459 | } |
460 | ||
8a5fb4c9 MD |
461 | /* |
462 | * urcu_init constructor. Called when the library is linked, but also when | |
463 | * reader threads are calling rcu_register_thread(). | |
464 | * Should only be called by a single thread at a given time. This is ensured by | |
465 | * holing the internal_urcu_lock() from rcu_register_thread() or by running at | |
466 | * library load time, which should not be executed by multiple threads nor | |
467 | * concurrently with rcu_register_thread() anyway. | |
468 | */ | |
469 | void urcu_init(void) | |
27b012e2 MD |
470 | { |
471 | struct sigaction act; | |
472 | int ret; | |
473 | ||
8a5fb4c9 MD |
474 | if (init_done) |
475 | return; | |
476 | init_done = 1; | |
477 | ||
27b012e2 | 478 | act.sa_sigaction = sigurcu_handler; |
dd052bd3 | 479 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
c297c21c | 480 | sigemptyset(&act.sa_mask); |
27b012e2 | 481 | ret = sigaction(SIGURCU, &act, NULL); |
f69f195a MD |
482 | if (ret) { |
483 | perror("Error in sigaction"); | |
27b012e2 MD |
484 | exit(-1); |
485 | } | |
486 | } | |
487 | ||
8a5fb4c9 | 488 | void urcu_exit(void) |
27b012e2 MD |
489 | { |
490 | struct sigaction act; | |
491 | int ret; | |
492 | ||
493 | ret = sigaction(SIGURCU, NULL, &act); | |
f69f195a MD |
494 | if (ret) { |
495 | perror("Error in sigaction"); | |
27b012e2 MD |
496 | exit(-1); |
497 | } | |
498 | assert(act.sa_sigaction == sigurcu_handler); | |
0a52082b | 499 | free(registry); |
27b012e2 | 500 | } |
0a1d290b | 501 | #endif /* #ifndef URCU_MB */ |