4 * Userspace RCU QSBR library
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
35 #define BUILD_QSBR_LIB
36 #include "urcu-qsbr-static.h"
37 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
38 #include "urcu-qsbr.h"
40 void __attribute__((destructor
)) rcu_exit(void);
42 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
47 * Global grace period counter.
49 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
52 * Written to only by each individual reader. Read by both the reader and the
55 struct rcu_reader __thread rcu_reader
;
58 unsigned int yield_active
;
59 unsigned int __thread rand_yield
;
62 static LIST_HEAD(registry
);
64 static void mutex_lock(pthread_mutex_t
*mutex
)
68 #ifndef DISTRUST_SIGNALS_EXTREME
69 ret
= pthread_mutex_lock(mutex
);
71 perror("Error in pthread mutex lock");
74 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
75 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
76 if (ret
!= EBUSY
&& ret
!= EINTR
) {
77 printf("ret = %d, errno = %d\n", ret
, errno
);
78 perror("Error in pthread mutex lock");
83 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
86 static void mutex_unlock(pthread_mutex_t
*mutex
)
90 ret
= pthread_mutex_unlock(mutex
);
92 perror("Error in pthread mutex unlock");
98 * synchronize_rcu() waiting. Single thread.
100 static void wait_gp(void)
102 /* Read reader_gp before read futex */
104 if (uatomic_read(&gp_futex
) == -1)
105 futex_noasync(&gp_futex
, FUTEX_WAIT
, -1,
109 static void update_counter_and_wait(void)
111 LIST_HEAD(qsreaders
);
113 struct rcu_reader
*index
, *tmp
;
115 #if (BITS_PER_LONG < 64)
116 /* Switch parity: 0 -> 1, 1 -> 0 */
117 STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
118 #else /* !(BITS_PER_LONG < 64) */
119 /* Increment current G.P. */
120 STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
121 #endif /* !(BITS_PER_LONG < 64) */
124 * Enforce compiler-order of store to rcu_gp_ctr before before
125 * load rcu_reader ctr.
126 * This ensures synchronize_rcu() cannot be starved by readers.
131 * Wait for each thread rcu_reader_qs_gp count to become 0.
135 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
136 uatomic_dec(&gp_futex
);
137 /* Write futex before read reader_gp */
141 list_for_each_entry_safe(index
, tmp
, ®istry
, head
) {
142 if (!rcu_gp_ongoing(&index
->ctr
))
143 list_move(&index
->head
, &qsreaders
);
146 if (list_empty(®istry
)) {
147 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
148 /* Read reader_gp before write futex */
150 uatomic_set(&gp_futex
, 0);
154 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
157 #ifndef HAS_INCOHERENT_CACHES
159 #else /* #ifndef HAS_INCOHERENT_CACHES */
161 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
165 /* put back the reader list in the registry */
166 list_splice(&qsreaders
, ®istry
);
170 * Using a two-subphases algorithm for architectures with smaller than 64-bit
171 * long-size to ensure we do not encounter an overflow bug.
174 #if (BITS_PER_LONG < 64)
175 void synchronize_rcu(void)
177 unsigned long was_online
;
179 was_online
= rcu_reader
.ctr
;
181 /* All threads should read qparity before accessing data structure
182 * where new ptr points to.
184 /* Write new ptr before changing the qparity */
188 * Mark the writer thread offline to make sure we don't wait for
189 * our own quiescent state. This allows using synchronize_rcu() in
190 * threads registered as readers.
193 STORE_SHARED(rcu_reader
.ctr
, 0);
195 mutex_lock(&rcu_gp_lock
);
197 if (list_empty(®istry
))
201 * Wait for previous parity to be empty of readers.
203 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
206 * Must finish waiting for quiescent state for parity 0 before
207 * committing qparity update to memory. Failure to do so could result in
208 * the writer waiting forever while new readers are always accessing
209 * data (no progress).
210 * Ensured by STORE_SHARED and LOAD_SHARED.
214 * Adding a smp_mb() which is _not_ formally required, but makes the
215 * model easier to understand. It does not have a big performance impact
216 * anyway, given this is the write-side.
221 * Wait for previous parity to be empty of readers.
223 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
225 mutex_unlock(&rcu_gp_lock
);
228 * Finish waiting for reader threads before letting the old ptr being
232 _STORE_SHARED(rcu_reader
.ctr
, LOAD_SHARED(rcu_gp_ctr
));
235 #else /* !(BITS_PER_LONG < 64) */
236 void synchronize_rcu(void)
238 unsigned long was_online
;
240 was_online
= rcu_reader
.ctr
;
243 * Mark the writer thread offline to make sure we don't wait for
244 * our own quiescent state. This allows using synchronize_rcu() in
245 * threads registered as readers.
249 STORE_SHARED(rcu_reader
.ctr
, 0);
251 mutex_lock(&rcu_gp_lock
);
252 if (list_empty(®istry
))
254 update_counter_and_wait();
256 mutex_unlock(&rcu_gp_lock
);
259 _STORE_SHARED(rcu_reader
.ctr
, LOAD_SHARED(rcu_gp_ctr
));
262 #endif /* !(BITS_PER_LONG < 64) */
265 * library wrappers to be used by non-LGPL compatible source code.
268 void rcu_read_lock(void)
273 void rcu_read_unlock(void)
278 void rcu_quiescent_state(void)
280 _rcu_quiescent_state();
283 void rcu_thread_offline(void)
285 _rcu_thread_offline();
288 void rcu_thread_online(void)
290 _rcu_thread_online();
293 void rcu_register_thread(void)
295 rcu_reader
.tid
= pthread_self();
296 assert(rcu_reader
.ctr
== 0);
298 mutex_lock(&rcu_gp_lock
);
299 list_add(&rcu_reader
.head
, ®istry
);
300 mutex_unlock(&rcu_gp_lock
);
301 _rcu_thread_online();
304 void rcu_unregister_thread(void)
307 * We have to make the thread offline otherwise we end up dealocking
308 * with a waiting writer.
310 _rcu_thread_offline();
311 mutex_lock(&rcu_gp_lock
);
312 list_del(&rcu_reader
.head
);
313 mutex_unlock(&rcu_gp_lock
);
318 assert(list_empty(®istry
));