1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
4 // SPDX-License-Identifier: LGPL-2.1-or-later
6 #ifndef _URCU_BP_STATIC_H
7 #define _URCU_BP_STATIC_H
10 * Userspace RCU header.
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.
15 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
22 #include <urcu/debug.h>
23 #include <urcu/config.h>
24 #include <urcu/compiler.h>
25 #include <urcu/arch.h>
26 #include <urcu/system.h>
27 #include <urcu/uatomic.h>
28 #include <urcu/list.h>
29 #include <urcu/tls-compat.h>
32 * This code section can only be included in LGPL 2.1 compatible source code.
33 * See below for the function call wrappers which can be used in code meant to
34 * be only linked with the Userspace RCU library. This comes with a small
35 * performance degradation on the read-side due to the added function calls.
36 * This is required to permit relinking with newer versions of the library.
44 URCU_BP_READER_ACTIVE_CURRENT
,
45 URCU_BP_READER_ACTIVE_OLD
,
46 URCU_BP_READER_INACTIVE
,
50 * The trick here is that URCU_BP_GP_CTR_PHASE must be a multiple of 8 so we can use a
51 * full 8-bits, 16-bits or 32-bits bitmask for the lower order bits.
53 #define URCU_BP_GP_COUNT (1UL << 0)
54 /* Use the amount of bits equal to half of the architecture long size */
55 #define URCU_BP_GP_CTR_PHASE (1UL << (sizeof(long) << 2))
56 #define URCU_BP_GP_CTR_NEST_MASK (URCU_BP_GP_CTR_PHASE - 1)
59 * Used internally by _urcu_bp_read_lock.
61 extern void urcu_bp_register(void);
65 * Global grace period counter.
66 * Contains the current URCU_BP_GP_CTR_PHASE.
67 * Also has a URCU_BP_GP_COUNT of 1, to accelerate the reader fast path.
68 * Written to only by writer with mutex taken.
69 * Read by both writer and readers.
72 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
74 extern struct urcu_bp_gp urcu_bp_gp
;
76 struct urcu_bp_reader
{
77 /* Data used by both reader and urcu_bp_synchronize_rcu() */
79 /* Data used for registry */
80 struct cds_list_head node
__attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
82 int alloc
; /* registry entry allocated */
86 * Bulletproof version keeps a pointer to a registry not part of the TLS.
87 * Adds a pointer dereference on the read-side, but won't require to unregister
90 extern DECLARE_URCU_TLS(struct urcu_bp_reader
*, urcu_bp_reader
);
92 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
93 #define urcu_bp_has_sys_membarrier 1
95 extern int urcu_bp_has_sys_membarrier
;
98 static inline void urcu_bp_smp_mb_slave(void)
100 if (caa_likely(urcu_bp_has_sys_membarrier
))
106 static inline enum urcu_bp_state
urcu_bp_reader_state(unsigned long *ctr
)
111 return URCU_BP_READER_INACTIVE
;
113 * Make sure both tests below are done on the same version of *value
114 * to insure consistency.
116 v
= CMM_LOAD_SHARED(*ctr
);
117 if (!(v
& URCU_BP_GP_CTR_NEST_MASK
))
118 return URCU_BP_READER_INACTIVE
;
119 if (!((v
^ urcu_bp_gp
.ctr
) & URCU_BP_GP_CTR_PHASE
))
120 return URCU_BP_READER_ACTIVE_CURRENT
;
121 return URCU_BP_READER_ACTIVE_OLD
;
125 * Helper for _urcu_bp_read_lock(). The format of urcu_bp_gp.ctr (as well as
126 * the per-thread rcu_reader.ctr) has the lower-order bits containing a count of
127 * _urcu_bp_read_lock() nesting, and a single high-order URCU_BP_GP_CTR_PHASE bit
128 * that contains either zero or one. The smp_mb_slave() ensures that the accesses in
129 * _urcu_bp_read_lock() happen before the subsequent read-side critical section.
131 static inline void _urcu_bp_read_lock_update(unsigned long tmp
)
133 if (caa_likely(!(tmp
& URCU_BP_GP_CTR_NEST_MASK
))) {
134 _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader
)->ctr
, _CMM_LOAD_SHARED(urcu_bp_gp
.ctr
));
135 urcu_bp_smp_mb_slave();
137 _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader
)->ctr
, tmp
+ URCU_BP_GP_COUNT
);
141 * Enter an RCU read-side critical section.
143 * The first cmm_barrier() call ensures that the compiler does not reorder
144 * the body of _urcu_bp_read_lock() with a mutex.
146 * This function and its helper are both less than 10 lines long. The
147 * intent is that this function meets the 10-line criterion in LGPL,
148 * allowing this function to be invoked directly from non-LGPL code.
150 static inline void _urcu_bp_read_lock(void)
154 if (caa_unlikely(!URCU_TLS(urcu_bp_reader
)))
155 urcu_bp_register(); /* If not yet registered. */
156 cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */
157 tmp
= URCU_TLS(urcu_bp_reader
)->ctr
;
158 urcu_assert_debug((tmp
& URCU_BP_GP_CTR_NEST_MASK
) != URCU_BP_GP_CTR_NEST_MASK
);
159 _urcu_bp_read_lock_update(tmp
);
163 * Exit an RCU read-side critical section. This function is less than
164 * 10 lines of code, and is intended to be usable by non-LGPL code, as
165 * called out in LGPL.
167 static inline void _urcu_bp_read_unlock(void)
171 tmp
= URCU_TLS(urcu_bp_reader
)->ctr
;
172 urcu_assert_debug(tmp
& URCU_BP_GP_CTR_NEST_MASK
);
173 /* Finish using rcu before decrementing the pointer. */
174 urcu_bp_smp_mb_slave();
175 _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader
)->ctr
, tmp
- URCU_BP_GP_COUNT
);
176 cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */
180 * Returns whether within a RCU read-side critical section.
182 * This function is less than 10 lines long. The intent is that this
183 * function meets the 10-line criterion for LGPL, allowing this function
184 * to be invoked directly from non-LGPL code.
186 static inline int _urcu_bp_read_ongoing(void)
188 if (caa_unlikely(!URCU_TLS(urcu_bp_reader
)))
189 urcu_bp_register(); /* If not yet registered. */
190 return URCU_TLS(urcu_bp_reader
)->ctr
& URCU_BP_GP_CTR_NEST_MASK
;
197 #endif /* _URCU_BP_STATIC_H */