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.
36 #include "urcu/map/urcu-qsbr.h"
38 #define BUILD_QSBR_LIB
39 #include "urcu-qsbr-static.h"
40 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
41 #include "urcu-qsbr.h"
43 void __attribute__((destructor
)) rcu_exit(void);
45 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
50 * Global grace period counter.
52 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
55 * Written to only by each individual reader. Read by both the reader and the
58 struct rcu_reader __thread rcu_reader
;
61 unsigned int yield_active
;
62 unsigned int __thread rand_yield
;
65 static CDS_LIST_HEAD(registry
);
67 static void mutex_lock(pthread_mutex_t
*mutex
)
71 #ifndef DISTRUST_SIGNALS_EXTREME
72 ret
= pthread_mutex_lock(mutex
);
74 perror("Error in pthread mutex lock");
77 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
78 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
79 if (ret
!= EBUSY
&& ret
!= EINTR
) {
80 printf("ret = %d, errno = %d\n", ret
, errno
);
81 perror("Error in pthread mutex lock");
86 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
89 static void mutex_unlock(pthread_mutex_t
*mutex
)
93 ret
= pthread_mutex_unlock(mutex
);
95 perror("Error in pthread mutex unlock");
101 * synchronize_rcu() waiting. Single thread.
103 static void wait_gp(void)
105 /* Read reader_gp before read futex */
107 if (uatomic_read(&gp_futex
) == -1)
108 futex_noasync(&gp_futex
, FUTEX_WAIT
, -1,
112 static void update_counter_and_wait(void)
114 CDS_LIST_HEAD(qsreaders
);
116 struct rcu_reader
*index
, *tmp
;
118 #if (CAA_BITS_PER_LONG < 64)
119 /* Switch parity: 0 -> 1, 1 -> 0 */
120 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
121 #else /* !(CAA_BITS_PER_LONG < 64) */
122 /* Increment current G.P. */
123 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
124 #endif /* !(CAA_BITS_PER_LONG < 64) */
127 * Must commit rcu_gp_ctr update to memory before waiting for
128 * quiescent state. Failure to do so could result in the writer
129 * waiting forever while new readers are always accessing data
130 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
131 * before load rcu_reader ctr.
136 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
137 * model easier to understand. It does not have a big performance impact
138 * anyway, given this is the write-side.
143 * Wait for each thread rcu_reader_qs_gp count to become 0.
147 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
148 uatomic_dec(&gp_futex
);
149 /* Write futex before read reader_gp */
153 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
154 if (!rcu_gp_ongoing(&index
->ctr
))
155 cds_list_move(&index
->node
, &qsreaders
);
158 if (cds_list_empty(®istry
)) {
159 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
160 /* Read reader_gp before write futex */
162 uatomic_set(&gp_futex
, 0);
166 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
169 #ifndef HAS_INCOHERENT_CACHES
171 #else /* #ifndef HAS_INCOHERENT_CACHES */
173 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
177 /* put back the reader list in the registry */
178 cds_list_splice(&qsreaders
, ®istry
);
182 * Using a two-subphases algorithm for architectures with smaller than 64-bit
183 * long-size to ensure we do not encounter an overflow bug.
186 #if (CAA_BITS_PER_LONG < 64)
187 void synchronize_rcu(void)
189 unsigned long was_online
;
191 was_online
= rcu_reader
.ctr
;
193 /* All threads should read qparity before accessing data structure
194 * where new ptr points to.
196 /* Write new ptr before changing the qparity */
200 * Mark the writer thread offline to make sure we don't wait for
201 * our own quiescent state. This allows using synchronize_rcu()
202 * in threads registered as readers.
205 CMM_STORE_SHARED(rcu_reader
.ctr
, 0);
207 mutex_lock(&rcu_gp_lock
);
209 if (cds_list_empty(®istry
))
213 * Wait for previous parity to be empty of readers.
215 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
218 * Must finish waiting for quiescent state for parity 0 before
219 * committing next rcu_gp_ctr update to memory. Failure to
220 * do so could result in the writer waiting forever while new
221 * readers are always accessing data (no progress). Enforce
222 * compiler-order of load rcu_reader ctr before store to
228 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
229 * model easier to understand. It does not have a big performance impact
230 * anyway, given this is the write-side.
235 * Wait for previous parity to be empty of readers.
237 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
239 mutex_unlock(&rcu_gp_lock
);
242 * Finish waiting for reader threads before letting the old ptr being
246 _CMM_STORE_SHARED(rcu_reader
.ctr
,
247 CMM_LOAD_SHARED(rcu_gp_ctr
));
250 #else /* !(CAA_BITS_PER_LONG < 64) */
251 void synchronize_rcu(void)
253 unsigned long was_online
;
255 was_online
= rcu_reader
.ctr
;
258 * Mark the writer thread offline to make sure we don't wait for
259 * our own quiescent state. This allows using synchronize_rcu()
260 * in threads registered as readers.
264 CMM_STORE_SHARED(rcu_reader
.ctr
, 0);
266 mutex_lock(&rcu_gp_lock
);
267 if (cds_list_empty(®istry
))
269 update_counter_and_wait();
271 mutex_unlock(&rcu_gp_lock
);
274 _CMM_STORE_SHARED(rcu_reader
.ctr
,
275 CMM_LOAD_SHARED(rcu_gp_ctr
));
278 #endif /* !(CAA_BITS_PER_LONG < 64) */
281 * library wrappers to be used by non-LGPL compatible source code.
284 void rcu_read_lock(void)
289 void rcu_read_unlock(void)
294 void rcu_quiescent_state(void)
296 _rcu_quiescent_state();
299 void rcu_thread_offline(void)
301 _rcu_thread_offline();
304 void rcu_thread_online(void)
306 _rcu_thread_online();
309 void rcu_register_thread(void)
311 rcu_reader
.tid
= pthread_self();
312 assert(rcu_reader
.ctr
== 0);
314 mutex_lock(&rcu_gp_lock
);
315 cds_list_add(&rcu_reader
.node
, ®istry
);
316 mutex_unlock(&rcu_gp_lock
);
317 _rcu_thread_online();
320 void rcu_unregister_thread(void)
323 * We have to make the thread offline otherwise we end up dealocking
324 * with a waiting writer.
326 _rcu_thread_offline();
327 mutex_lock(&rcu_gp_lock
);
328 cds_list_del(&rcu_reader
.node
);
329 mutex_unlock(&rcu_gp_lock
);
334 assert(cds_list_empty(®istry
));
337 #include "urcu-call-rcu-impl.h"
338 #include "urcu-defer-impl.h"