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.
38 #include "urcu/wfqueue.h"
39 #include "urcu/map/urcu-qsbr.h"
40 #define BUILD_QSBR_LIB
41 #include "urcu/static/urcu-qsbr.h"
42 #include "urcu-pointer.h"
43 #include "urcu/tls-compat.h"
47 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
49 #include "urcu-qsbr.h"
52 void __attribute__((destructor
)) rcu_exit(void);
55 * rcu_gp_lock ensures mutual exclusion between threads calling
58 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
60 * rcu_registry_lock ensures mutual exclusion between threads
61 * registering and unregistering themselves to/from the registry, and
62 * with threads reading that registry from synchronize_rcu(). However,
63 * this lock is not held all the way through the completion of awaiting
64 * for the grace period. It is sporadically released between iterations
66 * rcu_registry_lock may nest inside rcu_gp_lock.
68 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
73 * Global grace period counter.
75 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
78 * Active attempts to check for reader Q.S. before calling futex().
80 #define RCU_QS_ACTIVE_ATTEMPTS 100
83 * Written to only by each individual reader. Read by both the reader and the
86 __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader
, rcu_reader
);
89 unsigned int yield_active
;
90 __DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield
);
93 static CDS_LIST_HEAD(registry
);
95 static void mutex_lock(pthread_mutex_t
*mutex
)
99 #ifndef DISTRUST_SIGNALS_EXTREME
100 ret
= pthread_mutex_lock(mutex
);
103 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
104 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
105 if (ret
!= EBUSY
&& ret
!= EINTR
)
109 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
112 static void mutex_unlock(pthread_mutex_t
*mutex
)
116 ret
= pthread_mutex_unlock(mutex
);
122 * synchronize_rcu() waiting. Single thread.
124 static void wait_gp(void)
126 /* Read reader_gp before read futex */
128 if (uatomic_read(&gp_futex
) == -1)
129 futex_noasync(&gp_futex
, FUTEX_WAIT
, -1,
134 * Always called with rcu_registry lock held. Releases this lock between
135 * iterations and grabs it again. Holds the lock when it returns.
137 static void update_counter_and_wait(void)
139 CDS_LIST_HEAD(qsreaders
);
140 unsigned int wait_loops
= 0;
141 struct rcu_reader
*index
, *tmp
;
143 #if (CAA_BITS_PER_LONG < 64)
144 /* Switch parity: 0 -> 1, 1 -> 0 */
145 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
146 #else /* !(CAA_BITS_PER_LONG < 64) */
147 /* Increment current G.P. */
148 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
149 #endif /* !(CAA_BITS_PER_LONG < 64) */
152 * Must commit rcu_gp_ctr update to memory before waiting for
153 * quiescent state. Failure to do so could result in the writer
154 * waiting forever while new readers are always accessing data
155 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
156 * before load URCU_TLS(rcu_reader).ctr.
161 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
162 * model easier to understand. It does not have a big performance impact
163 * anyway, given this is the write-side.
168 * Wait for each thread rcu_reader_qs_gp count to become 0.
171 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
173 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
174 uatomic_set(&gp_futex
, -1);
176 * Write futex before write waiting (the other side
177 * reads them in the opposite order).
180 cds_list_for_each_entry(index
, ®istry
, node
) {
181 _CMM_STORE_SHARED(index
->waiting
, 1);
183 /* Write futex before read reader_gp */
186 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
187 if (!rcu_gp_ongoing(&index
->ctr
))
188 cds_list_move(&index
->node
, &qsreaders
);
191 if (cds_list_empty(®istry
)) {
192 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
193 /* Read reader_gp before write futex */
195 uatomic_set(&gp_futex
, 0);
199 /* Temporarily unlock the registry lock. */
200 mutex_unlock(&rcu_registry_lock
);
201 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
204 #ifndef HAS_INCOHERENT_CACHES
206 #else /* #ifndef HAS_INCOHERENT_CACHES */
208 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
210 /* Re-lock the registry lock before the next loop. */
211 mutex_lock(&rcu_registry_lock
);
214 /* put back the reader list in the registry */
215 cds_list_splice(&qsreaders
, ®istry
);
219 * Using a two-subphases algorithm for architectures with smaller than 64-bit
220 * long-size to ensure we do not encounter an overflow bug.
223 #if (CAA_BITS_PER_LONG < 64)
224 void synchronize_rcu(void)
226 unsigned long was_online
;
228 was_online
= URCU_TLS(rcu_reader
).ctr
;
230 /* All threads should read qparity before accessing data structure
231 * where new ptr points to. In the "then" case, rcu_thread_offline
232 * includes a memory barrier.
234 * Mark the writer thread offline to make sure we don't wait for
235 * our own quiescent state. This allows using synchronize_rcu()
236 * in threads registered as readers.
239 rcu_thread_offline();
243 mutex_lock(&rcu_gp_lock
);
244 mutex_lock(&rcu_registry_lock
);
246 if (cds_list_empty(®istry
))
250 * Wait for previous parity to be empty of readers.
251 * update_counter_and_wait() can release and grab again
252 * rcu_registry_lock interally.
254 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
257 * Must finish waiting for quiescent state for parity 0 before
258 * committing next rcu_gp_ctr update to memory. Failure to
259 * do so could result in the writer waiting forever while new
260 * readers are always accessing data (no progress). Enforce
261 * compiler-order of load URCU_TLS(rcu_reader).ctr before store to
267 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
268 * model easier to understand. It does not have a big performance impact
269 * anyway, given this is the write-side.
274 * Wait for previous parity to be empty of readers.
275 * update_counter_and_wait() can release and grab again
276 * rcu_registry_lock interally.
278 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
280 mutex_unlock(&rcu_registry_lock
);
281 mutex_unlock(&rcu_gp_lock
);
284 * Finish waiting for reader threads before letting the old ptr being
292 #else /* !(CAA_BITS_PER_LONG < 64) */
293 void synchronize_rcu(void)
295 unsigned long was_online
;
297 was_online
= URCU_TLS(rcu_reader
).ctr
;
300 * Mark the writer thread offline to make sure we don't wait for
301 * our own quiescent state. This allows using synchronize_rcu()
302 * in threads registered as readers.
305 rcu_thread_offline();
309 mutex_lock(&rcu_gp_lock
);
310 mutex_lock(&rcu_registry_lock
);
311 if (cds_list_empty(®istry
))
314 * update_counter_and_wait() can release and grab again
315 * rcu_registry_lock interally.
317 update_counter_and_wait();
319 mutex_unlock(&rcu_registry_lock
);
320 mutex_unlock(&rcu_gp_lock
);
327 #endif /* !(CAA_BITS_PER_LONG < 64) */
330 * library wrappers to be used by non-LGPL compatible source code.
333 void rcu_read_lock(void)
338 void rcu_read_unlock(void)
343 void rcu_quiescent_state(void)
345 _rcu_quiescent_state();
348 void rcu_thread_offline(void)
350 _rcu_thread_offline();
353 void rcu_thread_online(void)
355 _rcu_thread_online();
358 void rcu_register_thread(void)
360 URCU_TLS(rcu_reader
).tid
= pthread_self();
361 assert(URCU_TLS(rcu_reader
).ctr
== 0);
363 mutex_lock(&rcu_registry_lock
);
364 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
365 mutex_unlock(&rcu_registry_lock
);
366 _rcu_thread_online();
369 void rcu_unregister_thread(void)
372 * We have to make the thread offline otherwise we end up dealocking
373 * with a waiting writer.
375 _rcu_thread_offline();
376 mutex_lock(&rcu_registry_lock
);
377 cds_list_del(&URCU_TLS(rcu_reader
).node
);
378 mutex_unlock(&rcu_registry_lock
);
384 * Assertion disabled because call_rcu threads are now rcu
385 * readers, and left running at exit.
386 * assert(cds_list_empty(®istry));
390 DEFINE_RCU_FLAVOR(rcu_flavor
);
392 #include "urcu-call-rcu-impl.h"
393 #include "urcu-defer-impl.h"