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"
43 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
45 #include "urcu-qsbr.h"
48 void __attribute__((destructor
)) rcu_exit(void);
50 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
55 * Global grace period counter.
57 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
60 * Active attempts to check for reader Q.S. before calling futex().
62 #define RCU_QS_ACTIVE_ATTEMPTS 100
65 * Written to only by each individual reader. Read by both the reader and the
68 struct rcu_reader __thread rcu_reader
;
71 unsigned int yield_active
;
72 unsigned int __thread rand_yield
;
75 static CDS_LIST_HEAD(registry
);
77 static void mutex_lock(pthread_mutex_t
*mutex
)
81 #ifndef DISTRUST_SIGNALS_EXTREME
82 ret
= pthread_mutex_lock(mutex
);
84 perror("Error in pthread mutex lock");
87 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
88 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
89 if (ret
!= EBUSY
&& ret
!= EINTR
) {
90 printf("ret = %d, errno = %d\n", ret
, errno
);
91 perror("Error in pthread mutex lock");
96 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
99 static void mutex_unlock(pthread_mutex_t
*mutex
)
103 ret
= pthread_mutex_unlock(mutex
);
105 perror("Error in pthread mutex unlock");
111 * synchronize_rcu() waiting. Single thread.
113 static void wait_gp(void)
115 /* Read reader_gp before read futex */
117 if (uatomic_read(&gp_futex
) == -1)
118 futex_noasync(&gp_futex
, FUTEX_WAIT
, -1,
122 static void update_counter_and_wait(void)
124 CDS_LIST_HEAD(qsreaders
);
126 struct rcu_reader
*index
, *tmp
;
128 #if (CAA_BITS_PER_LONG < 64)
129 /* Switch parity: 0 -> 1, 1 -> 0 */
130 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
131 #else /* !(CAA_BITS_PER_LONG < 64) */
132 /* Increment current G.P. */
133 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
134 #endif /* !(CAA_BITS_PER_LONG < 64) */
137 * Must commit rcu_gp_ctr update to memory before waiting for
138 * quiescent state. Failure to do so could result in the writer
139 * waiting forever while new readers are always accessing data
140 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
141 * before load rcu_reader ctr.
146 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
147 * model easier to understand. It does not have a big performance impact
148 * anyway, given this is the write-side.
153 * Wait for each thread rcu_reader_qs_gp count to become 0.
157 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
158 uatomic_set(&gp_futex
, -1);
160 * Write futex before write waiting (the other side
161 * reads them in the opposite order).
164 cds_list_for_each_entry(index
, ®istry
, node
) {
165 _CMM_STORE_SHARED(index
->waiting
, 1);
167 /* Write futex before read reader_gp */
170 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
171 if (!rcu_gp_ongoing(&index
->ctr
))
172 cds_list_move(&index
->node
, &qsreaders
);
175 if (cds_list_empty(®istry
)) {
176 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
177 /* Read reader_gp before write futex */
179 uatomic_set(&gp_futex
, 0);
183 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
186 #ifndef HAS_INCOHERENT_CACHES
188 #else /* #ifndef HAS_INCOHERENT_CACHES */
190 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
194 /* put back the reader list in the registry */
195 cds_list_splice(&qsreaders
, ®istry
);
199 * Using a two-subphases algorithm for architectures with smaller than 64-bit
200 * long-size to ensure we do not encounter an overflow bug.
203 #if (CAA_BITS_PER_LONG < 64)
204 void synchronize_rcu(void)
206 unsigned long was_online
;
208 was_online
= rcu_reader
.ctr
;
210 /* All threads should read qparity before accessing data structure
211 * where new ptr points to. In the "then" case, rcu_thread_offline
212 * includes a memory barrier.
214 * Mark the writer thread offline to make sure we don't wait for
215 * our own quiescent state. This allows using synchronize_rcu()
216 * in threads registered as readers.
219 rcu_thread_offline();
223 mutex_lock(&rcu_gp_lock
);
225 if (cds_list_empty(®istry
))
229 * Wait for previous parity to be empty of readers.
231 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
234 * Must finish waiting for quiescent state for parity 0 before
235 * committing next rcu_gp_ctr update to memory. Failure to
236 * do so could result in the writer waiting forever while new
237 * readers are always accessing data (no progress). Enforce
238 * compiler-order of load rcu_reader ctr before store to
244 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
245 * model easier to understand. It does not have a big performance impact
246 * anyway, given this is the write-side.
251 * Wait for previous parity to be empty of readers.
253 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
255 mutex_unlock(&rcu_gp_lock
);
258 * Finish waiting for reader threads before letting the old ptr being
266 #else /* !(CAA_BITS_PER_LONG < 64) */
267 void synchronize_rcu(void)
269 unsigned long was_online
;
271 was_online
= rcu_reader
.ctr
;
274 * Mark the writer thread offline to make sure we don't wait for
275 * our own quiescent state. This allows using synchronize_rcu()
276 * in threads registered as readers.
279 rcu_thread_offline();
283 mutex_lock(&rcu_gp_lock
);
284 if (cds_list_empty(®istry
))
286 update_counter_and_wait();
288 mutex_unlock(&rcu_gp_lock
);
295 #endif /* !(CAA_BITS_PER_LONG < 64) */
298 * library wrappers to be used by non-LGPL compatible source code.
301 void rcu_read_lock(void)
306 void rcu_read_unlock(void)
311 void rcu_quiescent_state(void)
313 _rcu_quiescent_state();
316 void rcu_thread_offline(void)
318 _rcu_thread_offline();
321 void rcu_thread_online(void)
323 _rcu_thread_online();
326 void rcu_register_thread(void)
328 rcu_reader
.tid
= pthread_self();
329 assert(rcu_reader
.ctr
== 0);
331 mutex_lock(&rcu_gp_lock
);
332 cds_list_add(&rcu_reader
.node
, ®istry
);
333 mutex_unlock(&rcu_gp_lock
);
334 _rcu_thread_online();
337 void rcu_unregister_thread(void)
340 * We have to make the thread offline otherwise we end up dealocking
341 * with a waiting writer.
343 _rcu_thread_offline();
344 mutex_lock(&rcu_gp_lock
);
345 cds_list_del(&rcu_reader
.node
);
346 mutex_unlock(&rcu_gp_lock
);
351 assert(cds_list_empty(®istry
));
354 #include "urcu-call-rcu-impl.h"
355 #include "urcu-defer-impl.h"