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"
45 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
47 #include "urcu-qsbr.h"
50 void __attribute__((destructor
)) rcu_exit(void);
52 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
57 * Global grace period counter.
59 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
62 * Active attempts to check for reader Q.S. before calling futex().
64 #define RCU_QS_ACTIVE_ATTEMPTS 100
67 * Written to only by each individual reader. Read by both the reader and the
70 DEFINE_URCU_TLS(struct rcu_reader
, rcu_reader
);
73 unsigned int yield_active
;
74 DEFINE_URCU_TLS(unsigned int, rand_yield
);
77 static CDS_LIST_HEAD(registry
);
79 static void mutex_lock(pthread_mutex_t
*mutex
)
83 #ifndef DISTRUST_SIGNALS_EXTREME
84 ret
= pthread_mutex_lock(mutex
);
86 perror("Error in pthread mutex lock");
89 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
90 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
91 if (ret
!= EBUSY
&& ret
!= EINTR
) {
92 printf("ret = %d, errno = %d\n", ret
, errno
);
93 perror("Error in pthread mutex lock");
98 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
101 static void mutex_unlock(pthread_mutex_t
*mutex
)
105 ret
= pthread_mutex_unlock(mutex
);
107 perror("Error in pthread mutex unlock");
113 * synchronize_rcu() waiting. Single thread.
115 static void wait_gp(void)
117 /* Read reader_gp before read futex */
119 if (uatomic_read(&gp_futex
) == -1)
120 futex_noasync(&gp_futex
, FUTEX_WAIT
, -1,
124 static void update_counter_and_wait(void)
126 CDS_LIST_HEAD(qsreaders
);
128 struct rcu_reader
*index
, *tmp
;
130 #if (CAA_BITS_PER_LONG < 64)
131 /* Switch parity: 0 -> 1, 1 -> 0 */
132 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
133 #else /* !(CAA_BITS_PER_LONG < 64) */
134 /* Increment current G.P. */
135 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
136 #endif /* !(CAA_BITS_PER_LONG < 64) */
139 * Must commit rcu_gp_ctr update to memory before waiting for
140 * quiescent state. Failure to do so could result in the writer
141 * waiting forever while new readers are always accessing data
142 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
143 * before load URCU_TLS(rcu_reader).ctr.
148 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
149 * model easier to understand. It does not have a big performance impact
150 * anyway, given this is the write-side.
155 * Wait for each thread rcu_reader_qs_gp count to become 0.
159 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
160 uatomic_set(&gp_futex
, -1);
162 * Write futex before write waiting (the other side
163 * reads them in the opposite order).
166 cds_list_for_each_entry(index
, ®istry
, node
) {
167 _CMM_STORE_SHARED(index
->waiting
, 1);
169 /* Write futex before read reader_gp */
172 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
173 if (!rcu_gp_ongoing(&index
->ctr
))
174 cds_list_move(&index
->node
, &qsreaders
);
177 if (cds_list_empty(®istry
)) {
178 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
179 /* Read reader_gp before write futex */
181 uatomic_set(&gp_futex
, 0);
185 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
188 #ifndef HAS_INCOHERENT_CACHES
190 #else /* #ifndef HAS_INCOHERENT_CACHES */
192 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
196 /* put back the reader list in the registry */
197 cds_list_splice(&qsreaders
, ®istry
);
201 * Using a two-subphases algorithm for architectures with smaller than 64-bit
202 * long-size to ensure we do not encounter an overflow bug.
205 #if (CAA_BITS_PER_LONG < 64)
206 void synchronize_rcu(void)
208 unsigned long was_online
;
210 was_online
= URCU_TLS(rcu_reader
).ctr
;
212 /* All threads should read qparity before accessing data structure
213 * where new ptr points to. In the "then" case, rcu_thread_offline
214 * includes a memory barrier.
216 * Mark the writer thread offline to make sure we don't wait for
217 * our own quiescent state. This allows using synchronize_rcu()
218 * in threads registered as readers.
221 rcu_thread_offline();
225 mutex_lock(&rcu_gp_lock
);
227 if (cds_list_empty(®istry
))
231 * Wait for previous parity to be empty of readers.
233 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
236 * Must finish waiting for quiescent state for parity 0 before
237 * committing next rcu_gp_ctr update to memory. Failure to
238 * do so could result in the writer waiting forever while new
239 * readers are always accessing data (no progress). Enforce
240 * compiler-order of load URCU_TLS(rcu_reader).ctr before store to
246 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
247 * model easier to understand. It does not have a big performance impact
248 * anyway, given this is the write-side.
253 * Wait for previous parity to be empty of readers.
255 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
257 mutex_unlock(&rcu_gp_lock
);
260 * Finish waiting for reader threads before letting the old ptr being
268 #else /* !(CAA_BITS_PER_LONG < 64) */
269 void synchronize_rcu(void)
271 unsigned long was_online
;
273 was_online
= URCU_TLS(rcu_reader
).ctr
;
276 * Mark the writer thread offline to make sure we don't wait for
277 * our own quiescent state. This allows using synchronize_rcu()
278 * in threads registered as readers.
281 rcu_thread_offline();
285 mutex_lock(&rcu_gp_lock
);
286 if (cds_list_empty(®istry
))
288 update_counter_and_wait();
290 mutex_unlock(&rcu_gp_lock
);
297 #endif /* !(CAA_BITS_PER_LONG < 64) */
300 * library wrappers to be used by non-LGPL compatible source code.
303 void rcu_read_lock(void)
308 void rcu_read_unlock(void)
313 void rcu_quiescent_state(void)
315 _rcu_quiescent_state();
318 void rcu_thread_offline(void)
320 _rcu_thread_offline();
323 void rcu_thread_online(void)
325 _rcu_thread_online();
328 void rcu_register_thread(void)
330 URCU_TLS(rcu_reader
).tid
= pthread_self();
331 assert(URCU_TLS(rcu_reader
).ctr
== 0);
333 mutex_lock(&rcu_gp_lock
);
334 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
335 mutex_unlock(&rcu_gp_lock
);
336 _rcu_thread_online();
339 void rcu_unregister_thread(void)
342 * We have to make the thread offline otherwise we end up dealocking
343 * with a waiting writer.
345 _rcu_thread_offline();
346 mutex_lock(&rcu_gp_lock
);
347 cds_list_del(&URCU_TLS(rcu_reader
).node
);
348 mutex_unlock(&rcu_gp_lock
);
354 * Assertion disabled because call_rcu threads are now rcu
355 * readers, and left running at exit.
356 * assert(cds_list_empty(®istry));
360 DEFINE_RCU_FLAVOR(rcu_flavor
);
362 #include "urcu-call-rcu-impl.h"
363 #include "urcu-defer-impl.h"