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 urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
47 * Global grace period counter.
49 unsigned long urcu_gp_ctr
= RCU_GP_ONLINE
;
52 * Written to only by each individual reader. Read by both the reader and the
55 struct urcu_reader __thread urcu_reader
;
58 unsigned int yield_active
;
59 unsigned int __thread rand_yield
;
62 static LIST_HEAD(registry
);
64 static void internal_urcu_lock(void)
68 #ifndef DISTRUST_SIGNALS_EXTREME
69 ret
= pthread_mutex_lock(&urcu_mutex
);
71 perror("Error in pthread mutex lock");
74 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
75 while ((ret
= pthread_mutex_trylock(&urcu_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 internal_urcu_unlock(void)
90 ret
= pthread_mutex_unlock(&urcu_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 wait_for_quiescent_state(void)
111 LIST_HEAD(qsreaders
);
113 struct urcu_reader
*index
, *tmp
;
115 if (list_empty(®istry
))
118 * Wait for each thread rcu_reader_qs_gp count to become 0.
122 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
123 uatomic_dec(&gp_futex
);
124 /* Write futex before read reader_gp */
128 list_for_each_entry_safe(index
, tmp
, ®istry
, head
) {
129 if (!rcu_gp_ongoing(&index
->ctr
))
130 list_move(&index
->head
, &qsreaders
);
133 if (list_empty(®istry
)) {
134 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
135 /* Read reader_gp before write futex */
137 uatomic_set(&gp_futex
, 0);
141 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
144 #ifndef HAS_INCOHERENT_CACHES
146 #else /* #ifndef HAS_INCOHERENT_CACHES */
148 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
152 /* put back the reader list in the registry */
153 list_splice(&qsreaders
, ®istry
);
157 * Using a two-subphases algorithm for architectures with smaller than 64-bit
158 * long-size to ensure we do not encounter an overflow bug.
161 #if (BITS_PER_LONG < 64)
163 * called with urcu_mutex held.
165 static void switch_next_urcu_qparity(void)
167 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR
);
170 void synchronize_rcu(void)
172 unsigned long was_online
;
174 was_online
= urcu_reader
.ctr
;
176 /* All threads should read qparity before accessing data structure
177 * where new ptr points to.
179 /* Write new ptr before changing the qparity */
183 * Mark the writer thread offline to make sure we don't wait for
184 * our own quiescent state. This allows using synchronize_rcu() in
185 * threads registered as readers.
188 STORE_SHARED(urcu_reader
.ctr
, 0);
190 internal_urcu_lock();
192 switch_next_urcu_qparity(); /* 0 -> 1 */
195 * Must commit qparity update to memory before waiting for parity
196 * 0 quiescent state. Failure to do so could result in the writer
197 * waiting forever while new readers are always accessing data (no
199 * Ensured by STORE_SHARED and LOAD_SHARED.
203 * Wait for previous parity to be empty of readers.
205 wait_for_quiescent_state(); /* Wait readers in parity 0 */
208 * Must finish waiting for quiescent state for parity 0 before
209 * committing qparity update to memory. Failure to do so could result in
210 * the writer waiting forever while new readers are always accessing
211 * data (no progress).
212 * Ensured by STORE_SHARED and LOAD_SHARED.
215 switch_next_urcu_qparity(); /* 1 -> 0 */
218 * Must commit qparity update to memory before waiting for parity
219 * 1 quiescent state. Failure to do so could result in the writer
220 * waiting forever while new readers are always accessing data (no
222 * Ensured by STORE_SHARED and LOAD_SHARED.
226 * Wait for previous parity to be empty of readers.
228 wait_for_quiescent_state(); /* Wait readers in parity 1 */
230 internal_urcu_unlock();
233 * Finish waiting for reader threads before letting the old ptr being
237 _STORE_SHARED(urcu_reader
.ctr
, LOAD_SHARED(urcu_gp_ctr
));
240 #else /* !(BITS_PER_LONG < 64) */
241 void synchronize_rcu(void)
243 unsigned long was_online
;
245 was_online
= urcu_reader
.ctr
;
248 * Mark the writer thread offline to make sure we don't wait for
249 * our own quiescent state. This allows using synchronize_rcu() in
250 * threads registered as readers.
254 STORE_SHARED(urcu_reader
.ctr
, 0);
256 internal_urcu_lock();
257 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
+ RCU_GP_CTR
);
258 wait_for_quiescent_state();
259 internal_urcu_unlock();
262 _STORE_SHARED(urcu_reader
.ctr
, LOAD_SHARED(urcu_gp_ctr
));
265 #endif /* !(BITS_PER_LONG < 64) */
268 * library wrappers to be used by non-LGPL compatible source code.
271 void rcu_read_lock(void)
276 void rcu_read_unlock(void)
281 void rcu_quiescent_state(void)
283 _rcu_quiescent_state();
286 void rcu_thread_offline(void)
288 _rcu_thread_offline();
291 void rcu_thread_online(void)
293 _rcu_thread_online();
296 void rcu_register_thread(void)
298 urcu_reader
.tid
= pthread_self();
299 assert(urcu_reader
.ctr
== 0);
301 internal_urcu_lock();
302 list_add(&urcu_reader
.head
, ®istry
);
303 internal_urcu_unlock();
304 _rcu_thread_online();
307 void rcu_unregister_thread(void)
310 * We have to make the thread offline otherwise we end up dealocking
311 * with a waiting writer.
313 _rcu_thread_offline();
314 internal_urcu_lock();
315 list_del(&urcu_reader
.head
);
316 internal_urcu_unlock();
321 assert(list_empty(®istry
));