4 * Userspace RCU QSBR library
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
125 * state. Failure to do so could result in the writer waiting forever
126 * while new readers are always accessing data (no progress). Enforce
127 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
132 * Adding a smp_mb() which is _not_ formally required, but makes the
133 * model easier to understand. It does not have a big performance impact
134 * anyway, given this is the write-side.
139 * Wait for each thread rcu_reader_qs_gp count to become 0.
143 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
144 uatomic_dec(&gp_futex
);
145 /* Write futex before read reader_gp */
149 list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
150 if (!rcu_gp_ongoing(&index
->ctr
))
151 list_move(&index
->node
, &qsreaders
);
154 if (list_empty(®istry
)) {
155 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
156 /* Read reader_gp before write futex */
158 uatomic_set(&gp_futex
, 0);
162 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
165 #ifndef HAS_INCOHERENT_CACHES
167 #else /* #ifndef HAS_INCOHERENT_CACHES */
169 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
173 /* put back the reader list in the registry */
174 list_splice(&qsreaders
, ®istry
);
178 * Using a two-subphases algorithm for architectures with smaller than 64-bit
179 * long-size to ensure we do not encounter an overflow bug.
182 #if (BITS_PER_LONG < 64)
183 void synchronize_rcu(void)
185 unsigned long was_online
;
187 was_online
= rcu_reader
.ctr
;
189 /* All threads should read qparity before accessing data structure
190 * where new ptr points to.
192 /* Write new ptr before changing the qparity */
196 * Mark the writer thread offline to make sure we don't wait for
197 * our own quiescent state. This allows using synchronize_rcu() in
198 * threads registered as readers.
201 STORE_SHARED(rcu_reader
.ctr
, 0);
203 mutex_lock(&rcu_gp_lock
);
205 if (list_empty(®istry
))
209 * Wait for previous parity to be empty of readers.
211 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
214 * Must finish waiting for quiescent state for parity 0 before
215 * committing next rcu_gp_ctr update to memory. Failure to do so could
216 * result in the writer waiting forever while new readers are always
217 * accessing data (no progress). Enforce compiler-order of load
218 * rcu_reader ctr before store to rcu_gp_ctr.
223 * Adding a smp_mb() which is _not_ formally required, but makes the
224 * model easier to understand. It does not have a big performance impact
225 * anyway, given this is the write-side.
230 * Wait for previous parity to be empty of readers.
232 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
234 mutex_unlock(&rcu_gp_lock
);
237 * Finish waiting for reader threads before letting the old ptr being
241 _STORE_SHARED(rcu_reader
.ctr
, LOAD_SHARED(rcu_gp_ctr
));
244 #else /* !(BITS_PER_LONG < 64) */
245 void synchronize_rcu(void)
247 unsigned long was_online
;
249 was_online
= rcu_reader
.ctr
;
252 * Mark the writer thread offline to make sure we don't wait for
253 * our own quiescent state. This allows using synchronize_rcu() in
254 * threads registered as readers.
258 STORE_SHARED(rcu_reader
.ctr
, 0);
260 mutex_lock(&rcu_gp_lock
);
261 if (list_empty(®istry
))
263 update_counter_and_wait();
265 mutex_unlock(&rcu_gp_lock
);
268 _STORE_SHARED(rcu_reader
.ctr
, LOAD_SHARED(rcu_gp_ctr
));
271 #endif /* !(BITS_PER_LONG < 64) */
274 * library wrappers to be used by non-LGPL compatible source code.
277 void rcu_read_lock(void)
282 void rcu_read_unlock(void)
287 void rcu_quiescent_state(void)
289 _rcu_quiescent_state();
292 void rcu_thread_offline(void)
294 _rcu_thread_offline();
297 void rcu_thread_online(void)
299 _rcu_thread_online();
302 void rcu_register_thread(void)
304 rcu_reader
.tid
= pthread_self();
305 assert(rcu_reader
.ctr
== 0);
307 mutex_lock(&rcu_gp_lock
);
308 list_add(&rcu_reader
.node
, ®istry
);
309 mutex_unlock(&rcu_gp_lock
);
310 _rcu_thread_online();
313 void rcu_unregister_thread(void)
316 * We have to make the thread offline otherwise we end up dealocking
317 * with a waiting writer.
319 _rcu_thread_offline();
320 mutex_lock(&rcu_gp_lock
);
321 list_del(&rcu_reader
.node
);
322 mutex_unlock(&rcu_gp_lock
);
327 assert(list_empty(®istry
));