Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * urcu.c | |
3 | * | |
4 | * Userspace RCU library | |
5 | * | |
6982d6d7 | 6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
af02d47e | 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 | ||
fdf01eed | 26 | #define _BSD_SOURCE |
c1d2c60b | 27 | #define _GNU_SOURCE |
71c811bf | 28 | #define _LGPL_SOURCE |
27b012e2 MD |
29 | #include <stdio.h> |
30 | #include <pthread.h> | |
31 | #include <signal.h> | |
32 | #include <assert.h> | |
f69f195a | 33 | #include <stdlib.h> |
6d841bc2 | 34 | #include <stdint.h> |
f69f195a | 35 | #include <string.h> |
09a9f986 | 36 | #include <errno.h> |
e8043c1b | 37 | #include <poll.h> |
27b012e2 | 38 | |
71c811bf | 39 | #include "urcu/wfqueue.h" |
57760d44 | 40 | #include "urcu/map/urcu.h" |
af7c2dbe | 41 | #include "urcu/static/urcu.h" |
618b2595 | 42 | #include "urcu-pointer.h" |
71c811bf | 43 | |
121a5d44 | 44 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
71c811bf | 45 | #undef _LGPL_SOURCE |
27b012e2 | 46 | #include "urcu.h" |
71c811bf | 47 | #define _LGPL_SOURCE |
27b012e2 | 48 | |
3a71751e PB |
49 | /* |
50 | * If a reader is really non-cooperative and refuses to commit its | |
51 | * rcu_active_readers count to memory (there is no barrier in the reader | |
52 | * per-se), kick it after a few loops waiting for it. | |
53 | */ | |
54 | #define KICK_READER_LOOPS 10000 | |
55 | ||
56 | /* | |
57 | * Active attempts to check for reader Q.S. before calling futex(). | |
58 | */ | |
59 | #define RCU_QS_ACTIVE_ATTEMPTS 100 | |
60 | ||
fdf01eed | 61 | #ifdef RCU_MEMBARRIER |
834a45ba | 62 | static int init_done; |
fdf01eed | 63 | int has_sys_membarrier; |
834a45ba | 64 | |
02be5561 | 65 | void __attribute__((constructor)) rcu_init(void); |
fdf01eed MD |
66 | #endif |
67 | ||
68 | #ifdef RCU_MB | |
02be5561 | 69 | void rcu_init(void) |
e90a6e9c MD |
70 | { |
71 | } | |
72 | #endif | |
8a5fb4c9 | 73 | |
fdf01eed MD |
74 | #ifdef RCU_SIGNAL |
75 | static int init_done; | |
76 | ||
77 | void __attribute__((constructor)) rcu_init(void); | |
78 | void __attribute__((destructor)) rcu_exit(void); | |
79 | #endif | |
80 | ||
6abb4bd5 | 81 | static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; |
27b012e2 | 82 | |
6d841bc2 | 83 | int32_t gp_futex; |
bc6c15bb | 84 | |
128166c9 MD |
85 | /* |
86 | * Global grace period counter. | |
02be5561 | 87 | * Contains the current RCU_GP_CTR_PHASE. |
afb8f2c9 | 88 | * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path. |
b0d5e790 | 89 | * Written to only by writer with mutex taken. Read by both writer and readers. |
128166c9 | 90 | */ |
27d65bc5 | 91 | unsigned long rcu_gp_ctr = RCU_GP_COUNT; |
27b012e2 | 92 | |
b0d5e790 MD |
93 | /* |
94 | * Written to only by each individual reader. Read by both the reader and the | |
95 | * writers. | |
96 | */ | |
02be5561 | 97 | struct rcu_reader __thread rcu_reader; |
27b012e2 | 98 | |
cf380c2f | 99 | #ifdef DEBUG_YIELD |
9d335088 MD |
100 | unsigned int yield_active; |
101 | unsigned int __thread rand_yield; | |
cf380c2f MD |
102 | #endif |
103 | ||
16aa9ee8 | 104 | static CDS_LIST_HEAD(registry); |
27b012e2 | 105 | |
6abb4bd5 | 106 | static void mutex_lock(pthread_mutex_t *mutex) |
41718ff9 MD |
107 | { |
108 | int ret; | |
09a9f986 PM |
109 | |
110 | #ifndef DISTRUST_SIGNALS_EXTREME | |
6abb4bd5 | 111 | ret = pthread_mutex_lock(mutex); |
41718ff9 MD |
112 | if (ret) { |
113 | perror("Error in pthread mutex lock"); | |
114 | exit(-1); | |
115 | } | |
09a9f986 | 116 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
6abb4bd5 | 117 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
09a9f986 PM |
118 | if (ret != EBUSY && ret != EINTR) { |
119 | printf("ret = %d, errno = %d\n", ret, errno); | |
120 | perror("Error in pthread mutex lock"); | |
121 | exit(-1); | |
122 | } | |
6cf3827c | 123 | if (CMM_LOAD_SHARED(rcu_reader.need_mb)) { |
5481ddb3 | 124 | cmm_smp_mb(); |
6cf3827c | 125 | _CMM_STORE_SHARED(rcu_reader.need_mb, 0); |
5481ddb3 | 126 | cmm_smp_mb(); |
09a9f986 PM |
127 | } |
128 | poll(NULL,0,10); | |
129 | } | |
130 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
41718ff9 MD |
131 | } |
132 | ||
6abb4bd5 | 133 | static void mutex_unlock(pthread_mutex_t *mutex) |
41718ff9 MD |
134 | { |
135 | int ret; | |
136 | ||
6abb4bd5 | 137 | ret = pthread_mutex_unlock(mutex); |
41718ff9 MD |
138 | if (ret) { |
139 | perror("Error in pthread mutex unlock"); | |
140 | exit(-1); | |
141 | } | |
142 | } | |
143 | ||
fdf01eed | 144 | #ifdef RCU_MEMBARRIER |
25cc6d18 | 145 | static void smp_mb_master(int group) |
fdf01eed | 146 | { |
a0b7f7ea | 147 | if (caa_likely(has_sys_membarrier)) |
f0708810 | 148 | membarrier(MEMBARRIER_EXPEDITED); |
fdf01eed | 149 | else |
5481ddb3 | 150 | cmm_smp_mb(); |
fdf01eed MD |
151 | } |
152 | #endif | |
153 | ||
02be5561 | 154 | #ifdef RCU_MB |
25cc6d18 | 155 | static void smp_mb_master(int group) |
40e140c9 | 156 | { |
5481ddb3 | 157 | cmm_smp_mb(); |
40e140c9 | 158 | } |
fdf01eed MD |
159 | #endif |
160 | ||
161 | #ifdef RCU_SIGNAL | |
78ff9419 | 162 | static void force_mb_all_readers(void) |
27b012e2 | 163 | { |
02be5561 | 164 | struct rcu_reader *index; |
e3b0cef0 | 165 | |
27b012e2 | 166 | /* |
5481ddb3 | 167 | * Ask for each threads to execute a cmm_smp_mb() so we can consider the |
27b012e2 MD |
168 | * compiler barriers around rcu read lock as real memory barriers. |
169 | */ | |
16aa9ee8 | 170 | if (cds_list_empty(®istry)) |
27b012e2 | 171 | return; |
3a86deba | 172 | /* |
5481ddb3 | 173 | * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs |
157dca95 | 174 | * a cache flush on architectures with non-coherent cache. Let's play |
5481ddb3 | 175 | * safe and don't assume anything : we use cmm_smp_mc() to make sure the |
157dca95 | 176 | * cache flush is enforced. |
3a86deba | 177 | */ |
16aa9ee8 | 178 | cds_list_for_each_entry(index, ®istry, node) { |
6cf3827c | 179 | CMM_STORE_SHARED(index->need_mb, 1); |
02be5561 | 180 | pthread_kill(index->tid, SIGRCU); |
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 | */ |
16aa9ee8 | 195 | cds_list_for_each_entry(index, ®istry, node) { |
6cf3827c | 196 | while (CMM_LOAD_SHARED(index->need_mb)) { |
02be5561 | 197 | pthread_kill(index->tid, SIGRCU); |
09a9f986 PM |
198 | poll(NULL, 0, 1); |
199 | } | |
200 | } | |
5481ddb3 | 201 | cmm_smp_mb(); /* read ->need_mb before ending the barrier */ |
27b012e2 | 202 | } |
9d7e3f89 | 203 | |
25cc6d18 | 204 | static void smp_mb_master(int group) |
9d7e3f89 MD |
205 | { |
206 | force_mb_all_readers(); | |
207 | } | |
fdf01eed | 208 | #endif /* #ifdef RCU_SIGNAL */ |
27b012e2 | 209 | |
bc6c15bb MD |
210 | /* |
211 | * synchronize_rcu() waiting. Single thread. | |
212 | */ | |
cfe78e25 | 213 | static void wait_gp(void) |
bc6c15bb | 214 | { |
cfe78e25 | 215 | /* Read reader_gp before read futex */ |
25cc6d18 | 216 | smp_mb_master(RCU_MB_GROUP); |
cfe78e25 | 217 | if (uatomic_read(&gp_futex) == -1) |
0854ccff | 218 | futex_async(&gp_futex, FUTEX_WAIT, -1, |
cfe78e25 | 219 | NULL, NULL, 0); |
bc6c15bb MD |
220 | } |
221 | ||
2dfb8b5e | 222 | void update_counter_and_wait(void) |
27b012e2 | 223 | { |
16aa9ee8 | 224 | CDS_LIST_HEAD(qsreaders); |
cfe78e25 | 225 | int wait_loops = 0; |
02be5561 | 226 | struct rcu_reader *index, *tmp; |
27b012e2 | 227 | |
32c15e4e | 228 | /* Switch parity: 0 -> 1, 1 -> 0 */ |
6cf3827c | 229 | CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE); |
2dfb8b5e MD |
230 | |
231 | /* | |
d40fde2c MD |
232 | * Must commit rcu_gp_ctr update to memory before waiting for quiescent |
233 | * state. Failure to do so could result in the writer waiting forever | |
234 | * while new readers are always accessing data (no progress). Enforce | |
235 | * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr. | |
2dfb8b5e | 236 | */ |
5481ddb3 | 237 | cmm_barrier(); |
2dfb8b5e MD |
238 | |
239 | /* | |
935b11ff | 240 | * |
5481ddb3 | 241 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
2dfb8b5e MD |
242 | * model easier to understand. It does not have a big performance impact |
243 | * anyway, given this is the write-side. | |
244 | */ | |
5481ddb3 | 245 | cmm_smp_mb(); |
2dfb8b5e | 246 | |
40e140c9 | 247 | /* |
02be5561 | 248 | * Wait for each thread rcu_reader.ctr count to become 0. |
27b012e2 | 249 | */ |
cfe78e25 MD |
250 | for (;;) { |
251 | wait_loops++; | |
252 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
253 | uatomic_dec(&gp_futex); | |
254 | /* Write futex before read reader_gp */ | |
25cc6d18 | 255 | smp_mb_master(RCU_MB_GROUP); |
cfe78e25 MD |
256 | } |
257 | ||
16aa9ee8 | 258 | cds_list_for_each_entry_safe(index, tmp, ®istry, node) { |
b95a001f | 259 | if (!rcu_gp_ongoing(&index->ctr)) |
16aa9ee8 | 260 | cds_list_move(&index->node, &qsreaders); |
cfe78e25 MD |
261 | } |
262 | ||
e8043c1b | 263 | #ifndef HAS_INCOHERENT_CACHES |
16aa9ee8 | 264 | if (cds_list_empty(®istry)) { |
cfe78e25 MD |
265 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { |
266 | /* Read reader_gp before write futex */ | |
25cc6d18 | 267 | smp_mb_master(RCU_MB_GROUP); |
cfe78e25 | 268 | uatomic_set(&gp_futex, 0); |
bc6c15bb | 269 | } |
cfe78e25 MD |
270 | break; |
271 | } else { | |
272 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) | |
273 | wait_gp(); | |
274 | else | |
06f22bdb | 275 | caa_cpu_relax(); |
bc6c15bb | 276 | } |
e8043c1b | 277 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 278 | /* |
40e140c9 | 279 | * BUSY-LOOP. Force the reader thread to commit its |
02be5561 | 280 | * rcu_reader.ctr update to memory if we wait for too long. |
27b012e2 | 281 | */ |
16aa9ee8 | 282 | if (cds_list_empty(®istry)) { |
cfe78e25 MD |
283 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { |
284 | /* Read reader_gp before write futex */ | |
25cc6d18 | 285 | smp_mb_master(RCU_MB_GROUP); |
cfe78e25 MD |
286 | uatomic_set(&gp_futex, 0); |
287 | } | |
288 | break; | |
289 | } else { | |
290 | switch (wait_loops) { | |
bc6c15bb | 291 | case RCU_QS_ACTIVE_ATTEMPTS: |
cfe78e25 MD |
292 | wait_gp(); |
293 | break; /* only escape switch */ | |
bc6c15bb | 294 | case KICK_READER_LOOPS: |
25cc6d18 | 295 | smp_mb_master(RCU_MB_GROUP); |
40e140c9 | 296 | wait_loops = 0; |
cfe78e25 | 297 | break; /* only escape switch */ |
bc6c15bb | 298 | default: |
06f22bdb | 299 | caa_cpu_relax(); |
40e140c9 MD |
300 | } |
301 | } | |
e8043c1b | 302 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 303 | } |
cfe78e25 | 304 | /* put back the reader list in the registry */ |
16aa9ee8 | 305 | cds_list_splice(&qsreaders, ®istry); |
27b012e2 MD |
306 | } |
307 | ||
9598a481 | 308 | void synchronize_rcu(void) |
2bc59bd7 | 309 | { |
6abb4bd5 | 310 | mutex_lock(&rcu_gp_lock); |
135530fd | 311 | |
16aa9ee8 | 312 | if (cds_list_empty(®istry)) |
2dfb8b5e MD |
313 | goto out; |
314 | ||
9598a481 | 315 | /* All threads should read qparity before accessing data structure |
6abb4bd5 MD |
316 | * where new ptr points to. Must be done within rcu_gp_lock because it |
317 | * iterates on reader threads.*/ | |
9598a481 | 318 | /* Write new ptr before changing the qparity */ |
25cc6d18 | 319 | smp_mb_master(RCU_MB_GROUP); |
9598a481 | 320 | |
9598a481 MD |
321 | /* |
322 | * Wait for previous parity to be empty of readers. | |
323 | */ | |
2dfb8b5e | 324 | update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */ |
9598a481 MD |
325 | |
326 | /* | |
327 | * Must finish waiting for quiescent state for parity 0 before | |
d40fde2c MD |
328 | * committing next rcu_gp_ctr update to memory. Failure to do so could |
329 | * result in the writer waiting forever while new readers are always | |
330 | * accessing data (no progress). Enforce compiler-order of load | |
331 | * rcu_reader ctr before store to rcu_gp_ctr. | |
9598a481 | 332 | */ |
5481ddb3 | 333 | cmm_barrier(); |
9598a481 | 334 | |
5dba80f9 | 335 | /* |
5481ddb3 | 336 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
5dba80f9 MD |
337 | * model easier to understand. It does not have a big performance impact |
338 | * anyway, given this is the write-side. | |
339 | */ | |
5481ddb3 | 340 | cmm_smp_mb(); |
67c2d80b | 341 | |
9598a481 MD |
342 | /* |
343 | * Wait for previous parity to be empty of readers. | |
344 | */ | |
2dfb8b5e | 345 | update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */ |
9598a481 | 346 | |
9598a481 | 347 | /* Finish waiting for reader threads before letting the old ptr being |
6abb4bd5 MD |
348 | * freed. Must be done within rcu_gp_lock because it iterates on reader |
349 | * threads. */ | |
25cc6d18 | 350 | smp_mb_master(RCU_MB_GROUP); |
2dfb8b5e | 351 | out: |
6abb4bd5 | 352 | mutex_unlock(&rcu_gp_lock); |
2bc59bd7 PM |
353 | } |
354 | ||
121a5d44 MD |
355 | /* |
356 | * library wrappers to be used by non-LGPL compatible source code. | |
357 | */ | |
358 | ||
359 | void rcu_read_lock(void) | |
360 | { | |
361 | _rcu_read_lock(); | |
362 | } | |
363 | ||
364 | void rcu_read_unlock(void) | |
365 | { | |
366 | _rcu_read_unlock(); | |
367 | } | |
368 | ||
121a5d44 | 369 | void rcu_register_thread(void) |
27b012e2 | 370 | { |
02be5561 MD |
371 | rcu_reader.tid = pthread_self(); |
372 | assert(rcu_reader.need_mb == 0); | |
4b5be3be | 373 | assert(!(rcu_reader.ctr & RCU_GP_CTR_NEST_MASK)); |
02be5561 | 374 | |
6abb4bd5 | 375 | mutex_lock(&rcu_gp_lock); |
02be5561 | 376 | rcu_init(); /* In case gcc does not support constructor attribute */ |
16aa9ee8 | 377 | cds_list_add(&rcu_reader.node, ®istry); |
6abb4bd5 | 378 | mutex_unlock(&rcu_gp_lock); |
27b012e2 MD |
379 | } |
380 | ||
121a5d44 | 381 | void rcu_unregister_thread(void) |
27b012e2 | 382 | { |
6abb4bd5 | 383 | mutex_lock(&rcu_gp_lock); |
16aa9ee8 | 384 | cds_list_del(&rcu_reader.node); |
6abb4bd5 | 385 | mutex_unlock(&rcu_gp_lock); |
27b012e2 MD |
386 | } |
387 | ||
fdf01eed MD |
388 | #ifdef RCU_MEMBARRIER |
389 | void rcu_init(void) | |
390 | { | |
391 | if (init_done) | |
392 | return; | |
393 | init_done = 1; | |
cf5271ee | 394 | if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY)) |
fdf01eed MD |
395 | has_sys_membarrier = 1; |
396 | } | |
397 | #endif | |
398 | ||
399 | #ifdef RCU_SIGNAL | |
02be5561 | 400 | static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 | 401 | { |
40e140c9 | 402 | /* |
5481ddb3 DG |
403 | * Executing this cmm_smp_mb() is the only purpose of this signal handler. |
404 | * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is | |
40e140c9 MD |
405 | * executed on. |
406 | */ | |
5481ddb3 | 407 | cmm_smp_mb(); |
6cf3827c | 408 | _CMM_STORE_SHARED(rcu_reader.need_mb, 0); |
5481ddb3 | 409 | cmm_smp_mb(); |
27b012e2 MD |
410 | } |
411 | ||
8a5fb4c9 | 412 | /* |
02be5561 | 413 | * rcu_init constructor. Called when the library is linked, but also when |
8a5fb4c9 MD |
414 | * reader threads are calling rcu_register_thread(). |
415 | * Should only be called by a single thread at a given time. This is ensured by | |
6abb4bd5 MD |
416 | * holing the rcu_gp_lock from rcu_register_thread() or by running at library |
417 | * load time, which should not be executed by multiple threads nor concurrently | |
418 | * with rcu_register_thread() anyway. | |
8a5fb4c9 | 419 | */ |
02be5561 | 420 | void rcu_init(void) |
27b012e2 MD |
421 | { |
422 | struct sigaction act; | |
423 | int ret; | |
424 | ||
8a5fb4c9 MD |
425 | if (init_done) |
426 | return; | |
427 | init_done = 1; | |
428 | ||
02be5561 | 429 | act.sa_sigaction = sigrcu_handler; |
dd052bd3 | 430 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
c297c21c | 431 | sigemptyset(&act.sa_mask); |
02be5561 | 432 | ret = sigaction(SIGRCU, &act, NULL); |
f69f195a MD |
433 | if (ret) { |
434 | perror("Error in sigaction"); | |
27b012e2 MD |
435 | exit(-1); |
436 | } | |
437 | } | |
438 | ||
02be5561 | 439 | void rcu_exit(void) |
27b012e2 MD |
440 | { |
441 | struct sigaction act; | |
442 | int ret; | |
443 | ||
02be5561 | 444 | ret = sigaction(SIGRCU, NULL, &act); |
f69f195a MD |
445 | if (ret) { |
446 | perror("Error in sigaction"); | |
27b012e2 MD |
447 | exit(-1); |
448 | } | |
02be5561 | 449 | assert(act.sa_sigaction == sigrcu_handler); |
16aa9ee8 | 450 | assert(cds_list_empty(®istry)); |
27b012e2 | 451 | } |
5e77fc1f | 452 | |
fdf01eed | 453 | #endif /* #ifdef RCU_SIGNAL */ |
5e77fc1f | 454 | |
5e6b23a6 | 455 | DEFINE_RCU_FLAVOR(rcu_flavor); |
541d828d | 456 | |
5e77fc1f | 457 | #include "urcu-call-rcu-impl.h" |
0376e7b2 | 458 | #include "urcu-defer-impl.h" |