| 1 | /* |
| 2 | * urcu-qsbr.c |
| 3 | * |
| 4 | * Userspace RCU QSBR library |
| 5 | * |
| 6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
| 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 | #include <stdio.h> |
| 27 | #include <pthread.h> |
| 28 | #include <signal.h> |
| 29 | #include <assert.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <errno.h> |
| 33 | #include <poll.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #define BUILD_QSBR_LIB |
| 37 | #include "urcu-qsbr-static.h" |
| 38 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
| 39 | #include "urcu-qsbr.h" |
| 40 | |
| 41 | static pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 42 | |
| 43 | /* |
| 44 | * Global grace period counter. |
| 45 | */ |
| 46 | unsigned long urcu_gp_ctr = RCU_GP_ONLINE; |
| 47 | |
| 48 | /* |
| 49 | * Written to only by each individual reader. Read by both the reader and the |
| 50 | * writers. |
| 51 | */ |
| 52 | struct urcu_reader_status __thread urcu_reader_status; |
| 53 | |
| 54 | /* Thread IDs of registered readers */ |
| 55 | #define INIT_NUM_THREADS 4 |
| 56 | |
| 57 | struct reader_registry { |
| 58 | pthread_t tid; |
| 59 | struct urcu_reader_status *urcu_reader_status; |
| 60 | }; |
| 61 | |
| 62 | #ifdef DEBUG_YIELD |
| 63 | unsigned int yield_active; |
| 64 | unsigned int __thread rand_yield; |
| 65 | #endif |
| 66 | |
| 67 | static struct reader_registry *registry; |
| 68 | static int num_readers, alloc_readers; |
| 69 | |
| 70 | static void internal_urcu_lock(void) |
| 71 | { |
| 72 | int ret; |
| 73 | |
| 74 | #ifndef DISTRUST_SIGNALS_EXTREME |
| 75 | ret = pthread_mutex_lock(&urcu_mutex); |
| 76 | if (ret) { |
| 77 | perror("Error in pthread mutex lock"); |
| 78 | exit(-1); |
| 79 | } |
| 80 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 81 | while ((ret = pthread_mutex_trylock(&urcu_mutex)) != 0) { |
| 82 | if (ret != EBUSY && ret != EINTR) { |
| 83 | printf("ret = %d, errno = %d\n", ret, errno); |
| 84 | perror("Error in pthread mutex lock"); |
| 85 | exit(-1); |
| 86 | } |
| 87 | poll(NULL,0,10); |
| 88 | } |
| 89 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 90 | } |
| 91 | |
| 92 | static void internal_urcu_unlock(void) |
| 93 | { |
| 94 | int ret; |
| 95 | |
| 96 | ret = pthread_mutex_unlock(&urcu_mutex); |
| 97 | if (ret) { |
| 98 | perror("Error in pthread mutex unlock"); |
| 99 | exit(-1); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * synchronize_rcu() waiting. Single thread. |
| 105 | */ |
| 106 | static void wait_for_quiescent_state(void) |
| 107 | { |
| 108 | struct reader_registry *index; |
| 109 | |
| 110 | if (!registry) |
| 111 | return; |
| 112 | /* |
| 113 | * Wait for each thread rcu_reader qs_gp count to become 0. |
| 114 | */ |
| 115 | for (index = registry; index < registry + num_readers; index++) { |
| 116 | int wait_loops = 0; |
| 117 | |
| 118 | if (likely(!rcu_gp_ongoing(&index->urcu_reader_status->qs_gp))) |
| 119 | continue; |
| 120 | |
| 121 | index->urcu_reader_status->gp_waiting = 1; |
| 122 | while (rcu_gp_ongoing(&index->urcu_reader_status->qs_gp)) { |
| 123 | if (wait_loops++ == RCU_QS_ACTIVE_ATTEMPTS) { |
| 124 | /* adapted wait time, in us */ |
| 125 | usleep(LOAD_SHARED(index->urcu_reader_status->qs_time_delta_usec) / 4); |
| 126 | wait_loops = 0; |
| 127 | } else { |
| 128 | #ifndef HAS_INCOHERENT_CACHES |
| 129 | cpu_relax(); |
| 130 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
| 131 | smp_mb(); |
| 132 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
| 133 | } |
| 134 | } |
| 135 | index->urcu_reader_status->gp_waiting = 0; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Using a two-subphases algorithm for architectures with smaller than 64-bit |
| 141 | * long-size to ensure we do not encounter an overflow bug. |
| 142 | */ |
| 143 | |
| 144 | #if (BITS_PER_LONG < 64) |
| 145 | /* |
| 146 | * called with urcu_mutex held. |
| 147 | */ |
| 148 | static void switch_next_urcu_qparity(void) |
| 149 | { |
| 150 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR); |
| 151 | } |
| 152 | |
| 153 | void synchronize_rcu(void) |
| 154 | { |
| 155 | unsigned long was_online; |
| 156 | |
| 157 | was_online = urcu_reader_status.qs_gp; |
| 158 | |
| 159 | /* All threads should read qparity before accessing data structure |
| 160 | * where new ptr points to. |
| 161 | */ |
| 162 | /* Write new ptr before changing the qparity */ |
| 163 | smp_mb(); |
| 164 | |
| 165 | /* |
| 166 | * Mark the writer thread offline to make sure we don't wait for |
| 167 | * our own quiescent state. This allows using synchronize_rcu() in |
| 168 | * threads registered as readers. |
| 169 | */ |
| 170 | if (was_online) |
| 171 | STORE_SHARED(urcu_reader_status.qs_gp, 0); |
| 172 | |
| 173 | internal_urcu_lock(); |
| 174 | |
| 175 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
| 176 | |
| 177 | switch_next_urcu_qparity(); /* 0 -> 1 */ |
| 178 | |
| 179 | /* |
| 180 | * Must commit qparity update to memory before waiting for parity |
| 181 | * 0 quiescent state. Failure to do so could result in the writer |
| 182 | * waiting forever while new readers are always accessing data (no |
| 183 | * progress). |
| 184 | * Ensured by STORE_SHARED and LOAD_SHARED. |
| 185 | */ |
| 186 | |
| 187 | /* |
| 188 | * Wait for previous parity to be empty of readers. |
| 189 | */ |
| 190 | wait_for_quiescent_state(); /* Wait readers in parity 0 */ |
| 191 | |
| 192 | /* |
| 193 | * Must finish waiting for quiescent state for parity 0 before |
| 194 | * committing qparity update to memory. Failure to do so could result in |
| 195 | * the writer waiting forever while new readers are always accessing |
| 196 | * data (no progress). |
| 197 | * Ensured by STORE_SHARED and LOAD_SHARED. |
| 198 | */ |
| 199 | |
| 200 | switch_next_urcu_qparity(); /* 1 -> 0 */ |
| 201 | |
| 202 | /* |
| 203 | * Must commit qparity update to memory before waiting for parity |
| 204 | * 1 quiescent state. Failure to do so could result in the writer |
| 205 | * waiting forever while new readers are always accessing data (no |
| 206 | * progress). |
| 207 | * Ensured by STORE_SHARED and LOAD_SHARED. |
| 208 | */ |
| 209 | |
| 210 | /* |
| 211 | * Wait for previous parity to be empty of readers. |
| 212 | */ |
| 213 | wait_for_quiescent_state(); /* Wait readers in parity 1 */ |
| 214 | |
| 215 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
| 216 | |
| 217 | internal_urcu_unlock(); |
| 218 | |
| 219 | /* |
| 220 | * Finish waiting for reader threads before letting the old ptr being |
| 221 | * freed. |
| 222 | */ |
| 223 | if (was_online) |
| 224 | _STORE_SHARED(urcu_reader_status.qs_gp, |
| 225 | LOAD_SHARED(urcu_gp_ctr)); |
| 226 | smp_mb(); |
| 227 | } |
| 228 | #else /* !(BITS_PER_LONG < 64) */ |
| 229 | void synchronize_rcu(void) |
| 230 | { |
| 231 | unsigned long was_online; |
| 232 | |
| 233 | was_online = urcu_reader_status.qs_gp; |
| 234 | |
| 235 | /* |
| 236 | * Mark the writer thread offline to make sure we don't wait for |
| 237 | * our own quiescent state. This allows using synchronize_rcu() in |
| 238 | * threads registered as readers. |
| 239 | */ |
| 240 | smp_mb(); |
| 241 | if (was_online) |
| 242 | STORE_SHARED(urcu_reader_status.qs_gp, 0); |
| 243 | |
| 244 | internal_urcu_lock(); |
| 245 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
| 246 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr + RCU_GP_CTR); |
| 247 | wait_for_quiescent_state(); |
| 248 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
| 249 | internal_urcu_unlock(); |
| 250 | |
| 251 | if (was_online) |
| 252 | _STORE_SHARED(urcu_reader_status.qs_gp, |
| 253 | LOAD_SHARED(urcu_gp_ctr)); |
| 254 | smp_mb(); |
| 255 | } |
| 256 | #endif /* !(BITS_PER_LONG < 64) */ |
| 257 | |
| 258 | /* |
| 259 | * library wrappers to be used by non-LGPL compatible source code. |
| 260 | */ |
| 261 | |
| 262 | void rcu_read_lock(void) |
| 263 | { |
| 264 | _rcu_read_lock(); |
| 265 | } |
| 266 | |
| 267 | void rcu_read_unlock(void) |
| 268 | { |
| 269 | _rcu_read_unlock(); |
| 270 | } |
| 271 | |
| 272 | void *rcu_dereference(void *p) |
| 273 | { |
| 274 | return _rcu_dereference(p); |
| 275 | } |
| 276 | |
| 277 | void *rcu_assign_pointer_sym(void **p, void *v) |
| 278 | { |
| 279 | wmb(); |
| 280 | return STORE_SHARED(p, v); |
| 281 | } |
| 282 | |
| 283 | void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new) |
| 284 | { |
| 285 | wmb(); |
| 286 | return cmpxchg(p, old, _new); |
| 287 | } |
| 288 | |
| 289 | void *rcu_xchg_pointer_sym(void **p, void *v) |
| 290 | { |
| 291 | wmb(); |
| 292 | return xchg(p, v); |
| 293 | } |
| 294 | |
| 295 | void *rcu_publish_content_sym(void **p, void *v) |
| 296 | { |
| 297 | void *oldptr; |
| 298 | |
| 299 | oldptr = _rcu_xchg_pointer(p, v); |
| 300 | synchronize_rcu(); |
| 301 | return oldptr; |
| 302 | } |
| 303 | |
| 304 | void rcu_quiescent_state(void) |
| 305 | { |
| 306 | _rcu_quiescent_state(); |
| 307 | } |
| 308 | |
| 309 | void rcu_thread_offline(void) |
| 310 | { |
| 311 | _rcu_thread_offline(); |
| 312 | } |
| 313 | |
| 314 | void rcu_thread_online(void) |
| 315 | { |
| 316 | _rcu_thread_online(); |
| 317 | } |
| 318 | |
| 319 | static void rcu_add_reader(pthread_t id) |
| 320 | { |
| 321 | struct reader_registry *oldarray; |
| 322 | |
| 323 | if (!registry) { |
| 324 | alloc_readers = INIT_NUM_THREADS; |
| 325 | num_readers = 0; |
| 326 | registry = |
| 327 | malloc(sizeof(struct reader_registry) * alloc_readers); |
| 328 | } |
| 329 | if (alloc_readers < num_readers + 1) { |
| 330 | oldarray = registry; |
| 331 | registry = malloc(sizeof(struct reader_registry) |
| 332 | * (alloc_readers << 1)); |
| 333 | memcpy(registry, oldarray, |
| 334 | sizeof(struct reader_registry) * alloc_readers); |
| 335 | alloc_readers <<= 1; |
| 336 | free(oldarray); |
| 337 | } |
| 338 | registry[num_readers].tid = id; |
| 339 | /* reference to the TLS of _this_ reader thread. */ |
| 340 | registry[num_readers].urcu_reader_status = &urcu_reader_status; |
| 341 | num_readers++; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Never shrink (implementation limitation). |
| 346 | * This is O(nb threads). Eventually use a hash table. |
| 347 | */ |
| 348 | static void rcu_remove_reader(pthread_t id) |
| 349 | { |
| 350 | struct reader_registry *index; |
| 351 | |
| 352 | assert(registry != NULL); |
| 353 | for (index = registry; index < registry + num_readers; index++) { |
| 354 | if (pthread_equal(index->tid, id)) { |
| 355 | memcpy(index, ®istry[num_readers - 1], |
| 356 | sizeof(struct reader_registry)); |
| 357 | registry[num_readers - 1].tid = 0; |
| 358 | registry[num_readers - 1].urcu_reader_status = NULL; |
| 359 | num_readers--; |
| 360 | return; |
| 361 | } |
| 362 | } |
| 363 | /* Hrm not found, forgot to register ? */ |
| 364 | assert(0); |
| 365 | } |
| 366 | |
| 367 | void rcu_register_thread(void) |
| 368 | { |
| 369 | internal_urcu_lock(); |
| 370 | rcu_add_reader(pthread_self()); |
| 371 | internal_urcu_unlock(); |
| 372 | _rcu_thread_online(); |
| 373 | } |
| 374 | |
| 375 | void rcu_unregister_thread(void) |
| 376 | { |
| 377 | /* |
| 378 | * We have to make the thread offline otherwise we end up dealocking |
| 379 | * with a waiting writer. |
| 380 | */ |
| 381 | _rcu_thread_offline(); |
| 382 | internal_urcu_lock(); |
| 383 | rcu_remove_reader(pthread_self()); |
| 384 | internal_urcu_unlock(); |
| 385 | } |