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/wfcqueue.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);
54 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
59 * Global grace period counter.
61 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
64 * Active attempts to check for reader Q.S. before calling futex().
66 #define RCU_QS_ACTIVE_ATTEMPTS 100
69 * Written to only by each individual reader. Read by both the reader and the
72 DEFINE_URCU_TLS(struct rcu_reader
, rcu_reader
);
75 unsigned int rcu_yield_active
;
76 DEFINE_URCU_TLS(unsigned int, rcu_rand_yield
);
79 static CDS_LIST_HEAD(registry
);
81 static void mutex_lock(pthread_mutex_t
*mutex
)
85 #ifndef DISTRUST_SIGNALS_EXTREME
86 ret
= pthread_mutex_lock(mutex
);
89 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
90 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
91 if (ret
!= EBUSY
&& ret
!= EINTR
)
95 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
98 static void mutex_unlock(pthread_mutex_t
*mutex
)
102 ret
= pthread_mutex_unlock(mutex
);
108 * synchronize_rcu() waiting. Single thread.
110 static void wait_gp(void)
112 /* Read reader_gp before read futex */
114 if (uatomic_read(&rcu_gp_futex
) == -1)
115 futex_noasync(&rcu_gp_futex
, FUTEX_WAIT
, -1,
119 static void wait_for_readers(void)
121 CDS_LIST_HEAD(qsreaders
);
123 struct rcu_reader
*index
, *tmp
;
126 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
127 * indicate quiescence (offline), or for them to observe the
128 * current rcu_gp_ctr value.
132 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
133 uatomic_set(&rcu_gp_futex
, -1);
135 * Write futex before write waiting (the other side
136 * reads them in the opposite order).
139 cds_list_for_each_entry(index
, ®istry
, node
) {
140 _CMM_STORE_SHARED(index
->waiting
, 1);
142 /* Write futex before read reader_gp */
145 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
146 if (!rcu_gp_ongoing(&index
->ctr
))
147 cds_list_move(&index
->node
, &qsreaders
);
150 if (cds_list_empty(®istry
)) {
151 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
152 /* Read reader_gp before write futex */
154 uatomic_set(&rcu_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 cds_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 (CAA_BITS_PER_LONG < 64)
179 void synchronize_rcu(void)
181 unsigned long was_online
;
183 was_online
= URCU_TLS(rcu_reader
).ctr
;
185 /* All threads should read qparity before accessing data structure
186 * where new ptr points to. In the "then" case, rcu_thread_offline
187 * includes a memory barrier.
189 * Mark the writer thread offline to make sure we don't wait for
190 * our own quiescent state. This allows using synchronize_rcu()
191 * in threads registered as readers.
194 rcu_thread_offline();
198 mutex_lock(&rcu_gp_lock
);
200 if (cds_list_empty(®istry
))
204 * Wait for readers to observe original parity or be quiescent.
209 * Must finish waiting for quiescent state for original parity
210 * before committing next rcu_gp_ctr update to memory. Failure
211 * to do so could result in the writer waiting forever while new
212 * readers are always accessing data (no progress). Enforce
213 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
219 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
220 * model easier to understand. It does not have a big performance impact
221 * anyway, given this is the write-side.
225 /* Switch parity: 0 -> 1, 1 -> 0 */
226 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
229 * Must commit rcu_gp_ctr update to memory before waiting for
230 * quiescent state. Failure to do so could result in the writer
231 * waiting forever while new readers are always accessing data
232 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
233 * before load URCU_TLS(rcu_reader).ctr.
238 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
239 * model easier to understand. It does not have a big performance impact
240 * anyway, given this is the write-side.
245 * Wait for readers to observe new parity or be quiescent.
249 mutex_unlock(&rcu_gp_lock
);
252 * Finish waiting for reader threads before letting the old ptr being
260 #else /* !(CAA_BITS_PER_LONG < 64) */
261 void synchronize_rcu(void)
263 unsigned long was_online
;
265 was_online
= URCU_TLS(rcu_reader
).ctr
;
268 * Mark the writer thread offline to make sure we don't wait for
269 * our own quiescent state. This allows using synchronize_rcu()
270 * in threads registered as readers.
273 rcu_thread_offline();
277 mutex_lock(&rcu_gp_lock
);
278 if (cds_list_empty(®istry
))
281 /* Increment current G.P. */
282 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
285 * Must commit rcu_gp_ctr update to memory before waiting for
286 * quiescent state. Failure to do so could result in the writer
287 * waiting forever while new readers are always accessing data
288 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
289 * before load URCU_TLS(rcu_reader).ctr.
294 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
295 * model easier to understand. It does not have a big performance impact
296 * anyway, given this is the write-side.
301 * Wait for readers to observe new count of be quiescent.
305 mutex_unlock(&rcu_gp_lock
);
312 #endif /* !(CAA_BITS_PER_LONG < 64) */
315 * library wrappers to be used by non-LGPL compatible source code.
318 void rcu_read_lock(void)
323 void rcu_read_unlock(void)
328 void rcu_quiescent_state(void)
330 _rcu_quiescent_state();
333 void rcu_thread_offline(void)
335 _rcu_thread_offline();
338 void rcu_thread_online(void)
340 _rcu_thread_online();
343 void rcu_register_thread(void)
345 URCU_TLS(rcu_reader
).tid
= pthread_self();
346 assert(URCU_TLS(rcu_reader
).ctr
== 0);
348 mutex_lock(&rcu_gp_lock
);
349 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
350 mutex_unlock(&rcu_gp_lock
);
351 _rcu_thread_online();
354 void rcu_unregister_thread(void)
357 * We have to make the thread offline otherwise we end up dealocking
358 * with a waiting writer.
360 _rcu_thread_offline();
361 mutex_lock(&rcu_gp_lock
);
362 cds_list_del(&URCU_TLS(rcu_reader
).node
);
363 mutex_unlock(&rcu_gp_lock
);
369 * Assertion disabled because call_rcu threads are now rcu
370 * readers, and left running at exit.
371 * assert(cds_list_empty(®istry));
375 DEFINE_RCU_FLAVOR(rcu_flavor
);
377 #include "urcu-call-rcu-impl.h"
378 #include "urcu-defer-impl.h"