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.
128 * Adding a smp_mb() which is _not_ formally required, but makes the
129 * model easier to understand. It does not have a big performance impact
130 * anyway, given this is the write-side.
135 * Wait for each thread rcu_reader_qs_gp count to become 0.
139 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
140 uatomic_dec(&gp_futex
);
141 /* Write futex before read reader_gp */
145 list_for_each_entry_safe(index
, tmp
, ®istry
, head
) {
146 if (!rcu_gp_ongoing(&index
->ctr
))
147 list_move(&index
->head
, &qsreaders
);
150 if (list_empty(®istry
)) {
151 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
152 /* Read reader_gp before write futex */
154 uatomic_set(&gp_futex
, 0);
158 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
161 #ifndef HAS_INCOHERENT_CACHES
163 #else /* #ifndef HAS_INCOHERENT_CACHES */
165 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
169 /* put back the reader list in the registry */
170 list_splice(&qsreaders
, ®istry
);
174 * Using a two-subphases algorithm for architectures with smaller than 64-bit
175 * long-size to ensure we do not encounter an overflow bug.
178 #if (BITS_PER_LONG < 64)
179 void synchronize_rcu(void)
181 unsigned long was_online
;
183 was_online
= rcu_reader
.ctr
;
185 /* All threads should read qparity before accessing data structure
186 * where new ptr points to.
188 /* Write new ptr before changing the qparity */
192 * Mark the writer thread offline to make sure we don't wait for
193 * our own quiescent state. This allows using synchronize_rcu() in
194 * threads registered as readers.
197 STORE_SHARED(rcu_reader
.ctr
, 0);
199 mutex_lock(&rcu_gp_lock
);
201 if (list_empty(®istry
))
205 * Wait for previous parity to be empty of readers.
207 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
210 * Must finish waiting for quiescent state for parity 0 before
211 * committing qparity update to memory. Failure to do so could result in
212 * the writer waiting forever while new readers are always accessing
213 * data (no progress).
214 * Ensured by STORE_SHARED and LOAD_SHARED.
218 * Adding a smp_mb() which is _not_ formally required, but makes the
219 * model easier to understand. It does not have a big performance impact
220 * anyway, given this is the write-side.
225 * Wait for previous parity to be empty of readers.
227 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
229 mutex_unlock(&rcu_gp_lock
);
232 * Finish waiting for reader threads before letting the old ptr being
236 _STORE_SHARED(rcu_reader
.ctr
, LOAD_SHARED(rcu_gp_ctr
));
239 #else /* !(BITS_PER_LONG < 64) */
240 void synchronize_rcu(void)
242 unsigned long was_online
;
244 was_online
= rcu_reader
.ctr
;
247 * Mark the writer thread offline to make sure we don't wait for
248 * our own quiescent state. This allows using synchronize_rcu() in
249 * threads registered as readers.
253 STORE_SHARED(rcu_reader
.ctr
, 0);
255 mutex_lock(&rcu_gp_lock
);
256 if (list_empty(®istry
))
258 update_counter_and_wait();
260 mutex_unlock(&rcu_gp_lock
);
263 _STORE_SHARED(rcu_reader
.ctr
, LOAD_SHARED(rcu_gp_ctr
));
266 #endif /* !(BITS_PER_LONG < 64) */
269 * library wrappers to be used by non-LGPL compatible source code.
272 void rcu_read_lock(void)
277 void rcu_read_unlock(void)
282 void rcu_quiescent_state(void)
284 _rcu_quiescent_state();
287 void rcu_thread_offline(void)
289 _rcu_thread_offline();
292 void rcu_thread_online(void)
294 _rcu_thread_online();
297 void rcu_register_thread(void)
299 rcu_reader
.tid
= pthread_self();
300 assert(rcu_reader
.ctr
== 0);
302 mutex_lock(&rcu_gp_lock
);
303 list_add(&rcu_reader
.head
, ®istry
);
304 mutex_unlock(&rcu_gp_lock
);
305 _rcu_thread_online();
308 void rcu_unregister_thread(void)
311 * We have to make the thread offline otherwise we end up dealocking
312 * with a waiting writer.
314 _rcu_thread_offline();
315 mutex_lock(&rcu_gp_lock
);
316 list_del(&rcu_reader
.head
);
317 mutex_unlock(&rcu_gp_lock
);
322 assert(list_empty(®istry
));