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