| 1 | /* |
| 2 | * urcu-qsbr.c |
| 3 | * |
| 4 | * Userspace RCU QSBR library |
| 5 | * |
| 6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. |
| 8 | * |
| 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 |
| 22 | * |
| 23 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 24 | */ |
| 25 | |
| 26 | #define _GNU_SOURCE |
| 27 | #define _LGPL_SOURCE |
| 28 | #include <stdio.h> |
| 29 | #include <pthread.h> |
| 30 | #include <signal.h> |
| 31 | #include <assert.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdint.h> |
| 34 | #include <string.h> |
| 35 | #include <errno.h> |
| 36 | #include <poll.h> |
| 37 | |
| 38 | #include "urcu/wfqueue.h" |
| 39 | #include "urcu/map/urcu-qsbr.h" |
| 40 | #define BUILD_QSBR_LIB |
| 41 | #include "urcu/static/urcu-qsbr.h" |
| 42 | #include "urcu-pointer.h" |
| 43 | #include "urcu/tls-compat.h" |
| 44 | |
| 45 | #include "urcu-die.h" |
| 46 | |
| 47 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
| 48 | #undef _LGPL_SOURCE |
| 49 | #include "urcu-qsbr.h" |
| 50 | #define _LGPL_SOURCE |
| 51 | |
| 52 | void __attribute__((destructor)) rcu_exit(void); |
| 53 | |
| 54 | /* |
| 55 | * rcu_gp_lock ensures mutual exclusion between threads calling |
| 56 | * synchronize_rcu(). |
| 57 | */ |
| 58 | static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; |
| 59 | /* |
| 60 | * rcu_registry_lock ensures mutual exclusion between threads |
| 61 | * registering and unregistering themselves to/from the registry, and |
| 62 | * with threads reading that registry from synchronize_rcu(). However, |
| 63 | * this lock is not held all the way through the completion of awaiting |
| 64 | * for the grace period. It is sporadically released between iterations |
| 65 | * on the registry. |
| 66 | * rcu_registry_lock may nest inside rcu_gp_lock. |
| 67 | */ |
| 68 | static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER; |
| 69 | |
| 70 | int32_t gp_futex; |
| 71 | |
| 72 | /* |
| 73 | * Global grace period counter. |
| 74 | */ |
| 75 | unsigned long rcu_gp_ctr = RCU_GP_ONLINE; |
| 76 | |
| 77 | /* |
| 78 | * Active attempts to check for reader Q.S. before calling futex(). |
| 79 | */ |
| 80 | #define RCU_QS_ACTIVE_ATTEMPTS 100 |
| 81 | |
| 82 | /* |
| 83 | * Written to only by each individual reader. Read by both the reader and the |
| 84 | * writers. |
| 85 | */ |
| 86 | __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader, rcu_reader); |
| 87 | |
| 88 | #ifdef DEBUG_YIELD |
| 89 | unsigned int yield_active; |
| 90 | __DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield); |
| 91 | #endif |
| 92 | |
| 93 | static CDS_LIST_HEAD(registry); |
| 94 | |
| 95 | static void mutex_lock(pthread_mutex_t *mutex) |
| 96 | { |
| 97 | int ret; |
| 98 | |
| 99 | #ifndef DISTRUST_SIGNALS_EXTREME |
| 100 | ret = pthread_mutex_lock(mutex); |
| 101 | if (ret) |
| 102 | urcu_die(ret); |
| 103 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 104 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
| 105 | if (ret != EBUSY && ret != EINTR) |
| 106 | urcu_die(ret); |
| 107 | poll(NULL,0,10); |
| 108 | } |
| 109 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 110 | } |
| 111 | |
| 112 | static void mutex_unlock(pthread_mutex_t *mutex) |
| 113 | { |
| 114 | int ret; |
| 115 | |
| 116 | ret = pthread_mutex_unlock(mutex); |
| 117 | if (ret) |
| 118 | urcu_die(ret); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * synchronize_rcu() waiting. Single thread. |
| 123 | */ |
| 124 | static void wait_gp(void) |
| 125 | { |
| 126 | /* Read reader_gp before read futex */ |
| 127 | cmm_smp_rmb(); |
| 128 | if (uatomic_read(&gp_futex) != -1) |
| 129 | return; |
| 130 | while (futex_noasync(&gp_futex, FUTEX_WAIT, -1, |
| 131 | NULL, NULL, 0)) { |
| 132 | switch (errno) { |
| 133 | case EWOULDBLOCK: |
| 134 | /* Value already changed. */ |
| 135 | return; |
| 136 | case EINTR: |
| 137 | /* Retry if interrupted by signal. */ |
| 138 | break; /* Get out of switch. */ |
| 139 | default: |
| 140 | /* Unexpected error. */ |
| 141 | urcu_die(errno); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Always called with rcu_registry lock held. Releases this lock between |
| 148 | * iterations and grabs it again. Holds the lock when it returns. |
| 149 | */ |
| 150 | static void update_counter_and_wait(void) |
| 151 | { |
| 152 | CDS_LIST_HEAD(qsreaders); |
| 153 | unsigned int wait_loops = 0; |
| 154 | struct rcu_reader *index, *tmp; |
| 155 | |
| 156 | #if (CAA_BITS_PER_LONG < 64) |
| 157 | /* Switch parity: 0 -> 1, 1 -> 0 */ |
| 158 | CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR); |
| 159 | #else /* !(CAA_BITS_PER_LONG < 64) */ |
| 160 | /* Increment current G.P. */ |
| 161 | CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR); |
| 162 | #endif /* !(CAA_BITS_PER_LONG < 64) */ |
| 163 | |
| 164 | /* |
| 165 | * Must commit rcu_gp_ctr update to memory before waiting for |
| 166 | * quiescent state. Failure to do so could result in the writer |
| 167 | * waiting forever while new readers are always accessing data |
| 168 | * (no progress). Enforce compiler-order of store to rcu_gp_ctr |
| 169 | * before load URCU_TLS(rcu_reader).ctr. |
| 170 | */ |
| 171 | cmm_barrier(); |
| 172 | |
| 173 | /* |
| 174 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
| 175 | * model easier to understand. It does not have a big performance impact |
| 176 | * anyway, given this is the write-side. |
| 177 | */ |
| 178 | cmm_smp_mb(); |
| 179 | |
| 180 | /* |
| 181 | * Wait for each thread rcu_reader_qs_gp count to become 0. |
| 182 | */ |
| 183 | for (;;) { |
| 184 | if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) |
| 185 | wait_loops++; |
| 186 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
| 187 | uatomic_set(&gp_futex, -1); |
| 188 | /* |
| 189 | * Write futex before write waiting (the other side |
| 190 | * reads them in the opposite order). |
| 191 | */ |
| 192 | cmm_smp_wmb(); |
| 193 | cds_list_for_each_entry(index, ®istry, node) { |
| 194 | _CMM_STORE_SHARED(index->waiting, 1); |
| 195 | } |
| 196 | /* Write futex before read reader_gp */ |
| 197 | cmm_smp_mb(); |
| 198 | } |
| 199 | cds_list_for_each_entry_safe(index, tmp, ®istry, node) { |
| 200 | if (!rcu_gp_ongoing(&index->ctr)) |
| 201 | cds_list_move(&index->node, &qsreaders); |
| 202 | } |
| 203 | |
| 204 | if (cds_list_empty(®istry)) { |
| 205 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
| 206 | /* Read reader_gp before write futex */ |
| 207 | cmm_smp_mb(); |
| 208 | uatomic_set(&gp_futex, 0); |
| 209 | } |
| 210 | break; |
| 211 | } else { |
| 212 | /* Temporarily unlock the registry lock. */ |
| 213 | mutex_unlock(&rcu_registry_lock); |
| 214 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
| 215 | wait_gp(); |
| 216 | } else { |
| 217 | #ifndef HAS_INCOHERENT_CACHES |
| 218 | caa_cpu_relax(); |
| 219 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
| 220 | cmm_smp_mb(); |
| 221 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
| 222 | } |
| 223 | /* Re-lock the registry lock before the next loop. */ |
| 224 | mutex_lock(&rcu_registry_lock); |
| 225 | } |
| 226 | } |
| 227 | /* put back the reader list in the registry */ |
| 228 | cds_list_splice(&qsreaders, ®istry); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Using a two-subphases algorithm for architectures with smaller than 64-bit |
| 233 | * long-size to ensure we do not encounter an overflow bug. |
| 234 | */ |
| 235 | |
| 236 | #if (CAA_BITS_PER_LONG < 64) |
| 237 | void synchronize_rcu(void) |
| 238 | { |
| 239 | unsigned long was_online; |
| 240 | |
| 241 | was_online = URCU_TLS(rcu_reader).ctr; |
| 242 | |
| 243 | /* All threads should read qparity before accessing data structure |
| 244 | * where new ptr points to. In the "then" case, rcu_thread_offline |
| 245 | * includes a memory barrier. |
| 246 | * |
| 247 | * Mark the writer thread offline to make sure we don't wait for |
| 248 | * our own quiescent state. This allows using synchronize_rcu() |
| 249 | * in threads registered as readers. |
| 250 | */ |
| 251 | if (was_online) |
| 252 | rcu_thread_offline(); |
| 253 | else |
| 254 | cmm_smp_mb(); |
| 255 | |
| 256 | mutex_lock(&rcu_gp_lock); |
| 257 | mutex_lock(&rcu_registry_lock); |
| 258 | |
| 259 | if (cds_list_empty(®istry)) |
| 260 | goto out; |
| 261 | |
| 262 | /* |
| 263 | * Wait for previous parity to be empty of readers. |
| 264 | * update_counter_and_wait() can release and grab again |
| 265 | * rcu_registry_lock interally. |
| 266 | */ |
| 267 | update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */ |
| 268 | |
| 269 | /* |
| 270 | * Must finish waiting for quiescent state for parity 0 before |
| 271 | * committing next rcu_gp_ctr update to memory. Failure to |
| 272 | * do so could result in the writer waiting forever while new |
| 273 | * readers are always accessing data (no progress). Enforce |
| 274 | * compiler-order of load URCU_TLS(rcu_reader).ctr before store to |
| 275 | * rcu_gp_ctr. |
| 276 | */ |
| 277 | cmm_barrier(); |
| 278 | |
| 279 | /* |
| 280 | * Adding a cmm_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. |
| 283 | */ |
| 284 | cmm_smp_mb(); |
| 285 | |
| 286 | /* |
| 287 | * Wait for previous parity to be empty of readers. |
| 288 | * update_counter_and_wait() can release and grab again |
| 289 | * rcu_registry_lock interally. |
| 290 | */ |
| 291 | update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */ |
| 292 | out: |
| 293 | mutex_unlock(&rcu_registry_lock); |
| 294 | mutex_unlock(&rcu_gp_lock); |
| 295 | |
| 296 | /* |
| 297 | * Finish waiting for reader threads before letting the old ptr being |
| 298 | * freed. |
| 299 | */ |
| 300 | if (was_online) |
| 301 | rcu_thread_online(); |
| 302 | else |
| 303 | cmm_smp_mb(); |
| 304 | } |
| 305 | #else /* !(CAA_BITS_PER_LONG < 64) */ |
| 306 | void synchronize_rcu(void) |
| 307 | { |
| 308 | unsigned long was_online; |
| 309 | |
| 310 | was_online = URCU_TLS(rcu_reader).ctr; |
| 311 | |
| 312 | /* |
| 313 | * Mark the writer thread offline to make sure we don't wait for |
| 314 | * our own quiescent state. This allows using synchronize_rcu() |
| 315 | * in threads registered as readers. |
| 316 | */ |
| 317 | if (was_online) |
| 318 | rcu_thread_offline(); |
| 319 | else |
| 320 | cmm_smp_mb(); |
| 321 | |
| 322 | mutex_lock(&rcu_gp_lock); |
| 323 | mutex_lock(&rcu_registry_lock); |
| 324 | if (cds_list_empty(®istry)) |
| 325 | goto out; |
| 326 | /* |
| 327 | * update_counter_and_wait() can release and grab again |
| 328 | * rcu_registry_lock interally. |
| 329 | */ |
| 330 | update_counter_and_wait(); |
| 331 | out: |
| 332 | mutex_unlock(&rcu_registry_lock); |
| 333 | mutex_unlock(&rcu_gp_lock); |
| 334 | |
| 335 | if (was_online) |
| 336 | rcu_thread_online(); |
| 337 | else |
| 338 | cmm_smp_mb(); |
| 339 | } |
| 340 | #endif /* !(CAA_BITS_PER_LONG < 64) */ |
| 341 | |
| 342 | /* |
| 343 | * library wrappers to be used by non-LGPL compatible source code. |
| 344 | */ |
| 345 | |
| 346 | void rcu_read_lock(void) |
| 347 | { |
| 348 | _rcu_read_lock(); |
| 349 | } |
| 350 | |
| 351 | void rcu_read_unlock(void) |
| 352 | { |
| 353 | _rcu_read_unlock(); |
| 354 | } |
| 355 | |
| 356 | void rcu_quiescent_state(void) |
| 357 | { |
| 358 | _rcu_quiescent_state(); |
| 359 | } |
| 360 | |
| 361 | void rcu_thread_offline(void) |
| 362 | { |
| 363 | _rcu_thread_offline(); |
| 364 | } |
| 365 | |
| 366 | void rcu_thread_online(void) |
| 367 | { |
| 368 | _rcu_thread_online(); |
| 369 | } |
| 370 | |
| 371 | void rcu_register_thread(void) |
| 372 | { |
| 373 | URCU_TLS(rcu_reader).tid = pthread_self(); |
| 374 | assert(URCU_TLS(rcu_reader).ctr == 0); |
| 375 | |
| 376 | mutex_lock(&rcu_registry_lock); |
| 377 | cds_list_add(&URCU_TLS(rcu_reader).node, ®istry); |
| 378 | mutex_unlock(&rcu_registry_lock); |
| 379 | _rcu_thread_online(); |
| 380 | } |
| 381 | |
| 382 | void rcu_unregister_thread(void) |
| 383 | { |
| 384 | /* |
| 385 | * We have to make the thread offline otherwise we end up dealocking |
| 386 | * with a waiting writer. |
| 387 | */ |
| 388 | _rcu_thread_offline(); |
| 389 | mutex_lock(&rcu_registry_lock); |
| 390 | cds_list_del(&URCU_TLS(rcu_reader).node); |
| 391 | mutex_unlock(&rcu_registry_lock); |
| 392 | } |
| 393 | |
| 394 | void rcu_exit(void) |
| 395 | { |
| 396 | /* |
| 397 | * Assertion disabled because call_rcu threads are now rcu |
| 398 | * readers, and left running at exit. |
| 399 | * assert(cds_list_empty(®istry)); |
| 400 | */ |
| 401 | } |
| 402 | |
| 403 | DEFINE_RCU_FLAVOR(rcu_flavor); |
| 404 | |
| 405 | #include "urcu-call-rcu-impl.h" |
| 406 | #include "urcu-defer-impl.h" |