Public headers: use SPDX identifiers
[urcu.git] / include / urcu / static / urcu-bp.h
CommitLineData
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
01477510 22#include <urcu/debug.h>
d8d9a340 23#include <urcu/config.h>
fdee2e6d
MD
24#include <urcu/compiler.h>
25#include <urcu/arch.h>
26#include <urcu/system.h>
a2e7bf9c 27#include <urcu/uatomic.h>
fdee2e6d 28#include <urcu/list.h>
bd252a04 29#include <urcu/tls-compat.h>
fdee2e6d
MD
30
31/*
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.
37 */
38
36bc70a8
MD
39#ifdef __cplusplus
40extern "C" {
41#endif
42
4477a870
MD
43enum urcu_bp_state {
44 URCU_BP_READER_ACTIVE_CURRENT,
45 URCU_BP_READER_ACTIVE_OLD,
46 URCU_BP_READER_INACTIVE,
52c75091
MD
47};
48
fdee2e6d 49/*
4477a870 50 * The trick here is that URCU_BP_GP_CTR_PHASE must be a multiple of 8 so we can use a
fdee2e6d
MD
51 * full 8-bits, 16-bits or 32-bits bitmask for the lower order bits.
52 */
4477a870 53#define URCU_BP_GP_COUNT (1UL << 0)
fdee2e6d 54/* Use the amount of bits equal to half of the architecture long size */
4477a870
MD
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)
fdee2e6d
MD
57
58/*
4477a870 59 * Used internally by _urcu_bp_read_lock.
fdee2e6d 60 */
4477a870 61extern void urcu_bp_register(void);
fdee2e6d 62
4477a870 63struct urcu_bp_gp {
c13c2e55
MD
64 /*
65 * Global grace period counter.
4477a870
MD
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.
c13c2e55
MD
68 * Written to only by writer with mutex taken.
69 * Read by both writer and readers.
70 */
71 unsigned long ctr;
72} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
73
4477a870 74extern struct urcu_bp_gp urcu_bp_gp;
fdee2e6d 75
4477a870
MD
76struct urcu_bp_reader {
77 /* Data used by both reader and urcu_bp_synchronize_rcu() */
52c75091 78 unsigned long ctr;
fdee2e6d 79 /* Data used for registry */
16aa9ee8 80 struct cds_list_head node __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
fdee2e6d
MD
81 pthread_t tid;
82 int alloc; /* registry entry allocated */
83};
84
85/*
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
88 * the reader thread.
89 */
4477a870 90extern DECLARE_URCU_TLS(struct urcu_bp_reader *, urcu_bp_reader);
fdee2e6d 91
d8d9a340
MD
92#ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
93#define urcu_bp_has_sys_membarrier 1
94#else
f541831e 95extern int urcu_bp_has_sys_membarrier;
d8d9a340 96#endif
f541831e
MD
97
98static inline void urcu_bp_smp_mb_slave(void)
99{
100 if (caa_likely(urcu_bp_has_sys_membarrier))
101 cmm_barrier();
102 else
103 cmm_smp_mb();
104}
105
4477a870 106static inline enum urcu_bp_state urcu_bp_reader_state(unsigned long *ctr)
fdee2e6d 107{
52c75091 108 unsigned long v;
fdee2e6d 109
52c75091 110 if (ctr == NULL)
4477a870 111 return URCU_BP_READER_INACTIVE;
fdee2e6d
MD
112 /*
113 * Make sure both tests below are done on the same version of *value
114 * to insure consistency.
115 */
52c75091 116 v = CMM_LOAD_SHARED(*ctr);
4477a870
MD
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;
fdee2e6d
MD
122}
123
a5a9f428 124/*
4477a870 125 * Helper for _urcu_bp_read_lock(). The format of urcu_bp_gp.ctr (as well as
be152bdb
LKO
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
4477a870 129 * _urcu_bp_read_lock() happen before the subsequent read-side critical section.
a5a9f428 130 */
4477a870 131static inline void _urcu_bp_read_lock_update(unsigned long tmp)
a5a9f428 132{
4477a870
MD
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));
f541831e 135 urcu_bp_smp_mb_slave();
a5a9f428 136 } else
4477a870 137 _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader)->ctr, tmp + URCU_BP_GP_COUNT);
a5a9f428
PM
138}
139
140/*
141 * Enter an RCU read-side critical section.
142 *
143 * The first cmm_barrier() call ensures that the compiler does not reorder
4477a870 144 * the body of _urcu_bp_read_lock() with a mutex.
a5a9f428
PM
145 *
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.
149 */
4477a870 150static inline void _urcu_bp_read_lock(void)
fdee2e6d 151{
52c75091 152 unsigned long tmp;
fdee2e6d 153
4477a870
MD
154 if (caa_unlikely(!URCU_TLS(urcu_bp_reader)))
155 urcu_bp_register(); /* If not yet registered. */
5481ddb3 156 cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */
4477a870 157 tmp = URCU_TLS(urcu_bp_reader)->ctr;
2a27e931 158 urcu_assert_debug((tmp & URCU_BP_GP_CTR_NEST_MASK) != URCU_BP_GP_CTR_NEST_MASK);
4477a870 159 _urcu_bp_read_lock_update(tmp);
fdee2e6d
MD
160}
161
a5a9f428
PM
162/*
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.
166 */
4477a870 167static inline void _urcu_bp_read_unlock(void)
fdee2e6d 168{
343c8b13
MD
169 unsigned long tmp;
170
4477a870 171 tmp = URCU_TLS(urcu_bp_reader)->ctr;
2a27e931 172 urcu_assert_debug(tmp & URCU_BP_GP_CTR_NEST_MASK);
343c8b13 173 /* Finish using rcu before decrementing the pointer. */
f541831e 174 urcu_bp_smp_mb_slave();
4477a870 175 _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader)->ctr, tmp - URCU_BP_GP_COUNT);
5481ddb3 176 cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */
fdee2e6d
MD
177}
178
882f3357
MD
179/*
180 * Returns whether within a RCU read-side critical section.
181 *
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.
185 */
4477a870 186static inline int _urcu_bp_read_ongoing(void)
882f3357 187{
4477a870
MD
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;
882f3357
MD
191}
192
67ecffc0 193#ifdef __cplusplus
36bc70a8
MD
194}
195#endif
196
fdee2e6d 197#endif /* _URCU_BP_STATIC_H */
This page took 0.057836 seconds and 4 git commands to generate.