Commit | Line | Data |
---|---|---|
d3d3857f MJ |
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 | ||
fdee2e6d MD |
6 | #ifndef _URCU_BP_STATIC_H |
7 | #define _URCU_BP_STATIC_H | |
8 | ||
9 | /* | |
fdee2e6d MD |
10 | * Userspace RCU header. |
11 | * | |
a5a9f428 PM |
12 | * TO BE INCLUDED ONLY IN CODE THAT IS TO BE RECOMPILED ON EACH LIBURCU |
13 | * RELEASE. See urcu.h for linking dynamically with the userspace rcu library. | |
fdee2e6d | 14 | * |
fdee2e6d MD |
15 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
16 | */ | |
17 | ||
18 | #include <stdlib.h> | |
19 | #include <pthread.h> | |
fdee2e6d MD |
20 | #include <unistd.h> |
21 | ||
601922a8 | 22 | #include <urcu/annotate.h> |
01477510 | 23 | #include <urcu/debug.h> |
d8d9a340 | 24 | #include <urcu/config.h> |
fdee2e6d MD |
25 | #include <urcu/compiler.h> |
26 | #include <urcu/arch.h> | |
27 | #include <urcu/system.h> | |
a2e7bf9c | 28 | #include <urcu/uatomic.h> |
fdee2e6d | 29 | #include <urcu/list.h> |
bd252a04 | 30 | #include <urcu/tls-compat.h> |
fdee2e6d MD |
31 | |
32 | /* | |
33 | * This code section can only be included in LGPL 2.1 compatible source code. | |
34 | * See below for the function call wrappers which can be used in code meant to | |
35 | * be only linked with the Userspace RCU library. This comes with a small | |
36 | * performance degradation on the read-side due to the added function calls. | |
37 | * This is required to permit relinking with newer versions of the library. | |
38 | */ | |
39 | ||
36bc70a8 MD |
40 | #ifdef __cplusplus |
41 | extern "C" { | |
42 | #endif | |
43 | ||
4477a870 MD |
44 | enum urcu_bp_state { |
45 | URCU_BP_READER_ACTIVE_CURRENT, | |
46 | URCU_BP_READER_ACTIVE_OLD, | |
47 | URCU_BP_READER_INACTIVE, | |
52c75091 MD |
48 | }; |
49 | ||
fdee2e6d | 50 | /* |
4477a870 | 51 | * The trick here is that URCU_BP_GP_CTR_PHASE must be a multiple of 8 so we can use a |
fdee2e6d MD |
52 | * full 8-bits, 16-bits or 32-bits bitmask for the lower order bits. |
53 | */ | |
4477a870 | 54 | #define URCU_BP_GP_COUNT (1UL << 0) |
fdee2e6d | 55 | /* Use the amount of bits equal to half of the architecture long size */ |
4477a870 MD |
56 | #define URCU_BP_GP_CTR_PHASE (1UL << (sizeof(long) << 2)) |
57 | #define URCU_BP_GP_CTR_NEST_MASK (URCU_BP_GP_CTR_PHASE - 1) | |
fdee2e6d MD |
58 | |
59 | /* | |
4477a870 | 60 | * Used internally by _urcu_bp_read_lock. |
fdee2e6d | 61 | */ |
4477a870 | 62 | extern void urcu_bp_register(void); |
fdee2e6d | 63 | |
4477a870 | 64 | struct urcu_bp_gp { |
c13c2e55 MD |
65 | /* |
66 | * Global grace period counter. | |
4477a870 MD |
67 | * Contains the current URCU_BP_GP_CTR_PHASE. |
68 | * Also has a URCU_BP_GP_COUNT of 1, to accelerate the reader fast path. | |
c13c2e55 MD |
69 | * Written to only by writer with mutex taken. |
70 | * Read by both writer and readers. | |
71 | */ | |
72 | unsigned long ctr; | |
73 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); | |
74 | ||
4477a870 | 75 | extern struct urcu_bp_gp urcu_bp_gp; |
fdee2e6d | 76 | |
4477a870 MD |
77 | struct urcu_bp_reader { |
78 | /* Data used by both reader and urcu_bp_synchronize_rcu() */ | |
52c75091 | 79 | unsigned long ctr; |
fdee2e6d | 80 | /* Data used for registry */ |
16aa9ee8 | 81 | struct cds_list_head node __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
fdee2e6d MD |
82 | pthread_t tid; |
83 | int alloc; /* registry entry allocated */ | |
84 | }; | |
85 | ||
86 | /* | |
87 | * Bulletproof version keeps a pointer to a registry not part of the TLS. | |
88 | * Adds a pointer dereference on the read-side, but won't require to unregister | |
89 | * the reader thread. | |
90 | */ | |
4477a870 | 91 | extern DECLARE_URCU_TLS(struct urcu_bp_reader *, urcu_bp_reader); |
fdee2e6d | 92 | |
d8d9a340 MD |
93 | #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER |
94 | #define urcu_bp_has_sys_membarrier 1 | |
95 | #else | |
f541831e | 96 | extern int urcu_bp_has_sys_membarrier; |
d8d9a340 | 97 | #endif |
f541831e MD |
98 | |
99 | static inline void urcu_bp_smp_mb_slave(void) | |
100 | { | |
101 | if (caa_likely(urcu_bp_has_sys_membarrier)) | |
102 | cmm_barrier(); | |
103 | else | |
104 | cmm_smp_mb(); | |
105 | } | |
106 | ||
601922a8 OD |
107 | static inline enum urcu_bp_state urcu_bp_reader_state(unsigned long *ctr, |
108 | cmm_annotate_t *group) | |
fdee2e6d | 109 | { |
52c75091 | 110 | unsigned long v; |
fdee2e6d | 111 | |
52c75091 | 112 | if (ctr == NULL) |
4477a870 | 113 | return URCU_BP_READER_INACTIVE; |
fdee2e6d MD |
114 | /* |
115 | * Make sure both tests below are done on the same version of *value | |
116 | * to insure consistency. | |
117 | */ | |
601922a8 OD |
118 | v = uatomic_load(ctr, CMM_RELAXED); |
119 | cmm_annotate_group_mem_acquire(group, ctr); | |
120 | ||
4477a870 MD |
121 | if (!(v & URCU_BP_GP_CTR_NEST_MASK)) |
122 | return URCU_BP_READER_INACTIVE; | |
123 | if (!((v ^ urcu_bp_gp.ctr) & URCU_BP_GP_CTR_PHASE)) | |
124 | return URCU_BP_READER_ACTIVE_CURRENT; | |
125 | return URCU_BP_READER_ACTIVE_OLD; | |
fdee2e6d MD |
126 | } |
127 | ||
a5a9f428 | 128 | /* |
4477a870 | 129 | * Helper for _urcu_bp_read_lock(). The format of urcu_bp_gp.ctr (as well as |
be152bdb LKO |
130 | * the per-thread rcu_reader.ctr) has the lower-order bits containing a count of |
131 | * _urcu_bp_read_lock() nesting, and a single high-order URCU_BP_GP_CTR_PHASE bit | |
132 | * that contains either zero or one. The smp_mb_slave() ensures that the accesses in | |
4477a870 | 133 | * _urcu_bp_read_lock() happen before the subsequent read-side critical section. |
a5a9f428 | 134 | */ |
4477a870 | 135 | static inline void _urcu_bp_read_lock_update(unsigned long tmp) |
a5a9f428 | 136 | { |
4477a870 MD |
137 | if (caa_likely(!(tmp & URCU_BP_GP_CTR_NEST_MASK))) { |
138 | _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader)->ctr, _CMM_LOAD_SHARED(urcu_bp_gp.ctr)); | |
f541831e | 139 | urcu_bp_smp_mb_slave(); |
a5a9f428 | 140 | } else |
4477a870 | 141 | _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader)->ctr, tmp + URCU_BP_GP_COUNT); |
a5a9f428 PM |
142 | } |
143 | ||
144 | /* | |
145 | * Enter an RCU read-side critical section. | |
146 | * | |
147 | * The first cmm_barrier() call ensures that the compiler does not reorder | |
4477a870 | 148 | * the body of _urcu_bp_read_lock() with a mutex. |
a5a9f428 PM |
149 | * |
150 | * This function and its helper are both less than 10 lines long. The | |
151 | * intent is that this function meets the 10-line criterion in LGPL, | |
152 | * allowing this function to be invoked directly from non-LGPL code. | |
153 | */ | |
4477a870 | 154 | static inline void _urcu_bp_read_lock(void) |
fdee2e6d | 155 | { |
52c75091 | 156 | unsigned long tmp; |
fdee2e6d | 157 | |
4477a870 MD |
158 | if (caa_unlikely(!URCU_TLS(urcu_bp_reader))) |
159 | urcu_bp_register(); /* If not yet registered. */ | |
5481ddb3 | 160 | cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */ |
4477a870 | 161 | tmp = URCU_TLS(urcu_bp_reader)->ctr; |
2a27e931 | 162 | urcu_assert_debug((tmp & URCU_BP_GP_CTR_NEST_MASK) != URCU_BP_GP_CTR_NEST_MASK); |
4477a870 | 163 | _urcu_bp_read_lock_update(tmp); |
fdee2e6d MD |
164 | } |
165 | ||
a5a9f428 PM |
166 | /* |
167 | * Exit an RCU read-side critical section. This function is less than | |
168 | * 10 lines of code, and is intended to be usable by non-LGPL code, as | |
169 | * called out in LGPL. | |
170 | */ | |
4477a870 | 171 | static inline void _urcu_bp_read_unlock(void) |
fdee2e6d | 172 | { |
343c8b13 | 173 | unsigned long tmp; |
601922a8 | 174 | unsigned long *ctr = &URCU_TLS(urcu_bp_reader)->ctr; |
343c8b13 | 175 | |
4477a870 | 176 | tmp = URCU_TLS(urcu_bp_reader)->ctr; |
2a27e931 | 177 | urcu_assert_debug(tmp & URCU_BP_GP_CTR_NEST_MASK); |
343c8b13 | 178 | /* Finish using rcu before decrementing the pointer. */ |
f541831e | 179 | urcu_bp_smp_mb_slave(); |
601922a8 OD |
180 | cmm_annotate_mem_release(ctr); |
181 | uatomic_store(ctr, tmp - URCU_BP_GP_COUNT, CMM_RELAXED); | |
5481ddb3 | 182 | cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */ |
fdee2e6d MD |
183 | } |
184 | ||
882f3357 MD |
185 | /* |
186 | * Returns whether within a RCU read-side critical section. | |
187 | * | |
188 | * This function is less than 10 lines long. The intent is that this | |
189 | * function meets the 10-line criterion for LGPL, allowing this function | |
190 | * to be invoked directly from non-LGPL code. | |
191 | */ | |
4477a870 | 192 | static inline int _urcu_bp_read_ongoing(void) |
882f3357 | 193 | { |
4477a870 MD |
194 | if (caa_unlikely(!URCU_TLS(urcu_bp_reader))) |
195 | urcu_bp_register(); /* If not yet registered. */ | |
196 | return URCU_TLS(urcu_bp_reader)->ctr & URCU_BP_GP_CTR_NEST_MASK; | |
882f3357 MD |
197 | } |
198 | ||
67ecffc0 | 199 | #ifdef __cplusplus |
36bc70a8 MD |
200 | } |
201 | #endif | |
202 | ||
fdee2e6d | 203 | #endif /* _URCU_BP_STATIC_H */ |