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 #include "urcu-qsbr-map.h"
37 #define BUILD_QSBR_LIB
38 #include "urcu-qsbr-static.h"
39 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
40 #include "urcu-qsbr.h"
42 void __attribute__((destructor
)) rcu_exit(void);
44 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
49 * Global grace period counter.
51 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
54 * Written to only by each individual reader. Read by both the reader and the
57 struct rcu_reader __thread rcu_reader
;
60 unsigned int yield_active
;
61 unsigned int __thread rand_yield
;
64 static CDS_LIST_HEAD(registry
);
66 static void mutex_lock(pthread_mutex_t
*mutex
)
70 #ifndef DISTRUST_SIGNALS_EXTREME
71 ret
= pthread_mutex_lock(mutex
);
73 perror("Error in pthread mutex lock");
76 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
77 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
78 if (ret
!= EBUSY
&& ret
!= EINTR
) {
79 printf("ret = %d, errno = %d\n", ret
, errno
);
80 perror("Error in pthread mutex lock");
85 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
88 static void mutex_unlock(pthread_mutex_t
*mutex
)
92 ret
= pthread_mutex_unlock(mutex
);
94 perror("Error in pthread mutex unlock");
100 * synchronize_rcu() waiting. Single thread.
102 static void wait_gp(void)
104 /* Read reader_gp before read futex */
106 if (uatomic_read(&gp_futex
) == -1)
107 futex_noasync(&gp_futex
, FUTEX_WAIT
, -1,
111 static void update_counter_and_wait(void)
113 CDS_LIST_HEAD(qsreaders
);
115 struct rcu_reader
*index
, *tmp
;
117 #if (CAA_BITS_PER_LONG < 64)
118 /* Switch parity: 0 -> 1, 1 -> 0 */
119 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
120 #else /* !(CAA_BITS_PER_LONG < 64) */
121 /* Increment current G.P. */
122 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
123 #endif /* !(CAA_BITS_PER_LONG < 64) */
126 * Must commit rcu_gp_ctr update to memory before waiting for
127 * quiescent state. Failure to do so could result in the writer
128 * waiting forever while new readers are always accessing data
129 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
130 * before load rcu_reader ctr.
135 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
136 * model easier to understand. It does not have a big performance impact
137 * anyway, given this is the write-side.
142 * Wait for each thread rcu_reader_qs_gp count to become 0.
146 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
147 uatomic_dec(&gp_futex
);
148 /* Write futex before read reader_gp */
152 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
153 if (!rcu_gp_ongoing(&index
->ctr
))
154 cds_list_move(&index
->node
, &qsreaders
);
157 if (cds_list_empty(®istry
)) {
158 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
159 /* Read reader_gp before write futex */
161 uatomic_set(&gp_futex
, 0);
165 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
168 #ifndef HAS_INCOHERENT_CACHES
170 #else /* #ifndef HAS_INCOHERENT_CACHES */
172 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
176 /* put back the reader list in the registry */
177 cds_list_splice(&qsreaders
, ®istry
);
181 * Using a two-subphases algorithm for architectures with smaller than 64-bit
182 * long-size to ensure we do not encounter an overflow bug.
185 #if (CAA_BITS_PER_LONG < 64)
186 void synchronize_rcu(void)
188 unsigned long was_online
;
190 was_online
= rcu_reader
.ctr
;
192 /* All threads should read qparity before accessing data structure
193 * where new ptr points to.
195 /* Write new ptr before changing the qparity */
199 * Mark the writer thread offline to make sure we don't wait for
200 * our own quiescent state. This allows using synchronize_rcu()
201 * in threads registered as readers.
204 CMM_STORE_SHARED(rcu_reader
.ctr
, 0);
206 mutex_lock(&rcu_gp_lock
);
208 if (cds_list_empty(®istry
))
212 * Wait for previous parity to be empty of readers.
214 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
217 * Must finish waiting for quiescent state for parity 0 before
218 * committing next rcu_gp_ctr update to memory. Failure to
219 * do so could result in the writer waiting forever while new
220 * readers are always accessing data (no progress). Enforce
221 * compiler-order of load rcu_reader ctr before store to
227 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
228 * model easier to understand. It does not have a big performance impact
229 * anyway, given this is the write-side.
234 * Wait for previous parity to be empty of readers.
236 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
238 mutex_unlock(&rcu_gp_lock
);
241 * Finish waiting for reader threads before letting the old ptr being
245 _CMM_STORE_SHARED(rcu_reader
.ctr
,
246 CMM_LOAD_SHARED(rcu_gp_ctr
));
249 #else /* !(CAA_BITS_PER_LONG < 64) */
250 void synchronize_rcu(void)
252 unsigned long was_online
;
254 was_online
= rcu_reader
.ctr
;
257 * Mark the writer thread offline to make sure we don't wait for
258 * our own quiescent state. This allows using synchronize_rcu()
259 * in threads registered as readers.
263 CMM_STORE_SHARED(rcu_reader
.ctr
, 0);
265 mutex_lock(&rcu_gp_lock
);
266 if (cds_list_empty(®istry
))
268 update_counter_and_wait();
270 mutex_unlock(&rcu_gp_lock
);
273 _CMM_STORE_SHARED(rcu_reader
.ctr
,
274 CMM_LOAD_SHARED(rcu_gp_ctr
));
277 #endif /* !(CAA_BITS_PER_LONG < 64) */
280 * library wrappers to be used by non-LGPL compatible source code.
283 void rcu_read_lock(void)
288 void rcu_read_unlock(void)
293 void rcu_quiescent_state(void)
295 _rcu_quiescent_state();
298 void rcu_thread_offline(void)
300 _rcu_thread_offline();
303 void rcu_thread_online(void)
305 _rcu_thread_online();
308 void rcu_register_thread(void)
310 rcu_reader
.tid
= pthread_self();
311 assert(rcu_reader
.ctr
== 0);
313 mutex_lock(&rcu_gp_lock
);
314 cds_list_add(&rcu_reader
.node
, ®istry
);
315 mutex_unlock(&rcu_gp_lock
);
316 _rcu_thread_online();
319 void rcu_unregister_thread(void)
322 * We have to make the thread offline otherwise we end up dealocking
323 * with a waiting writer.
325 _rcu_thread_offline();
326 mutex_lock(&rcu_gp_lock
);
327 cds_list_del(&rcu_reader
.node
);
328 mutex_unlock(&rcu_gp_lock
);
333 assert(cds_list_empty(®istry
));
336 #include "urcu-call-rcu-impl.h"
337 #include "urcu-defer-impl.h"