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