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 | ||
8a5fb4c9 MD |
39 | void __attribute__((constructor)) urcu_init(void); |
40 | void __attribute__((destructor)) urcu_exit(void); | |
41 | ||
42 | int init_done; | |
43 | ||
27b012e2 MD |
44 | pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
45 | ||
128166c9 MD |
46 | /* |
47 | * Global grace period counter. | |
48 | * Contains the current RCU_GP_CTR_BIT. | |
49 | * Also has a RCU_GP_CTR_BIT of 1, to accelerate the reader fast path. | |
b0d5e790 | 50 | * Written to only by writer with mutex taken. Read by both writer and readers. |
128166c9 MD |
51 | */ |
52 | long urcu_gp_ctr = RCU_GP_COUNT; | |
27b012e2 | 53 | |
b0d5e790 MD |
54 | /* |
55 | * Written to only by each individual reader. Read by both the reader and the | |
56 | * writers. | |
57 | */ | |
6e8b8429 | 58 | long __thread urcu_active_readers; |
27b012e2 MD |
59 | |
60 | /* Thread IDs of registered readers */ | |
61 | #define INIT_NUM_THREADS 4 | |
62 | ||
0a52082b | 63 | struct reader_registry { |
27b012e2 | 64 | pthread_t tid; |
128166c9 | 65 | long *urcu_active_readers; |
09a9f986 | 66 | char *need_mb; |
27b012e2 MD |
67 | }; |
68 | ||
cf380c2f | 69 | #ifdef DEBUG_YIELD |
9d335088 MD |
70 | unsigned int yield_active; |
71 | unsigned int __thread rand_yield; | |
cf380c2f MD |
72 | #endif |
73 | ||
0a52082b | 74 | static struct reader_registry *registry; |
09a9f986 | 75 | static char __thread need_mb; |
27b012e2 | 76 | static int num_readers, alloc_readers; |
27b012e2 | 77 | |
c265818b | 78 | void internal_urcu_lock(void) |
41718ff9 MD |
79 | { |
80 | int ret; | |
09a9f986 PM |
81 | |
82 | #ifndef DISTRUST_SIGNALS_EXTREME | |
41718ff9 MD |
83 | ret = pthread_mutex_lock(&urcu_mutex); |
84 | if (ret) { | |
85 | perror("Error in pthread mutex lock"); | |
86 | exit(-1); | |
87 | } | |
09a9f986 PM |
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 (need_mb) { | |
96 | smp_mb(); | |
97 | need_mb = 0; | |
98 | smp_mb(); | |
99 | } | |
100 | poll(NULL,0,10); | |
101 | } | |
102 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
41718ff9 MD |
103 | } |
104 | ||
c265818b | 105 | void internal_urcu_unlock(void) |
41718ff9 MD |
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 | ||
27b012e2 MD |
116 | /* |
117 | * called with urcu_mutex held. | |
118 | */ | |
1430ee0b | 119 | static void switch_next_urcu_qparity(void) |
27b012e2 | 120 | { |
b0d5e790 | 121 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR_BIT); |
27b012e2 MD |
122 | } |
123 | ||
bb488185 | 124 | #ifdef DEBUG_FULL_MB |
e8043c1b | 125 | #ifdef HAS_INCOHERENT_CACHES |
09a9f986 | 126 | static void force_mb_single_thread(struct reader_registry *index) |
40e140c9 MD |
127 | { |
128 | smp_mb(); | |
129 | } | |
e8043c1b | 130 | #endif /* #ifdef HAS_INCOHERENT_CACHES */ |
40e140c9 | 131 | |
bb488185 MD |
132 | static void force_mb_all_threads(void) |
133 | { | |
b715b99e | 134 | smp_mb(); |
bb488185 | 135 | } |
e8043c1b MD |
136 | #else /* #ifdef DEBUG_FULL_MB */ |
137 | #ifdef HAS_INCOHERENT_CACHES | |
09a9f986 | 138 | static void force_mb_single_thread(struct reader_registry *index) |
40e140c9 | 139 | { |
0a52082b | 140 | assert(registry); |
157dca95 MD |
141 | /* |
142 | * pthread_kill has a smp_mb(). But beware, we assume it performs | |
143 | * a cache flush on architectures with non-coherent cache. Let's play | |
144 | * safe and don't assume anything : we use smp_mc() to make sure the | |
145 | * cache flush is enforced. | |
157dca95 | 146 | */ |
09a9f986 PM |
147 | *index->need_mb = 1; |
148 | smp_mc(); /* write ->need_mb before sending the signals */ | |
149 | pthread_kill(index->tid, SIGURCU); | |
150 | smp_mb(); | |
40e140c9 MD |
151 | /* |
152 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
153 | * BUSY-LOOP. | |
154 | */ | |
09a9f986 PM |
155 | while (*index->need_mb) { |
156 | poll(NULL, 0, 1); | |
157 | } | |
158 | smp_mb(); /* read ->need_mb before ending the barrier */ | |
40e140c9 | 159 | } |
e8043c1b | 160 | #endif /* #ifdef HAS_INCOHERENT_CACHES */ |
40e140c9 | 161 | |
27b012e2 MD |
162 | static void force_mb_all_threads(void) |
163 | { | |
0a52082b | 164 | struct reader_registry *index; |
27b012e2 | 165 | /* |
b715b99e | 166 | * Ask for each threads to execute a smp_mb() so we can consider the |
27b012e2 MD |
167 | * compiler barriers around rcu read lock as real memory barriers. |
168 | */ | |
0a52082b | 169 | if (!registry) |
27b012e2 | 170 | return; |
3a86deba MD |
171 | /* |
172 | * pthread_kill has a smp_mb(). But beware, we assume it performs | |
157dca95 MD |
173 | * a cache flush on architectures with non-coherent cache. Let's play |
174 | * safe and don't assume anything : we use smp_mc() to make sure the | |
175 | * cache flush is enforced. | |
3a86deba | 176 | */ |
09a9f986 PM |
177 | for (index = registry; index < registry + num_readers; index++) { |
178 | *index->need_mb = 1; | |
179 | smp_mc(); /* write need_mb before sending the signal */ | |
f69f195a | 180 | pthread_kill(index->tid, SIGURCU); |
09a9f986 | 181 | } |
27b012e2 MD |
182 | /* |
183 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
09a9f986 PM |
184 | * |
185 | * Note that the pthread_kill() will never be executed on systems | |
186 | * that correctly deliver signals in a timely manner. However, it | |
187 | * is not uncommon for kernels to have bugs that can result in | |
188 | * lost or unduly delayed signals. | |
189 | * | |
190 | * If you are seeing the below pthread_kill() executing much at | |
191 | * all, we suggest testing the underlying kernel and filing the | |
192 | * relevant bug report. For Linux kernels, we recommend getting | |
193 | * the Linux Test Project (LTP). | |
27b012e2 | 194 | */ |
09a9f986 PM |
195 | for (index = registry; index < registry + num_readers; index++) { |
196 | while (*index->need_mb) { | |
197 | pthread_kill(index->tid, SIGURCU); | |
198 | poll(NULL, 0, 1); | |
199 | } | |
200 | } | |
201 | smp_mb(); /* read ->need_mb before ending the barrier */ | |
27b012e2 | 202 | } |
e8043c1b | 203 | #endif /* #else #ifdef DEBUG_FULL_MB */ |
27b012e2 | 204 | |
1430ee0b | 205 | void wait_for_quiescent_state(void) |
27b012e2 | 206 | { |
0a52082b | 207 | struct reader_registry *index; |
27b012e2 | 208 | |
0a52082b | 209 | if (!registry) |
27b012e2 | 210 | return; |
40e140c9 MD |
211 | /* |
212 | * Wait for each thread urcu_active_readers count to become 0. | |
27b012e2 | 213 | */ |
0a52082b | 214 | for (index = registry; index < registry + num_readers; index++) { |
e8043c1b MD |
215 | #ifndef HAS_INCOHERENT_CACHES |
216 | while (rcu_old_gp_ongoing(index->urcu_active_readers)) | |
217 | cpu_relax(); | |
218 | #else /* #ifndef HAS_INCOHERENT_CACHES */ | |
40e140c9 | 219 | int wait_loops = 0; |
27b012e2 | 220 | /* |
40e140c9 MD |
221 | * BUSY-LOOP. Force the reader thread to commit its |
222 | * urcu_active_readers update to memory if we wait for too long. | |
27b012e2 | 223 | */ |
40e140c9 MD |
224 | while (rcu_old_gp_ongoing(index->urcu_active_readers)) { |
225 | if (wait_loops++ == KICK_READER_LOOPS) { | |
09a9f986 | 226 | force_mb_single_thread(index); |
40e140c9 | 227 | wait_loops = 0; |
3b55dbf4 MD |
228 | } else { |
229 | cpu_relax(); | |
40e140c9 MD |
230 | } |
231 | } | |
e8043c1b | 232 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 233 | } |
27b012e2 MD |
234 | } |
235 | ||
9598a481 | 236 | void synchronize_rcu(void) |
2bc59bd7 | 237 | { |
135530fd MD |
238 | internal_urcu_lock(); |
239 | ||
9598a481 | 240 | /* All threads should read qparity before accessing data structure |
135530fd MD |
241 | * where new ptr points to. Must be done within internal_urcu_lock |
242 | * because it iterates on reader threads.*/ | |
9598a481 | 243 | /* Write new ptr before changing the qparity */ |
2bc59bd7 | 244 | force_mb_all_threads(); |
9598a481 | 245 | |
9598a481 | 246 | switch_next_urcu_qparity(); /* 0 -> 1 */ |
2bc59bd7 PM |
247 | |
248 | /* | |
9598a481 MD |
249 | * Must commit qparity update to memory before waiting for parity |
250 | * 0 quiescent state. Failure to do so could result in the writer | |
251 | * waiting forever while new readers are always accessing data (no | |
252 | * progress). | |
b0d5e790 | 253 | * Ensured by STORE_SHARED and LOAD_SHARED. |
2bc59bd7 | 254 | */ |
2bc59bd7 | 255 | |
67c2d80b MD |
256 | /* |
257 | * Current RCU formal verification model assumes sequential execution of | |
258 | * the write-side. Add core synchronization instructions. Can be removed | |
259 | * if the formal model is extended to prove that reordering is still | |
260 | * correct. | |
261 | */ | |
262 | sync_core(); /* Formal model assumes serialized execution */ | |
263 | ||
9598a481 MD |
264 | /* |
265 | * Wait for previous parity to be empty of readers. | |
266 | */ | |
267 | wait_for_quiescent_state(); /* Wait readers in parity 0 */ | |
9598a481 MD |
268 | |
269 | /* | |
270 | * Must finish waiting for quiescent state for parity 0 before | |
271 | * committing qparity update to memory. Failure to do so could result in | |
272 | * the writer waiting forever while new readers are always accessing | |
273 | * data (no progress). | |
b0d5e790 | 274 | * Ensured by STORE_SHARED and LOAD_SHARED. |
9598a481 | 275 | */ |
9598a481 | 276 | |
67c2d80b MD |
277 | sync_core(); /* Formal model assumes serialized execution */ |
278 | ||
9598a481 | 279 | switch_next_urcu_qparity(); /* 1 -> 0 */ |
9598a481 MD |
280 | |
281 | /* | |
282 | * Must commit qparity update to memory before waiting for parity | |
283 | * 1 quiescent state. Failure to do so could result in the writer | |
284 | * waiting forever while new readers are always accessing data (no | |
285 | * progress). | |
b0d5e790 | 286 | * Ensured by STORE_SHARED and LOAD_SHARED. |
9598a481 | 287 | */ |
9598a481 | 288 | |
67c2d80b MD |
289 | sync_core(); /* Formal model assumes serialized execution */ |
290 | ||
9598a481 MD |
291 | /* |
292 | * Wait for previous parity to be empty of readers. | |
293 | */ | |
294 | wait_for_quiescent_state(); /* Wait readers in parity 1 */ | |
9598a481 | 295 | |
9598a481 | 296 | /* Finish waiting for reader threads before letting the old ptr being |
135530fd MD |
297 | * freed. Must be done within internal_urcu_lock because it iterates on |
298 | * reader threads. */ | |
9598a481 | 299 | force_mb_all_threads(); |
135530fd MD |
300 | |
301 | internal_urcu_unlock(); | |
2bc59bd7 PM |
302 | } |
303 | ||
121a5d44 MD |
304 | /* |
305 | * library wrappers to be used by non-LGPL compatible source code. | |
306 | */ | |
307 | ||
308 | void rcu_read_lock(void) | |
309 | { | |
310 | _rcu_read_lock(); | |
311 | } | |
312 | ||
313 | void rcu_read_unlock(void) | |
314 | { | |
315 | _rcu_read_unlock(); | |
316 | } | |
317 | ||
318 | void *rcu_dereference(void *p) | |
319 | { | |
320 | return _rcu_dereference(p); | |
321 | } | |
322 | ||
323 | void *rcu_assign_pointer_sym(void **p, void *v) | |
324 | { | |
325 | wmb(); | |
326 | return STORE_SHARED(p, v); | |
327 | } | |
328 | ||
329 | void *rcu_xchg_pointer_sym(void **p, void *v) | |
330 | { | |
331 | wmb(); | |
332 | return xchg(p, v); | |
333 | } | |
334 | ||
335 | void *rcu_publish_content_sym(void **p, void *v) | |
336 | { | |
337 | void *oldptr; | |
338 | ||
339 | oldptr = _rcu_xchg_pointer(p, v); | |
340 | synchronize_rcu(); | |
341 | return oldptr; | |
342 | } | |
343 | ||
344 | static void rcu_add_reader(pthread_t id) | |
27b012e2 | 345 | { |
0a52082b | 346 | struct reader_registry *oldarray; |
f69f195a | 347 | |
0a52082b | 348 | if (!registry) { |
27b012e2 | 349 | alloc_readers = INIT_NUM_THREADS; |
f69f195a | 350 | num_readers = 0; |
0a52082b MD |
351 | registry = |
352 | malloc(sizeof(struct reader_registry) * alloc_readers); | |
27b012e2 MD |
353 | } |
354 | if (alloc_readers < num_readers + 1) { | |
0a52082b MD |
355 | oldarray = registry; |
356 | registry = malloc(sizeof(struct reader_registry) | |
27b012e2 | 357 | * (alloc_readers << 1)); |
0a52082b MD |
358 | memcpy(registry, oldarray, |
359 | sizeof(struct reader_registry) * alloc_readers); | |
27b012e2 MD |
360 | alloc_readers <<= 1; |
361 | free(oldarray); | |
362 | } | |
0a52082b | 363 | registry[num_readers].tid = id; |
27b012e2 | 364 | /* reference to the TLS of _this_ reader thread. */ |
0a52082b | 365 | registry[num_readers].urcu_active_readers = &urcu_active_readers; |
09a9f986 | 366 | registry[num_readers].need_mb = &need_mb; |
27b012e2 MD |
367 | num_readers++; |
368 | } | |
369 | ||
370 | /* | |
371 | * Never shrink (implementation limitation). | |
372 | * This is O(nb threads). Eventually use a hash table. | |
373 | */ | |
121a5d44 | 374 | static void rcu_remove_reader(pthread_t id) |
27b012e2 | 375 | { |
0a52082b | 376 | struct reader_registry *index; |
27b012e2 | 377 | |
0a52082b MD |
378 | assert(registry != NULL); |
379 | for (index = registry; index < registry + num_readers; index++) { | |
e6d6e2dc | 380 | if (pthread_equal(index->tid, id)) { |
0a52082b MD |
381 | memcpy(index, ®istry[num_readers - 1], |
382 | sizeof(struct reader_registry)); | |
383 | registry[num_readers - 1].tid = 0; | |
384 | registry[num_readers - 1].urcu_active_readers = NULL; | |
27b012e2 MD |
385 | num_readers--; |
386 | return; | |
387 | } | |
388 | } | |
389 | /* Hrm not found, forgot to register ? */ | |
390 | assert(0); | |
391 | } | |
392 | ||
121a5d44 | 393 | void rcu_register_thread(void) |
27b012e2 | 394 | { |
c265818b | 395 | internal_urcu_lock(); |
8a5fb4c9 | 396 | urcu_init(); /* In case gcc does not support constructor attribute */ |
121a5d44 | 397 | rcu_add_reader(pthread_self()); |
c265818b | 398 | internal_urcu_unlock(); |
27b012e2 MD |
399 | } |
400 | ||
121a5d44 | 401 | void rcu_unregister_thread(void) |
27b012e2 | 402 | { |
c265818b | 403 | internal_urcu_lock(); |
121a5d44 | 404 | rcu_remove_reader(pthread_self()); |
c265818b | 405 | internal_urcu_unlock(); |
27b012e2 MD |
406 | } |
407 | ||
bb488185 | 408 | #ifndef DEBUG_FULL_MB |
121a5d44 | 409 | static void sigurcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 | 410 | { |
40e140c9 MD |
411 | /* |
412 | * Executing this smp_mb() is the only purpose of this signal handler. | |
413 | * It punctually promotes barrier() into smp_mb() on every thread it is | |
414 | * executed on. | |
415 | */ | |
b715b99e | 416 | smp_mb(); |
09a9f986 PM |
417 | need_mb = 0; |
418 | smp_mb(); | |
27b012e2 MD |
419 | } |
420 | ||
8a5fb4c9 MD |
421 | /* |
422 | * urcu_init constructor. Called when the library is linked, but also when | |
423 | * reader threads are calling rcu_register_thread(). | |
424 | * Should only be called by a single thread at a given time. This is ensured by | |
425 | * holing the internal_urcu_lock() from rcu_register_thread() or by running at | |
426 | * library load time, which should not be executed by multiple threads nor | |
427 | * concurrently with rcu_register_thread() anyway. | |
428 | */ | |
429 | void urcu_init(void) | |
27b012e2 MD |
430 | { |
431 | struct sigaction act; | |
432 | int ret; | |
433 | ||
8a5fb4c9 MD |
434 | if (init_done) |
435 | return; | |
436 | init_done = 1; | |
437 | ||
27b012e2 MD |
438 | act.sa_sigaction = sigurcu_handler; |
439 | ret = sigaction(SIGURCU, &act, NULL); | |
f69f195a MD |
440 | if (ret) { |
441 | perror("Error in sigaction"); | |
27b012e2 MD |
442 | exit(-1); |
443 | } | |
444 | } | |
445 | ||
8a5fb4c9 | 446 | void urcu_exit(void) |
27b012e2 MD |
447 | { |
448 | struct sigaction act; | |
449 | int ret; | |
450 | ||
451 | ret = sigaction(SIGURCU, NULL, &act); | |
f69f195a MD |
452 | if (ret) { |
453 | perror("Error in sigaction"); | |
27b012e2 MD |
454 | exit(-1); |
455 | } | |
456 | assert(act.sa_sigaction == sigurcu_handler); | |
0a52082b | 457 | free(registry); |
27b012e2 | 458 | } |
e8043c1b | 459 | #endif /* #ifndef DEBUG_FULL_MB */ |