| 1 | #ifndef URCU_TESTS_DEBUG_YIELD_H |
| 2 | #define URCU_TESTS_DEBUG_YIELD_H |
| 3 | |
| 4 | /* |
| 5 | * debug-yield.h |
| 6 | * |
| 7 | * Userspace RCU library tests - Debugging header |
| 8 | * |
| 9 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | * |
| 26 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 27 | */ |
| 28 | |
| 29 | #include <sched.h> |
| 30 | #include <time.h> |
| 31 | #include <pthread.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include <compat-rand.h> |
| 35 | |
| 36 | #define RCU_YIELD_READ (1 << 0) |
| 37 | #define RCU_YIELD_WRITE (1 << 1) |
| 38 | |
| 39 | /* |
| 40 | * Updates with RCU_SIGNAL are much slower. Account this in the delay. |
| 41 | */ |
| 42 | #ifdef RCU_SIGNAL |
| 43 | /* maximum sleep delay, in us */ |
| 44 | #define MAX_SLEEP 30000 |
| 45 | #else |
| 46 | #define MAX_SLEEP 50 |
| 47 | #endif |
| 48 | |
| 49 | extern unsigned int rcu_yield_active; |
| 50 | extern DECLARE_URCU_TLS(unsigned int, rcu_rand_yield); |
| 51 | |
| 52 | #ifdef DEBUG_YIELD |
| 53 | static inline void rcu_debug_yield_read(void) |
| 54 | { |
| 55 | if (rcu_yield_active & RCU_YIELD_READ) |
| 56 | if (rand_r(&URCU_TLS(rcu_rand_yield)) & 0x1) |
| 57 | usleep(rand_r(&URCU_TLS(rcu_rand_yield)) % MAX_SLEEP); |
| 58 | } |
| 59 | |
| 60 | static inline void rcu_debug_yield_write(void) |
| 61 | { |
| 62 | if (rcu_yield_active & RCU_YIELD_WRITE) |
| 63 | if (rand_r(&URCU_TLS(rcu_rand_yield)) & 0x1) |
| 64 | usleep(rand_r(&URCU_TLS(rcu_rand_yield)) % MAX_SLEEP); |
| 65 | } |
| 66 | |
| 67 | static inline void rcu_debug_yield_enable(unsigned int flags) |
| 68 | { |
| 69 | rcu_yield_active |= flags; |
| 70 | } |
| 71 | |
| 72 | static inline void rcu_debug_yield_disable(unsigned int flags) |
| 73 | { |
| 74 | rcu_yield_active &= ~flags; |
| 75 | } |
| 76 | |
| 77 | static inline void rcu_debug_yield_init(void) |
| 78 | { |
| 79 | URCU_TLS(rcu_rand_yield) = time(NULL) ^ (unsigned long) pthread_self(); |
| 80 | } |
| 81 | #else /* DEBUG_YIELD */ |
| 82 | static inline void rcu_debug_yield_read(void) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | static inline void rcu_debug_yield_write(void) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | static inline void rcu_debug_yield_enable(unsigned int flags) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | static inline void rcu_debug_yield_disable(unsigned int flags) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | static inline void rcu_debug_yield_init(void) |
| 99 | { |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | #endif /* URCU_TESTS_DEBUG_YIELD_H */ |