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 |
71c811bf | 27 | #define _LGPL_SOURCE |
82d50e1a | 28 | #define _DEFAULT_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 | |
999991c6 | 39 | #include "urcu/arch.h" |
d73fb81f | 40 | #include "urcu/wfcqueue.h" |
57760d44 | 41 | #include "urcu/map/urcu.h" |
af7c2dbe | 42 | #include "urcu/static/urcu.h" |
618b2595 | 43 | #include "urcu-pointer.h" |
bd252a04 | 44 | #include "urcu/tls-compat.h" |
71c811bf | 45 | |
4a6d7378 | 46 | #include "urcu-die.h" |
5bffdd5d | 47 | #include "urcu-wait.h" |
4a6d7378 | 48 | |
121a5d44 | 49 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
71c811bf | 50 | #undef _LGPL_SOURCE |
27b012e2 | 51 | #include "urcu.h" |
71c811bf | 52 | #define _LGPL_SOURCE |
27b012e2 | 53 | |
3a71751e PB |
54 | /* |
55 | * If a reader is really non-cooperative and refuses to commit its | |
56 | * rcu_active_readers count to memory (there is no barrier in the reader | |
9340c38d | 57 | * per-se), kick it after 10 loops waiting for it. |
3a71751e | 58 | */ |
9340c38d | 59 | #define KICK_READER_LOOPS 10 |
3a71751e PB |
60 | |
61 | /* | |
62 | * Active attempts to check for reader Q.S. before calling futex(). | |
63 | */ | |
64 | #define RCU_QS_ACTIVE_ATTEMPTS 100 | |
65 | ||
999991c6 MD |
66 | /* If the headers do not support membarrier system call, fall back on RCU_MB */ |
67 | #ifdef __NR_membarrier | |
68 | # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__) | |
553b7eb9 MD |
69 | #else |
70 | # define membarrier(...) -ENOSYS | |
71 | #endif | |
72 | ||
64f469e6 MD |
73 | enum membarrier_cmd { |
74 | MEMBARRIER_CMD_QUERY = 0, | |
75 | MEMBARRIER_CMD_SHARED = (1 << 0), | |
76 | }; | |
553b7eb9 | 77 | |
fdf01eed | 78 | #ifdef RCU_MEMBARRIER |
834a45ba | 79 | static int init_done; |
d8d9a340 MD |
80 | #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER |
81 | int rcu_has_sys_membarrier_memb; | |
82 | #endif | |
834a45ba | 83 | |
02be5561 | 84 | void __attribute__((constructor)) rcu_init(void); |
fdf01eed MD |
85 | #endif |
86 | ||
87 | #ifdef RCU_MB | |
02be5561 | 88 | void rcu_init(void) |
e90a6e9c MD |
89 | { |
90 | } | |
91 | #endif | |
8a5fb4c9 | 92 | |
fdf01eed MD |
93 | #ifdef RCU_SIGNAL |
94 | static int init_done; | |
95 | ||
96 | void __attribute__((constructor)) rcu_init(void); | |
97 | void __attribute__((destructor)) rcu_exit(void); | |
98 | #endif | |
99 | ||
731ccb96 MD |
100 | /* |
101 | * rcu_gp_lock ensures mutual exclusion between threads calling | |
102 | * synchronize_rcu(). | |
103 | */ | |
6abb4bd5 | 104 | static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; |
731ccb96 MD |
105 | /* |
106 | * rcu_registry_lock ensures mutual exclusion between threads | |
107 | * registering and unregistering themselves to/from the registry, and | |
108 | * with threads reading that registry from synchronize_rcu(). However, | |
109 | * this lock is not held all the way through the completion of awaiting | |
110 | * for the grace period. It is sporadically released between iterations | |
111 | * on the registry. | |
112 | * rcu_registry_lock may nest inside rcu_gp_lock. | |
113 | */ | |
114 | static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER; | |
4de0cd31 | 115 | struct rcu_gp rcu_gp = { .ctr = RCU_GP_COUNT }; |
27b012e2 | 116 | |
b0d5e790 MD |
117 | /* |
118 | * Written to only by each individual reader. Read by both the reader and the | |
119 | * writers. | |
120 | */ | |
bd252a04 | 121 | DEFINE_URCU_TLS(struct rcu_reader, rcu_reader); |
27b012e2 | 122 | |
16aa9ee8 | 123 | static CDS_LIST_HEAD(registry); |
27b012e2 | 124 | |
5bffdd5d MD |
125 | /* |
126 | * Queue keeping threads awaiting to wait for a grace period. Contains | |
127 | * struct gp_waiters_thread objects. | |
128 | */ | |
129 | static DEFINE_URCU_WAIT_QUEUE(gp_waiters); | |
130 | ||
6abb4bd5 | 131 | static void mutex_lock(pthread_mutex_t *mutex) |
41718ff9 MD |
132 | { |
133 | int ret; | |
09a9f986 PM |
134 | |
135 | #ifndef DISTRUST_SIGNALS_EXTREME | |
6abb4bd5 | 136 | ret = pthread_mutex_lock(mutex); |
4a6d7378 MD |
137 | if (ret) |
138 | urcu_die(ret); | |
09a9f986 | 139 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
6abb4bd5 | 140 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
4a6d7378 MD |
141 | if (ret != EBUSY && ret != EINTR) |
142 | urcu_die(ret); | |
bd252a04 | 143 | if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) { |
5481ddb3 | 144 | cmm_smp_mb(); |
bd252a04 | 145 | _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0); |
5481ddb3 | 146 | cmm_smp_mb(); |
09a9f986 | 147 | } |
8999a9ee | 148 | (void) poll(NULL, 0, 10); |
09a9f986 PM |
149 | } |
150 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
41718ff9 MD |
151 | } |
152 | ||
6abb4bd5 | 153 | static void mutex_unlock(pthread_mutex_t *mutex) |
41718ff9 MD |
154 | { |
155 | int ret; | |
156 | ||
6abb4bd5 | 157 | ret = pthread_mutex_unlock(mutex); |
4a6d7378 MD |
158 | if (ret) |
159 | urcu_die(ret); | |
41718ff9 MD |
160 | } |
161 | ||
fdf01eed | 162 | #ifdef RCU_MEMBARRIER |
a4922ed9 | 163 | static void smp_mb_master(void) |
fdf01eed | 164 | { |
d8d9a340 | 165 | if (caa_likely(rcu_has_sys_membarrier_memb)) |
64f469e6 | 166 | (void) membarrier(MEMBARRIER_CMD_SHARED, 0); |
fdf01eed | 167 | else |
5481ddb3 | 168 | cmm_smp_mb(); |
fdf01eed MD |
169 | } |
170 | #endif | |
171 | ||
02be5561 | 172 | #ifdef RCU_MB |
a4922ed9 | 173 | static void smp_mb_master(void) |
40e140c9 | 174 | { |
5481ddb3 | 175 | cmm_smp_mb(); |
40e140c9 | 176 | } |
fdf01eed MD |
177 | #endif |
178 | ||
179 | #ifdef RCU_SIGNAL | |
78ff9419 | 180 | static void force_mb_all_readers(void) |
27b012e2 | 181 | { |
02be5561 | 182 | struct rcu_reader *index; |
e3b0cef0 | 183 | |
27b012e2 | 184 | /* |
5481ddb3 | 185 | * Ask for each threads to execute a cmm_smp_mb() so we can consider the |
27b012e2 MD |
186 | * compiler barriers around rcu read lock as real memory barriers. |
187 | */ | |
16aa9ee8 | 188 | if (cds_list_empty(®istry)) |
27b012e2 | 189 | return; |
3a86deba | 190 | /* |
5481ddb3 | 191 | * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs |
157dca95 | 192 | * a cache flush on architectures with non-coherent cache. Let's play |
5481ddb3 | 193 | * safe and don't assume anything : we use cmm_smp_mc() to make sure the |
157dca95 | 194 | * cache flush is enforced. |
3a86deba | 195 | */ |
16aa9ee8 | 196 | cds_list_for_each_entry(index, ®istry, node) { |
6cf3827c | 197 | CMM_STORE_SHARED(index->need_mb, 1); |
02be5561 | 198 | pthread_kill(index->tid, SIGRCU); |
09a9f986 | 199 | } |
27b012e2 MD |
200 | /* |
201 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
09a9f986 PM |
202 | * |
203 | * Note that the pthread_kill() will never be executed on systems | |
204 | * that correctly deliver signals in a timely manner. However, it | |
205 | * is not uncommon for kernels to have bugs that can result in | |
206 | * lost or unduly delayed signals. | |
207 | * | |
208 | * If you are seeing the below pthread_kill() executing much at | |
209 | * all, we suggest testing the underlying kernel and filing the | |
210 | * relevant bug report. For Linux kernels, we recommend getting | |
211 | * the Linux Test Project (LTP). | |
27b012e2 | 212 | */ |
16aa9ee8 | 213 | cds_list_for_each_entry(index, ®istry, node) { |
6cf3827c | 214 | while (CMM_LOAD_SHARED(index->need_mb)) { |
02be5561 | 215 | pthread_kill(index->tid, SIGRCU); |
8999a9ee | 216 | (void) poll(NULL, 0, 1); |
09a9f986 PM |
217 | } |
218 | } | |
5481ddb3 | 219 | cmm_smp_mb(); /* read ->need_mb before ending the barrier */ |
27b012e2 | 220 | } |
9d7e3f89 | 221 | |
a4922ed9 | 222 | static void smp_mb_master(void) |
9d7e3f89 MD |
223 | { |
224 | force_mb_all_readers(); | |
225 | } | |
fdf01eed | 226 | #endif /* #ifdef RCU_SIGNAL */ |
27b012e2 | 227 | |
bc6c15bb MD |
228 | /* |
229 | * synchronize_rcu() waiting. Single thread. | |
fca9fb96 MD |
230 | * Always called with rcu_registry lock held. Releases this lock and |
231 | * grabs it again. Holds the lock when it returns. | |
bc6c15bb | 232 | */ |
cfe78e25 | 233 | static void wait_gp(void) |
bc6c15bb | 234 | { |
fca9fb96 MD |
235 | /* |
236 | * Read reader_gp before read futex. smp_mb_master() needs to | |
237 | * be called with the rcu registry lock held in RCU_SIGNAL | |
238 | * flavor. | |
239 | */ | |
a4922ed9 | 240 | smp_mb_master(); |
fca9fb96 MD |
241 | /* Temporarily unlock the registry lock. */ |
242 | mutex_unlock(&rcu_registry_lock); | |
b0a841b4 | 243 | if (uatomic_read(&rcu_gp.futex) != -1) |
fca9fb96 | 244 | goto end; |
b0a841b4 MD |
245 | while (futex_async(&rcu_gp.futex, FUTEX_WAIT, -1, |
246 | NULL, NULL, 0)) { | |
247 | switch (errno) { | |
248 | case EWOULDBLOCK: | |
249 | /* Value already changed. */ | |
fca9fb96 | 250 | goto end; |
b0a841b4 MD |
251 | case EINTR: |
252 | /* Retry if interrupted by signal. */ | |
253 | break; /* Get out of switch. */ | |
254 | default: | |
255 | /* Unexpected error. */ | |
256 | urcu_die(errno); | |
257 | } | |
258 | } | |
fca9fb96 MD |
259 | end: |
260 | /* | |
261 | * Re-lock the registry lock before the next loop. | |
262 | */ | |
263 | mutex_lock(&rcu_registry_lock); | |
bc6c15bb MD |
264 | } |
265 | ||
731ccb96 MD |
266 | /* |
267 | * Always called with rcu_registry lock held. Releases this lock between | |
268 | * iterations and grabs it again. Holds the lock when it returns. | |
269 | */ | |
fd189fa5 MD |
270 | static void wait_for_readers(struct cds_list_head *input_readers, |
271 | struct cds_list_head *cur_snap_readers, | |
272 | struct cds_list_head *qsreaders) | |
27b012e2 | 273 | { |
9340c38d | 274 | unsigned int wait_loops = 0; |
02be5561 | 275 | struct rcu_reader *index, *tmp; |
9340c38d MD |
276 | #ifdef HAS_INCOHERENT_CACHES |
277 | unsigned int wait_gp_loops = 0; | |
278 | #endif /* HAS_INCOHERENT_CACHES */ | |
27b012e2 | 279 | |
40e140c9 | 280 | /* |
c9488684 MD |
281 | * Wait for each thread URCU_TLS(rcu_reader).ctr to either |
282 | * indicate quiescence (not nested), or observe the current | |
ed1b099e | 283 | * rcu_gp.ctr value. |
27b012e2 | 284 | */ |
cfe78e25 | 285 | for (;;) { |
5e81fed7 MD |
286 | if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) |
287 | wait_loops++; | |
9340c38d | 288 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
ed1b099e | 289 | uatomic_dec(&rcu_gp.futex); |
cfe78e25 | 290 | /* Write futex before read reader_gp */ |
a4922ed9 | 291 | smp_mb_master(); |
cfe78e25 MD |
292 | } |
293 | ||
fd189fa5 MD |
294 | cds_list_for_each_entry_safe(index, tmp, input_readers, node) { |
295 | switch (rcu_reader_state(&index->ctr)) { | |
296 | case RCU_READER_ACTIVE_CURRENT: | |
297 | if (cur_snap_readers) { | |
298 | cds_list_move(&index->node, | |
299 | cur_snap_readers); | |
300 | break; | |
301 | } | |
302 | /* Fall-through */ | |
303 | case RCU_READER_INACTIVE: | |
304 | cds_list_move(&index->node, qsreaders); | |
305 | break; | |
306 | case RCU_READER_ACTIVE_OLD: | |
307 | /* | |
308 | * Old snapshot. Leaving node in | |
309 | * input_readers will make us busy-loop | |
310 | * until the snapshot becomes current or | |
311 | * the reader becomes inactive. | |
312 | */ | |
313 | break; | |
314 | } | |
cfe78e25 MD |
315 | } |
316 | ||
e8043c1b | 317 | #ifndef HAS_INCOHERENT_CACHES |
fd189fa5 | 318 | if (cds_list_empty(input_readers)) { |
9340c38d | 319 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
cfe78e25 | 320 | /* Read reader_gp before write futex */ |
a4922ed9 | 321 | smp_mb_master(); |
ed1b099e | 322 | uatomic_set(&rcu_gp.futex, 0); |
bc6c15bb | 323 | } |
cfe78e25 MD |
324 | break; |
325 | } else { | |
fca9fb96 MD |
326 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
327 | /* wait_gp unlocks/locks registry lock. */ | |
cfe78e25 | 328 | wait_gp(); |
fca9fb96 MD |
329 | } else { |
330 | /* Temporarily unlock the registry lock. */ | |
331 | mutex_unlock(&rcu_registry_lock); | |
06f22bdb | 332 | caa_cpu_relax(); |
fca9fb96 MD |
333 | /* |
334 | * Re-lock the registry lock before the | |
335 | * next loop. | |
336 | */ | |
337 | mutex_lock(&rcu_registry_lock); | |
338 | } | |
bc6c15bb | 339 | } |
e8043c1b | 340 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 341 | /* |
40e140c9 | 342 | * BUSY-LOOP. Force the reader thread to commit its |
bd252a04 MD |
343 | * URCU_TLS(rcu_reader).ctr update to memory if we wait |
344 | * for too long. | |
27b012e2 | 345 | */ |
fd189fa5 | 346 | if (cds_list_empty(input_readers)) { |
9340c38d | 347 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
cfe78e25 | 348 | /* Read reader_gp before write futex */ |
a4922ed9 | 349 | smp_mb_master(); |
ed1b099e | 350 | uatomic_set(&rcu_gp.futex, 0); |
cfe78e25 MD |
351 | } |
352 | break; | |
353 | } else { | |
9340c38d | 354 | if (wait_gp_loops == KICK_READER_LOOPS) { |
a4922ed9 | 355 | smp_mb_master(); |
9340c38d MD |
356 | wait_gp_loops = 0; |
357 | } | |
358 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { | |
fca9fb96 | 359 | /* wait_gp unlocks/locks registry lock. */ |
9340c38d MD |
360 | wait_gp(); |
361 | wait_gp_loops++; | |
362 | } else { | |
fca9fb96 MD |
363 | /* Temporarily unlock the registry lock. */ |
364 | mutex_unlock(&rcu_registry_lock); | |
06f22bdb | 365 | caa_cpu_relax(); |
fca9fb96 MD |
366 | /* |
367 | * Re-lock the registry lock before the | |
368 | * next loop. | |
369 | */ | |
370 | mutex_lock(&rcu_registry_lock); | |
40e140c9 MD |
371 | } |
372 | } | |
e8043c1b | 373 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 374 | } |
27b012e2 MD |
375 | } |
376 | ||
9598a481 | 377 | void synchronize_rcu(void) |
2bc59bd7 | 378 | { |
fd189fa5 MD |
379 | CDS_LIST_HEAD(cur_snap_readers); |
380 | CDS_LIST_HEAD(qsreaders); | |
5bffdd5d MD |
381 | DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING); |
382 | struct urcu_waiters waiters; | |
383 | ||
384 | /* | |
385 | * Add ourself to gp_waiters queue of threads awaiting to wait | |
386 | * for a grace period. Proceed to perform the grace period only | |
387 | * if we are the first thread added into the queue. | |
388 | * The implicit memory barrier before urcu_wait_add() | |
389 | * orders prior memory accesses of threads put into the wait | |
390 | * queue before their insertion into the wait queue. | |
391 | */ | |
392 | if (urcu_wait_add(&gp_waiters, &wait) != 0) { | |
393 | /* Not first in queue: will be awakened by another thread. */ | |
394 | urcu_adaptative_busy_wait(&wait); | |
395 | /* Order following memory accesses after grace period. */ | |
396 | cmm_smp_mb(); | |
397 | return; | |
398 | } | |
399 | /* We won't need to wake ourself up */ | |
400 | urcu_wait_set_state(&wait, URCU_WAIT_RUNNING); | |
fd189fa5 | 401 | |
6abb4bd5 | 402 | mutex_lock(&rcu_gp_lock); |
135530fd | 403 | |
5bffdd5d MD |
404 | /* |
405 | * Move all waiters into our local queue. | |
406 | */ | |
407 | urcu_move_waiters(&waiters, &gp_waiters); | |
408 | ||
731ccb96 MD |
409 | mutex_lock(&rcu_registry_lock); |
410 | ||
16aa9ee8 | 411 | if (cds_list_empty(®istry)) |
2dfb8b5e MD |
412 | goto out; |
413 | ||
731ccb96 MD |
414 | /* |
415 | * All threads should read qparity before accessing data structure | |
416 | * where new ptr points to. Must be done within rcu_registry_lock | |
417 | * because it iterates on reader threads. | |
418 | */ | |
9598a481 | 419 | /* Write new ptr before changing the qparity */ |
a4922ed9 | 420 | smp_mb_master(); |
9598a481 | 421 | |
9598a481 | 422 | /* |
c9488684 | 423 | * Wait for readers to observe original parity or be quiescent. |
731ccb96 MD |
424 | * wait_for_readers() can release and grab again rcu_registry_lock |
425 | * interally. | |
9598a481 | 426 | */ |
fd189fa5 | 427 | wait_for_readers(®istry, &cur_snap_readers, &qsreaders); |
9598a481 MD |
428 | |
429 | /* | |
c9488684 | 430 | * Must finish waiting for quiescent state for original parity before |
ed1b099e | 431 | * committing next rcu_gp.ctr update to memory. Failure to do so could |
d40fde2c MD |
432 | * result in the writer waiting forever while new readers are always |
433 | * accessing data (no progress). Enforce compiler-order of load | |
ed1b099e | 434 | * URCU_TLS(rcu_reader).ctr before store to rcu_gp.ctr. |
9598a481 | 435 | */ |
5481ddb3 | 436 | cmm_barrier(); |
9598a481 | 437 | |
5dba80f9 | 438 | /* |
5481ddb3 | 439 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
5dba80f9 MD |
440 | * model easier to understand. It does not have a big performance impact |
441 | * anyway, given this is the write-side. | |
442 | */ | |
5481ddb3 | 443 | cmm_smp_mb(); |
67c2d80b | 444 | |
c9488684 | 445 | /* Switch parity: 0 -> 1, 1 -> 0 */ |
ed1b099e | 446 | CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR_PHASE); |
c9488684 MD |
447 | |
448 | /* | |
ed1b099e | 449 | * Must commit rcu_gp.ctr update to memory before waiting for quiescent |
c9488684 MD |
450 | * state. Failure to do so could result in the writer waiting forever |
451 | * while new readers are always accessing data (no progress). Enforce | |
ed1b099e | 452 | * compiler-order of store to rcu_gp.ctr before load rcu_reader ctr. |
c9488684 MD |
453 | */ |
454 | cmm_barrier(); | |
455 | ||
456 | /* | |
457 | * | |
458 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the | |
459 | * model easier to understand. It does not have a big performance impact | |
460 | * anyway, given this is the write-side. | |
461 | */ | |
462 | cmm_smp_mb(); | |
463 | ||
9598a481 | 464 | /* |
c9488684 | 465 | * Wait for readers to observe new parity or be quiescent. |
731ccb96 MD |
466 | * wait_for_readers() can release and grab again rcu_registry_lock |
467 | * interally. | |
9598a481 | 468 | */ |
fd189fa5 MD |
469 | wait_for_readers(&cur_snap_readers, NULL, &qsreaders); |
470 | ||
471 | /* | |
472 | * Put quiescent reader list back into registry. | |
473 | */ | |
474 | cds_list_splice(&qsreaders, ®istry); | |
9598a481 | 475 | |
731ccb96 MD |
476 | /* |
477 | * Finish waiting for reader threads before letting the old ptr | |
478 | * being freed. Must be done within rcu_registry_lock because it | |
479 | * iterates on reader threads. | |
480 | */ | |
a4922ed9 | 481 | smp_mb_master(); |
2dfb8b5e | 482 | out: |
731ccb96 | 483 | mutex_unlock(&rcu_registry_lock); |
6abb4bd5 | 484 | mutex_unlock(&rcu_gp_lock); |
5bffdd5d MD |
485 | |
486 | /* | |
487 | * Wakeup waiters only after we have completed the grace period | |
488 | * and have ensured the memory barriers at the end of the grace | |
489 | * period have been issued. | |
490 | */ | |
491 | urcu_wake_all_waiters(&waiters); | |
2bc59bd7 PM |
492 | } |
493 | ||
121a5d44 MD |
494 | /* |
495 | * library wrappers to be used by non-LGPL compatible source code. | |
496 | */ | |
497 | ||
498 | void rcu_read_lock(void) | |
499 | { | |
500 | _rcu_read_lock(); | |
501 | } | |
502 | ||
503 | void rcu_read_unlock(void) | |
504 | { | |
505 | _rcu_read_unlock(); | |
506 | } | |
507 | ||
882f3357 MD |
508 | int rcu_read_ongoing(void) |
509 | { | |
510 | return _rcu_read_ongoing(); | |
511 | } | |
512 | ||
121a5d44 | 513 | void rcu_register_thread(void) |
27b012e2 | 514 | { |
bd252a04 MD |
515 | URCU_TLS(rcu_reader).tid = pthread_self(); |
516 | assert(URCU_TLS(rcu_reader).need_mb == 0); | |
517 | assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK)); | |
02be5561 | 518 | |
731ccb96 | 519 | mutex_lock(&rcu_registry_lock); |
a77f7d82 MD |
520 | assert(!URCU_TLS(rcu_reader).registered); |
521 | URCU_TLS(rcu_reader).registered = 1; | |
02be5561 | 522 | rcu_init(); /* In case gcc does not support constructor attribute */ |
bd252a04 | 523 | cds_list_add(&URCU_TLS(rcu_reader).node, ®istry); |
731ccb96 | 524 | mutex_unlock(&rcu_registry_lock); |
27b012e2 MD |
525 | } |
526 | ||
121a5d44 | 527 | void rcu_unregister_thread(void) |
27b012e2 | 528 | { |
731ccb96 | 529 | mutex_lock(&rcu_registry_lock); |
a77f7d82 MD |
530 | assert(URCU_TLS(rcu_reader).registered); |
531 | URCU_TLS(rcu_reader).registered = 0; | |
bd252a04 | 532 | cds_list_del(&URCU_TLS(rcu_reader).node); |
731ccb96 | 533 | mutex_unlock(&rcu_registry_lock); |
27b012e2 MD |
534 | } |
535 | ||
fdf01eed | 536 | #ifdef RCU_MEMBARRIER |
d8d9a340 MD |
537 | |
538 | #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER | |
539 | static | |
540 | void rcu_sys_membarrier_status(int available) | |
541 | { | |
542 | if (!available) | |
543 | abort(); | |
544 | } | |
545 | #else | |
546 | static | |
547 | void rcu_sys_membarrier_status(int available) | |
548 | { | |
549 | if (available) | |
550 | rcu_has_sys_membarrier_memb = 1; | |
551 | } | |
552 | #endif | |
553 | ||
fdf01eed MD |
554 | void rcu_init(void) |
555 | { | |
64f469e6 MD |
556 | int ret; |
557 | ||
fdf01eed MD |
558 | if (init_done) |
559 | return; | |
560 | init_done = 1; | |
64f469e6 | 561 | ret = membarrier(MEMBARRIER_CMD_QUERY, 0); |
d8d9a340 | 562 | rcu_sys_membarrier_status(ret >= 0 && (ret & MEMBARRIER_CMD_SHARED)); |
fdf01eed MD |
563 | } |
564 | #endif | |
565 | ||
566 | #ifdef RCU_SIGNAL | |
02be5561 | 567 | static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 | 568 | { |
40e140c9 | 569 | /* |
5481ddb3 DG |
570 | * Executing this cmm_smp_mb() is the only purpose of this signal handler. |
571 | * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is | |
40e140c9 MD |
572 | * executed on. |
573 | */ | |
5481ddb3 | 574 | cmm_smp_mb(); |
bd252a04 | 575 | _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0); |
5481ddb3 | 576 | cmm_smp_mb(); |
27b012e2 MD |
577 | } |
578 | ||
8a5fb4c9 | 579 | /* |
02be5561 | 580 | * rcu_init constructor. Called when the library is linked, but also when |
8a5fb4c9 MD |
581 | * reader threads are calling rcu_register_thread(). |
582 | * Should only be called by a single thread at a given time. This is ensured by | |
731ccb96 MD |
583 | * holing the rcu_registry_lock from rcu_register_thread() or by running |
584 | * at library load time, which should not be executed by multiple | |
585 | * threads nor concurrently with rcu_register_thread() anyway. | |
8a5fb4c9 | 586 | */ |
02be5561 | 587 | void rcu_init(void) |
27b012e2 MD |
588 | { |
589 | struct sigaction act; | |
590 | int ret; | |
591 | ||
8a5fb4c9 MD |
592 | if (init_done) |
593 | return; | |
594 | init_done = 1; | |
595 | ||
02be5561 | 596 | act.sa_sigaction = sigrcu_handler; |
dd052bd3 | 597 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
c297c21c | 598 | sigemptyset(&act.sa_mask); |
02be5561 | 599 | ret = sigaction(SIGRCU, &act, NULL); |
4a6d7378 MD |
600 | if (ret) |
601 | urcu_die(errno); | |
27b012e2 MD |
602 | } |
603 | ||
02be5561 | 604 | void rcu_exit(void) |
27b012e2 | 605 | { |
71210954 MD |
606 | /* |
607 | * Don't unregister the SIGRCU signal handler anymore, because | |
608 | * call_rcu threads could still be using it shortly before the | |
609 | * application exits. | |
610 | * Assertion disabled because call_rcu threads are now rcu | |
611 | * readers, and left running at exit. | |
612 | * assert(cds_list_empty(®istry)); | |
613 | */ | |
27b012e2 | 614 | } |
5e77fc1f | 615 | |
fdf01eed | 616 | #endif /* #ifdef RCU_SIGNAL */ |
5e77fc1f | 617 | |
5e6b23a6 | 618 | DEFINE_RCU_FLAVOR(rcu_flavor); |
541d828d | 619 | |
5e77fc1f | 620 | #include "urcu-call-rcu-impl.h" |
0376e7b2 | 621 | #include "urcu-defer-impl.h" |