urcu/annotate: Add CMM annotation
[urcu.git] / include / urcu / static / urcu-qsbr.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
7ac06cef
MD
6#ifndef _URCU_QSBR_STATIC_H
7#define _URCU_QSBR_STATIC_H
8
9/*
7ac06cef
MD
10 * Userspace RCU QSBR 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.
7ac06cef 14 *
7ac06cef
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>
f0f7dbdd 20#include <limits.h>
bc6c15bb 21#include <unistd.h>
6d841bc2 22#include <stdint.h>
7ac06cef 23
601922a8 24#include <urcu/annotate.h>
01477510 25#include <urcu/debug.h>
ec4e58a3
MD
26#include <urcu/compiler.h>
27#include <urcu/arch.h>
7e30abe3 28#include <urcu/system.h>
a2e7bf9c 29#include <urcu/uatomic.h>
4f8e3380 30#include <urcu/list.h>
41849996 31#include <urcu/futex.h>
bd252a04 32#include <urcu/tls-compat.h>
4477a870 33#include <urcu/static/urcu-common.h>
bc6c15bb 34
36bc70a8
MD
35#ifdef __cplusplus
36extern "C" {
67ecffc0 37#endif
36bc70a8 38
7ac06cef
MD
39/*
40 * This code section can only be included in LGPL 2.1 compatible source code.
41 * See below for the function call wrappers which can be used in code meant to
42 * be only linked with the Userspace RCU library. This comes with a small
43 * performance degradation on the read-side due to the added function calls.
44 * This is required to permit relinking with newer versions of the library.
45 */
46
4477a870
MD
47#define URCU_QSBR_GP_ONLINE (1UL << 0)
48#define URCU_QSBR_GP_CTR (1UL << 1)
15302e28 49
4477a870 50extern struct urcu_gp urcu_qsbr_gp;
7ac06cef 51
4477a870 52struct urcu_qsbr_reader {
bd1a5e15 53 /* Data used by both reader and synchronize_rcu() */
4f8e3380 54 unsigned long ctr;
bd1a5e15 55 /* Data used for registry */
16aa9ee8 56 struct cds_list_head node __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
83a2c421 57 int waiting;
4f8e3380 58 pthread_t tid;
a77f7d82
MD
59 /* Reader registered flag, for internal checks. */
60 unsigned int registered:1;
4f8e3380
MD
61};
62
4477a870 63extern DECLARE_URCU_TLS(struct urcu_qsbr_reader, urcu_qsbr_reader);
7ac06cef 64
bc6c15bb
MD
65/*
66 * Wake-up waiting synchronize_rcu(). Called from many concurrent threads.
67 */
4477a870 68static inline void urcu_qsbr_wake_up_gp(void)
bc6c15bb 69{
4477a870
MD
70 if (caa_unlikely(_CMM_LOAD_SHARED(URCU_TLS(urcu_qsbr_reader).waiting))) {
71 _CMM_STORE_SHARED(URCU_TLS(urcu_qsbr_reader).waiting, 0);
83a2c421 72 cmm_smp_mb();
4477a870 73 if (uatomic_read(&urcu_qsbr_gp.futex) != -1)
83a2c421 74 return;
4477a870 75 uatomic_set(&urcu_qsbr_gp.futex, 0);
b0a841b4
MD
76 /*
77 * Ignoring return value until we can make this function
78 * return something (because urcu_die() is not publicly
79 * exposed).
80 */
4477a870 81 (void) futex_noasync(&urcu_qsbr_gp.futex, FUTEX_WAKE, 1,
b0a841b4 82 NULL, NULL, 0);
bc6c15bb
MD
83 }
84}
85
601922a8
OD
86static inline enum urcu_state urcu_qsbr_reader_state(unsigned long *ctr,
87 cmm_annotate_t *group)
7ac06cef 88{
e26fa029 89 unsigned long v;
4e560c17 90
601922a8
OD
91 v = uatomic_load(ctr, CMM_RELAXED);
92 cmm_annotate_group_mem_acquire(group, ctr);
93
708d89f0 94 if (!v)
4477a870
MD
95 return URCU_READER_INACTIVE;
96 if (v == urcu_qsbr_gp.ctr)
97 return URCU_READER_ACTIVE_CURRENT;
98 return URCU_READER_ACTIVE_OLD;
7ac06cef
MD
99}
100
a5a9f428
PM
101/*
102 * Enter an RCU read-side critical section.
103 *
104 * This function is less than 10 lines long. The intent is that this
105 * function meets the 10-line criterion for LGPL, allowing this function
106 * to be invoked directly from non-LGPL code.
107 */
4477a870 108static inline void _urcu_qsbr_read_lock(void)
7ac06cef 109{
2a27e931 110 urcu_assert_debug(URCU_TLS(urcu_qsbr_reader).ctr);
7ac06cef
MD
111}
112
a5a9f428
PM
113/*
114 * Exit an RCU read-side critical section.
115 *
116 * This function is less than 10 lines long. The intent is that this
117 * function meets the 10-line criterion for LGPL, allowing this function
118 * to be invoked directly from non-LGPL code.
119 */
4477a870 120static inline void _urcu_qsbr_read_unlock(void)
7ac06cef 121{
2a27e931 122 urcu_assert_debug(URCU_TLS(urcu_qsbr_reader).ctr);
7ac06cef
MD
123}
124
882f3357
MD
125/*
126 * Returns whether within a RCU read-side critical section.
127 *
128 * This function is less than 10 lines long. The intent is that this
129 * function meets the 10-line criterion for LGPL, allowing this function
130 * to be invoked directly from non-LGPL code.
131 */
4477a870 132static inline int _urcu_qsbr_read_ongoing(void)
882f3357 133{
4477a870 134 return URCU_TLS(urcu_qsbr_reader).ctr;
882f3357
MD
135}
136
f864c15d
MD
137/*
138 * This is a helper function for _rcu_quiescent_state().
139 * The first cmm_smp_mb() ensures memory accesses in the prior read-side
140 * critical sections are not reordered with store to
4477a870 141 * URCU_TLS(urcu_qsbr_reader).ctr, and ensures that mutexes held within an
f864c15d 142 * offline section that would happen to end with this
4477a870
MD
143 * urcu_qsbr_quiescent_state() call are not reordered with
144 * store to URCU_TLS(urcu_qsbr_reader).ctr.
f864c15d 145 */
4477a870 146static inline void _urcu_qsbr_quiescent_state_update_and_wakeup(unsigned long gp_ctr)
f864c15d 147{
601922a8
OD
148 uatomic_store(&URCU_TLS(urcu_qsbr_reader).ctr, gp_ctr, CMM_SEQ_CST);
149
150 /* write URCU_TLS(urcu_qsbr_reader).ctr before read futex */
4477a870 151 urcu_qsbr_wake_up_gp();
f864c15d
MD
152 cmm_smp_mb();
153}
154
a5a9f428
PM
155/*
156 * Inform RCU of a quiescent state.
157 *
158 * This function is less than 10 lines long. The intent is that this
159 * function meets the 10-line criterion for LGPL, allowing this function
160 * to be invoked directly from non-LGPL code.
f864c15d
MD
161 *
162 * We skip the memory barriers and gp store if our local ctr already
4477a870 163 * matches the global urcu_qsbr_gp.ctr value: this is OK because a prior
f864c15d
MD
164 * _rcu_quiescent_state() or _rcu_thread_online() already updated it
165 * within our thread, so we have no quiescent state to report.
a5a9f428 166 */
4477a870 167static inline void _urcu_qsbr_quiescent_state(void)
7ac06cef 168{
f864c15d
MD
169 unsigned long gp_ctr;
170
2a27e931 171 urcu_assert_debug(URCU_TLS(urcu_qsbr_reader).registered);
601922a8
OD
172 gp_ctr = uatomic_load(&urcu_qsbr_gp.ctr, CMM_RELAXED);
173 if (gp_ctr == URCU_TLS(urcu_qsbr_reader).ctr)
f864c15d 174 return;
4477a870 175 _urcu_qsbr_quiescent_state_update_and_wakeup(gp_ctr);
7ac06cef
MD
176}
177
a5a9f428
PM
178/*
179 * Take a thread offline, prohibiting it from entering further RCU
180 * read-side critical sections.
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 void _urcu_qsbr_thread_offline(void)
7ac06cef 187{
2a27e931 188 urcu_assert_debug(URCU_TLS(urcu_qsbr_reader).registered);
601922a8
OD
189 uatomic_store(&URCU_TLS(urcu_qsbr_reader).ctr, 0, CMM_SEQ_CST);
190 /* write URCU_TLS(urcu_qsbr_reader).ctr before read futex */
4477a870 191 urcu_qsbr_wake_up_gp();
5481ddb3 192 cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */
7ac06cef
MD
193}
194
a5a9f428
PM
195/*
196 * Bring a thread online, allowing it to once again enter RCU
197 * read-side critical sections.
198 *
199 * This function is less than 10 lines long. The intent is that this
200 * function meets the 10-line criterion for LGPL, allowing this function
201 * to be invoked directly from non-LGPL code.
202 */
4477a870 203static inline void _urcu_qsbr_thread_online(void)
7ac06cef 204{
601922a8
OD
205 unsigned long *pctr = &URCU_TLS(urcu_qsbr_reader).ctr;
206 unsigned long ctr;
207
2a27e931 208 urcu_assert_debug(URCU_TLS(urcu_qsbr_reader).registered);
5481ddb3 209 cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */
601922a8
OD
210 ctr = uatomic_load(&urcu_qsbr_gp.ctr, CMM_RELAXED);
211 cmm_annotate_mem_acquire(&urcu_qsbr_gp.ctr);
212 uatomic_store(pctr, ctr, CMM_RELAXED);
5481ddb3 213 cmm_smp_mb();
7ac06cef
MD
214}
215
67ecffc0 216#ifdef __cplusplus
36bc70a8
MD
217}
218#endif
219
7ac06cef 220#endif /* _URCU_QSBR_STATIC_H */
This page took 0.059336 seconds and 4 git commands to generate.