c60e4e0eb1c272b9dbf5db9691c7e2fce9cd06d0
[urcu.git] / tests / common / debug-yield.h
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 #define RCU_YIELD_READ (1 << 0)
35 #define RCU_YIELD_WRITE (1 << 1)
36
37 /*
38 * Updates with RCU_SIGNAL are much slower. Account this in the delay.
39 */
40 #ifdef RCU_SIGNAL
41 /* maximum sleep delay, in us */
42 #define MAX_SLEEP 30000
43 #else
44 #define MAX_SLEEP 50
45 #endif
46
47 extern unsigned int rcu_yield_active;
48 extern DECLARE_URCU_TLS(unsigned int, rcu_rand_yield);
49
50 #ifdef DEBUG_YIELD
51 static inline void rcu_debug_yield_read(void)
52 {
53 if (rcu_yield_active & RCU_YIELD_READ)
54 if (rand_r(&URCU_TLS(rcu_rand_yield)) & 0x1)
55 usleep(rand_r(&URCU_TLS(rcu_rand_yield)) % MAX_SLEEP);
56 }
57
58 static inline void rcu_debug_yield_write(void)
59 {
60 if (rcu_yield_active & RCU_YIELD_WRITE)
61 if (rand_r(&URCU_TLS(rcu_rand_yield)) & 0x1)
62 usleep(rand_r(&URCU_TLS(rcu_rand_yield)) % MAX_SLEEP);
63 }
64
65 static inline void rcu_debug_yield_enable(unsigned int flags)
66 {
67 rcu_yield_active |= flags;
68 }
69
70 static inline void rcu_debug_yield_disable(unsigned int flags)
71 {
72 rcu_yield_active &= ~flags;
73 }
74
75 static inline void rcu_debug_yield_init(void)
76 {
77 URCU_TLS(rcu_rand_yield) = time(NULL) ^ (unsigned long) pthread_self();
78 }
79 #else /* DEBUG_YIELD */
80 static inline void rcu_debug_yield_read(void)
81 {
82 }
83
84 static inline void rcu_debug_yield_write(void)
85 {
86 }
87
88 static inline void rcu_debug_yield_enable(unsigned int flags)
89 {
90 }
91
92 static inline void rcu_debug_yield_disable(unsigned int flags)
93 {
94 }
95
96 static inline void rcu_debug_yield_init(void)
97 {
98 }
99 #endif
100
101 #endif /* URCU_TESTS_DEBUG_YIELD_H */
This page took 0.040723 seconds and 3 git commands to generate.