| 1 | // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 2 | // SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation. |
| 3 | // |
| 4 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | |
| 6 | #ifndef URCU_TESTS_DEBUG_YIELD_H |
| 7 | #define URCU_TESTS_DEBUG_YIELD_H |
| 8 | |
| 9 | /* |
| 10 | * Userspace RCU library tests - Debugging header |
| 11 | * |
| 12 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 13 | */ |
| 14 | |
| 15 | #include <sched.h> |
| 16 | #include <time.h> |
| 17 | #include <pthread.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <compat-rand.h> |
| 21 | |
| 22 | #define RCU_YIELD_READ (1 << 0) |
| 23 | #define RCU_YIELD_WRITE (1 << 1) |
| 24 | |
| 25 | #define MAX_SLEEP 50 |
| 26 | |
| 27 | extern unsigned int rcu_yield_active; |
| 28 | extern DECLARE_URCU_TLS(unsigned int, rcu_rand_yield); |
| 29 | |
| 30 | #ifdef DEBUG_YIELD |
| 31 | static inline void rcu_debug_yield_read(void) |
| 32 | { |
| 33 | if (rcu_yield_active & RCU_YIELD_READ) |
| 34 | if (rand_r(&URCU_TLS(rcu_rand_yield)) & 0x1) |
| 35 | usleep(rand_r(&URCU_TLS(rcu_rand_yield)) % MAX_SLEEP); |
| 36 | } |
| 37 | |
| 38 | static inline void rcu_debug_yield_write(void) |
| 39 | { |
| 40 | if (rcu_yield_active & RCU_YIELD_WRITE) |
| 41 | if (rand_r(&URCU_TLS(rcu_rand_yield)) & 0x1) |
| 42 | usleep(rand_r(&URCU_TLS(rcu_rand_yield)) % MAX_SLEEP); |
| 43 | } |
| 44 | |
| 45 | static inline void rcu_debug_yield_enable(unsigned int flags) |
| 46 | { |
| 47 | rcu_yield_active |= flags; |
| 48 | } |
| 49 | |
| 50 | static inline void rcu_debug_yield_disable(unsigned int flags) |
| 51 | { |
| 52 | rcu_yield_active &= ~flags; |
| 53 | } |
| 54 | |
| 55 | static inline void rcu_debug_yield_init(void) |
| 56 | { |
| 57 | URCU_TLS(rcu_rand_yield) = time(NULL) ^ (unsigned long) pthread_self(); |
| 58 | } |
| 59 | #else /* DEBUG_YIELD */ |
| 60 | static inline void rcu_debug_yield_read(void) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | static inline void rcu_debug_yield_write(void) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | static inline void rcu_debug_yield_enable( |
| 69 | unsigned int flags __attribute__((unused))) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | static inline void rcu_debug_yield_disable( |
| 74 | unsigned int flags __attribute__((unused))) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | static inline void rcu_debug_yield_init(void) |
| 79 | { |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | #endif /* URCU_TESTS_DEBUG_YIELD_H */ |