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 * Written to only by each individual reader. Read by both the reader and the
59 struct rcu_reader __thread rcu_reader
;
62 unsigned int yield_active
;
63 unsigned int __thread rand_yield
;
66 static CDS_LIST_HEAD(registry
);
68 static void mutex_lock(pthread_mutex_t
*mutex
)
72 #ifndef DISTRUST_SIGNALS_EXTREME
73 ret
= pthread_mutex_lock(mutex
);
75 perror("Error in pthread mutex lock");
78 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
79 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
80 if (ret
!= EBUSY
&& ret
!= EINTR
) {
81 printf("ret = %d, errno = %d\n", ret
, errno
);
82 perror("Error in pthread mutex lock");
87 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
90 static void mutex_unlock(pthread_mutex_t
*mutex
)
94 ret
= pthread_mutex_unlock(mutex
);
96 perror("Error in pthread mutex unlock");
102 * synchronize_rcu() waiting. Single thread.
104 static void wait_gp(void)
106 /* Read reader_gp before read futex */
108 if (uatomic_read(&gp_futex
) == -1)
109 futex_noasync(&gp_futex
, FUTEX_WAIT
, -1,
113 static void update_counter_and_wait(void)
115 CDS_LIST_HEAD(qsreaders
);
117 struct rcu_reader
*index
, *tmp
;
119 #if (CAA_BITS_PER_LONG < 64)
120 /* Switch parity: 0 -> 1, 1 -> 0 */
121 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
122 #else /* !(CAA_BITS_PER_LONG < 64) */
123 /* Increment current G.P. */
124 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
125 #endif /* !(CAA_BITS_PER_LONG < 64) */
128 * Must commit rcu_gp_ctr update to memory before waiting for
129 * quiescent state. Failure to do so could result in the writer
130 * waiting forever while new readers are always accessing data
131 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
132 * before load rcu_reader ctr.
137 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
138 * model easier to understand. It does not have a big performance impact
139 * anyway, given this is the write-side.
144 * Wait for each thread rcu_reader_qs_gp count to become 0.
148 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
149 uatomic_dec(&gp_futex
);
150 /* Write futex before read reader_gp */
154 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
155 if (!rcu_gp_ongoing(&index
->ctr
))
156 cds_list_move(&index
->node
, &qsreaders
);
159 if (cds_list_empty(®istry
)) {
160 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
161 /* Read reader_gp before write futex */
163 uatomic_set(&gp_futex
, 0);
167 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
170 #ifndef HAS_INCOHERENT_CACHES
172 #else /* #ifndef HAS_INCOHERENT_CACHES */
174 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
178 /* put back the reader list in the registry */
179 cds_list_splice(&qsreaders
, ®istry
);
183 * Using a two-subphases algorithm for architectures with smaller than 64-bit
184 * long-size to ensure we do not encounter an overflow bug.
187 #if (CAA_BITS_PER_LONG < 64)
188 void synchronize_rcu(void)
190 unsigned long was_online
;
192 was_online
= rcu_reader
.ctr
;
194 /* All threads should read qparity before accessing data structure
195 * where new ptr points to.
197 /* Write new ptr before changing the qparity */
201 * Mark the writer thread offline to make sure we don't wait for
202 * our own quiescent state. This allows using synchronize_rcu()
203 * in threads registered as readers.
206 CMM_STORE_SHARED(rcu_reader
.ctr
, 0);
208 mutex_lock(&rcu_gp_lock
);
210 if (cds_list_empty(®istry
))
214 * Wait for previous parity to be empty of readers.
216 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
219 * Must finish waiting for quiescent state for parity 0 before
220 * committing next rcu_gp_ctr update to memory. Failure to
221 * do so could result in the writer waiting forever while new
222 * readers are always accessing data (no progress). Enforce
223 * compiler-order of load rcu_reader ctr before store to
229 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
230 * model easier to understand. It does not have a big performance impact
231 * anyway, given this is the write-side.
236 * Wait for previous parity to be empty of readers.
238 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
240 mutex_unlock(&rcu_gp_lock
);
243 * Finish waiting for reader threads before letting the old ptr being
247 _CMM_STORE_SHARED(rcu_reader
.ctr
,
248 CMM_LOAD_SHARED(rcu_gp_ctr
));
251 #else /* !(CAA_BITS_PER_LONG < 64) */
252 void synchronize_rcu(void)
254 unsigned long was_online
;
256 was_online
= rcu_reader
.ctr
;
259 * Mark the writer thread offline to make sure we don't wait for
260 * our own quiescent state. This allows using synchronize_rcu()
261 * in threads registered as readers.
265 CMM_STORE_SHARED(rcu_reader
.ctr
, 0);
267 mutex_lock(&rcu_gp_lock
);
268 if (cds_list_empty(®istry
))
270 update_counter_and_wait();
272 mutex_unlock(&rcu_gp_lock
);
275 _CMM_STORE_SHARED(rcu_reader
.ctr
,
276 CMM_LOAD_SHARED(rcu_gp_ctr
));
279 #endif /* !(CAA_BITS_PER_LONG < 64) */
282 * library wrappers to be used by non-LGPL compatible source code.
285 void rcu_read_lock(void)
290 void rcu_read_unlock(void)
295 void rcu_quiescent_state(void)
297 _rcu_quiescent_state();
300 void rcu_thread_offline(void)
302 _rcu_thread_offline();
305 void rcu_thread_online(void)
307 _rcu_thread_online();
310 void rcu_register_thread(void)
312 rcu_reader
.tid
= pthread_self();
313 assert(rcu_reader
.ctr
== 0);
315 mutex_lock(&rcu_gp_lock
);
316 cds_list_add(&rcu_reader
.node
, ®istry
);
317 mutex_unlock(&rcu_gp_lock
);
318 _rcu_thread_online();
321 void rcu_unregister_thread(void)
324 * We have to make the thread offline otherwise we end up dealocking
325 * with a waiting writer.
327 _rcu_thread_offline();
328 mutex_lock(&rcu_gp_lock
);
329 cds_list_del(&rcu_reader
.node
);
330 mutex_unlock(&rcu_gp_lock
);
335 assert(cds_list_empty(®istry
));
338 #include "urcu-call-rcu-impl.h"
339 #include "urcu-defer-impl.h"