| 1 | #ifndef _URCU_STATIC_H |
| 2 | #define _URCU_STATIC_H |
| 3 | |
| 4 | /* |
| 5 | * urcu-static.h |
| 6 | * |
| 7 | * Userspace RCU header. |
| 8 | * |
| 9 | * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu.h for linking |
| 10 | * dynamically with the userspace rcu library. |
| 11 | * |
| 12 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
| 13 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. |
| 14 | * |
| 15 | * This library is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU Lesser General Public |
| 17 | * License as published by the Free Software Foundation; either |
| 18 | * version 2.1 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * This library is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | * Lesser General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU Lesser General Public |
| 26 | * License along with this library; if not, write to the Free Software |
| 27 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 | * |
| 29 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 30 | */ |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include <pthread.h> |
| 34 | #include <syscall.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include <urcu/compiler.h> |
| 38 | #include <urcu/arch.h> |
| 39 | #include <urcu/list.h> |
| 40 | |
| 41 | /* |
| 42 | * Identify a shared load. A smp_rmc() or smp_mc() should come before the load. |
| 43 | */ |
| 44 | #define _LOAD_SHARED(p) ACCESS_ONCE(p) |
| 45 | |
| 46 | /* |
| 47 | * Load a data from shared memory, doing a cache flush if required. |
| 48 | */ |
| 49 | #define LOAD_SHARED(p) \ |
| 50 | ({ \ |
| 51 | smp_rmc(); \ |
| 52 | _LOAD_SHARED(p); \ |
| 53 | }) |
| 54 | |
| 55 | /* |
| 56 | * Identify a shared store. A smp_wmc() or smp_mc() should follow the store. |
| 57 | */ |
| 58 | #define _STORE_SHARED(x, v) ({ ACCESS_ONCE(x) = (v); }) |
| 59 | |
| 60 | /* |
| 61 | * Store v into x, where x is located in shared memory. Performs the required |
| 62 | * cache flush after writing. Returns v. |
| 63 | */ |
| 64 | #define STORE_SHARED(x, v) \ |
| 65 | ({ \ |
| 66 | _STORE_SHARED(x, v); \ |
| 67 | smp_wmc(); \ |
| 68 | (v); \ |
| 69 | }) |
| 70 | |
| 71 | /** |
| 72 | * _rcu_dereference - reads (copy) a RCU-protected pointer to a local variable |
| 73 | * into a RCU read-side critical section. The pointer can later be safely |
| 74 | * dereferenced within the critical section. |
| 75 | * |
| 76 | * This ensures that the pointer copy is invariant thorough the whole critical |
| 77 | * section. |
| 78 | * |
| 79 | * Inserts memory barriers on architectures that require them (currently only |
| 80 | * Alpha) and documents which pointers are protected by RCU. |
| 81 | * |
| 82 | * The compiler memory barrier in LOAD_SHARED() ensures that value-speculative |
| 83 | * optimizations (e.g. VSS: Value Speculation Scheduling) does not perform the |
| 84 | * data read before the pointer read by speculating the value of the pointer. |
| 85 | * Correct ordering is ensured because the pointer is read as a volatile access. |
| 86 | * This acts as a global side-effect operation, which forbids reordering of |
| 87 | * dependent memory operations. Note that such concern about dependency-breaking |
| 88 | * optimizations will eventually be taken care of by the "memory_order_consume" |
| 89 | * addition to forthcoming C++ standard. |
| 90 | * |
| 91 | * Should match rcu_assign_pointer() or rcu_xchg_pointer(). |
| 92 | */ |
| 93 | |
| 94 | #define _rcu_dereference(p) ({ \ |
| 95 | typeof(p) _________p1 = LOAD_SHARED(p); \ |
| 96 | smp_read_barrier_depends(); \ |
| 97 | (_________p1); \ |
| 98 | }) |
| 99 | |
| 100 | #define futex(...) syscall(__NR_futex, __VA_ARGS__) |
| 101 | #define FUTEX_WAIT 0 |
| 102 | #define FUTEX_WAKE 1 |
| 103 | |
| 104 | /* |
| 105 | * This code section can only be included in LGPL 2.1 compatible source code. |
| 106 | * See below for the function call wrappers which can be used in code meant to |
| 107 | * be only linked with the Userspace RCU library. This comes with a small |
| 108 | * performance degradation on the read-side due to the added function calls. |
| 109 | * This is required to permit relinking with newer versions of the library. |
| 110 | */ |
| 111 | |
| 112 | /* |
| 113 | * The signal number used by the RCU library can be overridden with |
| 114 | * -DSIGURCU= when compiling the library. |
| 115 | */ |
| 116 | #ifndef SIGURCU |
| 117 | #define SIGURCU SIGUSR1 |
| 118 | #endif |
| 119 | |
| 120 | /* |
| 121 | * If a reader is really non-cooperative and refuses to commit its |
| 122 | * urcu_active_readers count to memory (there is no barrier in the reader |
| 123 | * per-se), kick it after a few loops waiting for it. |
| 124 | */ |
| 125 | #define KICK_READER_LOOPS 10000 |
| 126 | |
| 127 | /* |
| 128 | * Active attempts to check for reader Q.S. before calling futex(). |
| 129 | */ |
| 130 | #define RCU_QS_ACTIVE_ATTEMPTS 100 |
| 131 | |
| 132 | #ifdef DEBUG_RCU |
| 133 | #define rcu_assert(args...) assert(args) |
| 134 | #else |
| 135 | #define rcu_assert(args...) |
| 136 | #endif |
| 137 | |
| 138 | #ifdef DEBUG_YIELD |
| 139 | #include <sched.h> |
| 140 | #include <time.h> |
| 141 | #include <pthread.h> |
| 142 | #include <unistd.h> |
| 143 | |
| 144 | #define YIELD_READ (1 << 0) |
| 145 | #define YIELD_WRITE (1 << 1) |
| 146 | |
| 147 | /* |
| 148 | * Updates without URCU_MB are much slower. Account this in |
| 149 | * the delay. |
| 150 | */ |
| 151 | #ifdef URCU_MB |
| 152 | /* maximum sleep delay, in us */ |
| 153 | #define MAX_SLEEP 50 |
| 154 | #else |
| 155 | #define MAX_SLEEP 30000 |
| 156 | #endif |
| 157 | |
| 158 | extern unsigned int yield_active; |
| 159 | extern unsigned int __thread rand_yield; |
| 160 | |
| 161 | static inline void debug_yield_read(void) |
| 162 | { |
| 163 | if (yield_active & YIELD_READ) |
| 164 | if (rand_r(&rand_yield) & 0x1) |
| 165 | usleep(rand_r(&rand_yield) % MAX_SLEEP); |
| 166 | } |
| 167 | |
| 168 | static inline void debug_yield_write(void) |
| 169 | { |
| 170 | if (yield_active & YIELD_WRITE) |
| 171 | if (rand_r(&rand_yield) & 0x1) |
| 172 | usleep(rand_r(&rand_yield) % MAX_SLEEP); |
| 173 | } |
| 174 | |
| 175 | static inline void debug_yield_init(void) |
| 176 | { |
| 177 | rand_yield = time(NULL) ^ pthread_self(); |
| 178 | } |
| 179 | #else |
| 180 | static inline void debug_yield_read(void) |
| 181 | { |
| 182 | } |
| 183 | |
| 184 | static inline void debug_yield_write(void) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | static inline void debug_yield_init(void) |
| 189 | { |
| 190 | |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | #ifdef URCU_MB |
| 195 | static inline void reader_barrier() |
| 196 | { |
| 197 | smp_mb(); |
| 198 | } |
| 199 | #else |
| 200 | static inline void reader_barrier() |
| 201 | { |
| 202 | barrier(); |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | /* |
| 207 | * The trick here is that RCU_GP_CTR_BIT must be a multiple of 8 so we can use a |
| 208 | * full 8-bits, 16-bits or 32-bits bitmask for the lower order bits. |
| 209 | */ |
| 210 | #define RCU_GP_COUNT (1UL << 0) |
| 211 | /* Use the amount of bits equal to half of the architecture long size */ |
| 212 | #define RCU_GP_CTR_BIT (1UL << (sizeof(long) << 2)) |
| 213 | #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1) |
| 214 | |
| 215 | /* |
| 216 | * Global quiescent period counter with low-order bits unused. |
| 217 | * Using a int rather than a char to eliminate false register dependencies |
| 218 | * causing stalls on some architectures. |
| 219 | */ |
| 220 | extern long urcu_gp_ctr; |
| 221 | |
| 222 | struct urcu_reader { |
| 223 | /* Data used by both reader and synchronize_rcu() */ |
| 224 | long ctr; |
| 225 | char need_mb; |
| 226 | /* Data used for registry */ |
| 227 | struct list_head head __attribute__((aligned(CACHE_LINE_SIZE))); |
| 228 | pthread_t tid; |
| 229 | }; |
| 230 | |
| 231 | extern struct urcu_reader __thread urcu_reader; |
| 232 | |
| 233 | extern int gp_futex; |
| 234 | |
| 235 | /* |
| 236 | * Wake-up waiting synchronize_rcu(). Called from many concurrent threads. |
| 237 | */ |
| 238 | static inline void wake_up_gp(void) |
| 239 | { |
| 240 | if (unlikely(uatomic_read(&gp_futex) == -1)) { |
| 241 | uatomic_set(&gp_futex, 0); |
| 242 | futex(&gp_futex, FUTEX_WAKE, 1, |
| 243 | NULL, NULL, 0); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static inline int rcu_old_gp_ongoing(long *value) |
| 248 | { |
| 249 | long v; |
| 250 | |
| 251 | if (value == NULL) |
| 252 | return 0; |
| 253 | /* |
| 254 | * Make sure both tests below are done on the same version of *value |
| 255 | * to insure consistency. |
| 256 | */ |
| 257 | v = LOAD_SHARED(*value); |
| 258 | return (v & RCU_GP_CTR_NEST_MASK) && |
| 259 | ((v ^ urcu_gp_ctr) & RCU_GP_CTR_BIT); |
| 260 | } |
| 261 | |
| 262 | static inline void _rcu_read_lock(void) |
| 263 | { |
| 264 | long tmp; |
| 265 | |
| 266 | tmp = urcu_reader.ctr; |
| 267 | /* urcu_gp_ctr = RCU_GP_COUNT | (~RCU_GP_CTR_BIT or RCU_GP_CTR_BIT) */ |
| 268 | if (likely(!(tmp & RCU_GP_CTR_NEST_MASK))) { |
| 269 | _STORE_SHARED(urcu_reader.ctr, _LOAD_SHARED(urcu_gp_ctr)); |
| 270 | /* |
| 271 | * Set active readers count for outermost nesting level before |
| 272 | * accessing the pointer. See force_mb_all_threads(). |
| 273 | */ |
| 274 | reader_barrier(); |
| 275 | } else { |
| 276 | _STORE_SHARED(urcu_reader.ctr, tmp + RCU_GP_COUNT); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static inline void _rcu_read_unlock(void) |
| 281 | { |
| 282 | long tmp; |
| 283 | |
| 284 | tmp = urcu_reader.ctr; |
| 285 | /* |
| 286 | * Finish using rcu before decrementing the pointer. |
| 287 | * See force_mb_all_threads(). |
| 288 | */ |
| 289 | if (likely((tmp & RCU_GP_CTR_NEST_MASK) == RCU_GP_COUNT)) { |
| 290 | reader_barrier(); |
| 291 | _STORE_SHARED(urcu_reader.ctr, urcu_reader.ctr - RCU_GP_COUNT); |
| 292 | /* write urcu_reader.ctr before read futex */ |
| 293 | reader_barrier(); |
| 294 | wake_up_gp(); |
| 295 | } else { |
| 296 | _STORE_SHARED(urcu_reader.ctr, urcu_reader.ctr - RCU_GP_COUNT); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * _rcu_assign_pointer - assign (publicize) a pointer to a new data structure |
| 302 | * meant to be read by RCU read-side critical sections. Returns the assigned |
| 303 | * value. |
| 304 | * |
| 305 | * Documents which pointers will be dereferenced by RCU read-side critical |
| 306 | * sections and adds the required memory barriers on architectures requiring |
| 307 | * them. It also makes sure the compiler does not reorder code initializing the |
| 308 | * data structure before its publication. |
| 309 | * |
| 310 | * Should match rcu_dereference_pointer(). |
| 311 | */ |
| 312 | |
| 313 | #define _rcu_assign_pointer(p, v) \ |
| 314 | ({ \ |
| 315 | if (!__builtin_constant_p(v) || \ |
| 316 | ((v) != NULL)) \ |
| 317 | wmb(); \ |
| 318 | STORE_SHARED(p, v); \ |
| 319 | }) |
| 320 | |
| 321 | /** |
| 322 | * _rcu_cmpxchg_pointer - same as rcu_assign_pointer, but tests if the pointer |
| 323 | * is as expected by "old". If succeeds, returns the previous pointer to the |
| 324 | * data structure, which can be safely freed after waiting for a quiescent state |
| 325 | * using synchronize_rcu(). If fails (unexpected value), returns old (which |
| 326 | * should not be freed !). |
| 327 | */ |
| 328 | |
| 329 | #define _rcu_cmpxchg_pointer(p, old, _new) \ |
| 330 | ({ \ |
| 331 | if (!__builtin_constant_p(_new) || \ |
| 332 | ((_new) != NULL)) \ |
| 333 | wmb(); \ |
| 334 | uatomic_cmpxchg(p, old, _new); \ |
| 335 | }) |
| 336 | |
| 337 | /** |
| 338 | * _rcu_xchg_pointer - same as rcu_assign_pointer, but returns the previous |
| 339 | * pointer to the data structure, which can be safely freed after waiting for a |
| 340 | * quiescent state using synchronize_rcu(). |
| 341 | */ |
| 342 | |
| 343 | #define _rcu_xchg_pointer(p, v) \ |
| 344 | ({ \ |
| 345 | if (!__builtin_constant_p(v) || \ |
| 346 | ((v) != NULL)) \ |
| 347 | wmb(); \ |
| 348 | uatomic_xchg(p, v); \ |
| 349 | }) |
| 350 | |
| 351 | /* |
| 352 | * Exchanges the pointer and waits for quiescent state. |
| 353 | * The pointer returned can be freed. |
| 354 | */ |
| 355 | #define _rcu_publish_content(p, v) \ |
| 356 | ({ \ |
| 357 | void *oldptr; \ |
| 358 | oldptr = _rcu_xchg_pointer(p, v); \ |
| 359 | synchronize_rcu(); \ |
| 360 | oldptr; \ |
| 361 | }) |
| 362 | |
| 363 | #endif /* _URCU_STATIC_H */ |