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.
37 #include "urcu/map/urcu-qsbr.h"
39 #define BUILD_QSBR_LIB
40 #include "urcu/static/urcu-qsbr.h"
41 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42 #include "urcu-qsbr.h"
44 void __attribute__((destructor
)) rcu_exit(void);
46 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 * Global grace period counter.
53 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
56 * Active attempts to check for reader Q.S. before calling futex().
58 #define RCU_QS_ACTIVE_ATTEMPTS 100
61 * Written to only by each individual reader. Read by both the reader and the
64 struct rcu_reader __thread rcu_reader
;
67 unsigned int yield_active
;
68 unsigned int __thread rand_yield
;
71 static CDS_LIST_HEAD(registry
);
73 static void mutex_lock(pthread_mutex_t
*mutex
)
77 #ifndef DISTRUST_SIGNALS_EXTREME
78 ret
= pthread_mutex_lock(mutex
);
80 perror("Error in pthread mutex lock");
83 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
84 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
85 if (ret
!= EBUSY
&& ret
!= EINTR
) {
86 printf("ret = %d, errno = %d\n", ret
, errno
);
87 perror("Error in pthread mutex lock");
92 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
95 static void mutex_unlock(pthread_mutex_t
*mutex
)
99 ret
= pthread_mutex_unlock(mutex
);
101 perror("Error in pthread mutex unlock");
107 * synchronize_rcu() waiting. Single thread.
109 static void wait_gp(void)
111 /* Read reader_gp before read futex */
113 if (uatomic_read(&gp_futex
) == -1)
114 futex_noasync(&gp_futex
, FUTEX_WAIT
, -1,
118 static void update_counter_and_wait(void)
120 CDS_LIST_HEAD(qsreaders
);
122 struct rcu_reader
*index
, *tmp
;
124 #if (CAA_BITS_PER_LONG < 64)
125 /* Switch parity: 0 -> 1, 1 -> 0 */
126 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
127 #else /* !(CAA_BITS_PER_LONG < 64) */
128 /* Increment current G.P. */
129 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
130 #endif /* !(CAA_BITS_PER_LONG < 64) */
133 * Must commit rcu_gp_ctr update to memory before waiting for
134 * quiescent state. Failure to do so could result in the writer
135 * waiting forever while new readers are always accessing data
136 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
137 * before load rcu_reader ctr.
142 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
143 * model easier to understand. It does not have a big performance impact
144 * anyway, given this is the write-side.
149 * Wait for each thread rcu_reader_qs_gp count to become 0.
153 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
154 uatomic_dec(&gp_futex
);
155 /* Write futex before read reader_gp */
159 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
160 if (!rcu_gp_ongoing(&index
->ctr
))
161 cds_list_move(&index
->node
, &qsreaders
);
164 if (cds_list_empty(®istry
)) {
165 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
166 /* Read reader_gp before write futex */
168 uatomic_set(&gp_futex
, 0);
172 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
175 #ifndef HAS_INCOHERENT_CACHES
177 #else /* #ifndef HAS_INCOHERENT_CACHES */
179 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
183 /* put back the reader list in the registry */
184 cds_list_splice(&qsreaders
, ®istry
);
188 * Using a two-subphases algorithm for architectures with smaller than 64-bit
189 * long-size to ensure we do not encounter an overflow bug.
192 #if (CAA_BITS_PER_LONG < 64)
193 void synchronize_rcu(void)
195 unsigned long was_online
;
197 was_online
= rcu_reader
.ctr
;
199 /* All threads should read qparity before accessing data structure
200 * where new ptr points to.
202 /* Write new ptr before changing the qparity */
206 * Mark the writer thread offline to make sure we don't wait for
207 * our own quiescent state. This allows using synchronize_rcu()
208 * in threads registered as readers.
211 CMM_STORE_SHARED(rcu_reader
.ctr
, 0);
213 mutex_lock(&rcu_gp_lock
);
215 if (cds_list_empty(®istry
))
219 * Wait for previous parity to be empty of readers.
221 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
224 * Must finish waiting for quiescent state for parity 0 before
225 * committing next rcu_gp_ctr update to memory. Failure to
226 * do so could result in the writer waiting forever while new
227 * readers are always accessing data (no progress). Enforce
228 * compiler-order of load rcu_reader ctr before store to
234 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
235 * model easier to understand. It does not have a big performance impact
236 * anyway, given this is the write-side.
241 * Wait for previous parity to be empty of readers.
243 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
245 mutex_unlock(&rcu_gp_lock
);
248 * Finish waiting for reader threads before letting the old ptr being
252 _CMM_STORE_SHARED(rcu_reader
.ctr
,
253 CMM_LOAD_SHARED(rcu_gp_ctr
));
256 #else /* !(CAA_BITS_PER_LONG < 64) */
257 void synchronize_rcu(void)
259 unsigned long was_online
;
261 was_online
= rcu_reader
.ctr
;
264 * Mark the writer thread offline to make sure we don't wait for
265 * our own quiescent state. This allows using synchronize_rcu()
266 * in threads registered as readers.
270 CMM_STORE_SHARED(rcu_reader
.ctr
, 0);
272 mutex_lock(&rcu_gp_lock
);
273 if (cds_list_empty(®istry
))
275 update_counter_and_wait();
277 mutex_unlock(&rcu_gp_lock
);
280 _CMM_STORE_SHARED(rcu_reader
.ctr
,
281 CMM_LOAD_SHARED(rcu_gp_ctr
));
284 #endif /* !(CAA_BITS_PER_LONG < 64) */
287 * library wrappers to be used by non-LGPL compatible source code.
290 void rcu_read_lock(void)
295 void rcu_read_unlock(void)
300 void rcu_quiescent_state(void)
302 _rcu_quiescent_state();
305 void rcu_thread_offline(void)
307 _rcu_thread_offline();
310 void rcu_thread_online(void)
312 _rcu_thread_online();
315 void rcu_register_thread(void)
317 rcu_reader
.tid
= pthread_self();
318 assert(rcu_reader
.ctr
== 0);
320 mutex_lock(&rcu_gp_lock
);
321 cds_list_add(&rcu_reader
.node
, ®istry
);
322 mutex_unlock(&rcu_gp_lock
);
323 _rcu_thread_online();
326 void rcu_unregister_thread(void)
329 * We have to make the thread offline otherwise we end up dealocking
330 * with a waiting writer.
332 _rcu_thread_offline();
333 mutex_lock(&rcu_gp_lock
);
334 cds_list_del(&rcu_reader
.node
);
335 mutex_unlock(&rcu_gp_lock
);
340 assert(cds_list_empty(®istry
));
343 #include "urcu-call-rcu-impl.h"
344 #include "urcu-defer-impl.h"