4 * Userspace RCU QSBR library
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
35 #define BUILD_QSBR_LIB
36 #include "urcu-qsbr-static.h"
37 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
38 #include "urcu-qsbr.h"
40 static pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
45 * Global grace period counter.
47 unsigned long urcu_gp_ctr
= RCU_GP_ONLINE
;
50 * Written to only by each individual reader. Read by both the reader and the
53 struct urcu_reader __thread urcu_reader
;
56 unsigned int yield_active
;
57 unsigned int __thread rand_yield
;
60 static LIST_HEAD(registry
);
62 static void internal_urcu_lock(void)
66 #ifndef DISTRUST_SIGNALS_EXTREME
67 ret
= pthread_mutex_lock(&urcu_mutex
);
69 perror("Error in pthread mutex lock");
72 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
73 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
74 if (ret
!= EBUSY
&& ret
!= EINTR
) {
75 printf("ret = %d, errno = %d\n", ret
, errno
);
76 perror("Error in pthread mutex lock");
81 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
84 static void internal_urcu_unlock(void)
88 ret
= pthread_mutex_unlock(&urcu_mutex
);
90 perror("Error in pthread mutex unlock");
96 * synchronize_rcu() waiting. Single thread.
98 static void wait_gp(struct urcu_reader
*index
)
100 uatomic_dec(&gp_futex
);
101 smp_mb(); /* Write futex before read reader_gp */
102 if (!rcu_gp_ongoing(&index
->ctr
)) {
103 /* Read reader_gp before write futex */
105 /* Callbacks are queued, don't wait. */
106 uatomic_set(&gp_futex
, 0);
108 /* Read reader_gp before read futex */
110 if (uatomic_read(&gp_futex
) == -1)
111 futex(&gp_futex
, FUTEX_WAIT
, -1,
116 static void wait_for_quiescent_state(void)
118 struct urcu_reader
*index
;
120 if (list_empty(®istry
))
123 * Wait for each thread rcu_reader_qs_gp count to become 0.
125 list_for_each_entry(index
, ®istry
, head
) {
128 while (rcu_gp_ongoing(&index
->ctr
)) {
129 if (wait_loops
++ == RCU_QS_ACTIVE_ATTEMPTS
) {
132 #ifndef HAS_INCOHERENT_CACHES
134 #else /* #ifndef HAS_INCOHERENT_CACHES */
136 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
143 * Using a two-subphases algorithm for architectures with smaller than 64-bit
144 * long-size to ensure we do not encounter an overflow bug.
147 #if (BITS_PER_LONG < 64)
149 * called with urcu_mutex held.
151 static void switch_next_urcu_qparity(void)
153 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR
);
156 void synchronize_rcu(void)
158 unsigned long was_online
;
160 was_online
= urcu_reader
.ctr
;
162 /* All threads should read qparity before accessing data structure
163 * where new ptr points to.
165 /* Write new ptr before changing the qparity */
169 * Mark the writer thread offline to make sure we don't wait for
170 * our own quiescent state. This allows using synchronize_rcu() in
171 * threads registered as readers.
174 STORE_SHARED(urcu_reader
.ctr
, 0);
176 internal_urcu_lock();
178 switch_next_urcu_qparity(); /* 0 -> 1 */
181 * Must commit qparity update to memory before waiting for parity
182 * 0 quiescent state. Failure to do so could result in the writer
183 * waiting forever while new readers are always accessing data (no
185 * Ensured by STORE_SHARED and LOAD_SHARED.
189 * Wait for previous parity to be empty of readers.
191 wait_for_quiescent_state(); /* Wait readers in parity 0 */
194 * Must finish waiting for quiescent state for parity 0 before
195 * committing qparity update to memory. Failure to do so could result in
196 * the writer waiting forever while new readers are always accessing
197 * data (no progress).
198 * Ensured by STORE_SHARED and LOAD_SHARED.
201 switch_next_urcu_qparity(); /* 1 -> 0 */
204 * Must commit qparity update to memory before waiting for parity
205 * 1 quiescent state. Failure to do so could result in the writer
206 * waiting forever while new readers are always accessing data (no
208 * Ensured by STORE_SHARED and LOAD_SHARED.
212 * Wait for previous parity to be empty of readers.
214 wait_for_quiescent_state(); /* Wait readers in parity 1 */
216 internal_urcu_unlock();
219 * Finish waiting for reader threads before letting the old ptr being
223 _STORE_SHARED(urcu_reader
.ctr
, LOAD_SHARED(urcu_gp_ctr
));
226 #else /* !(BITS_PER_LONG < 64) */
227 void synchronize_rcu(void)
229 unsigned long was_online
;
231 was_online
= urcu_reader
.ctr
;
234 * Mark the writer thread offline to make sure we don't wait for
235 * our own quiescent state. This allows using synchronize_rcu() in
236 * threads registered as readers.
240 STORE_SHARED(urcu_reader
.ctr
, 0);
242 internal_urcu_lock();
243 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
+ RCU_GP_CTR
);
244 wait_for_quiescent_state();
245 internal_urcu_unlock();
248 _STORE_SHARED(urcu_reader
.ctr
, LOAD_SHARED(urcu_gp_ctr
));
251 #endif /* !(BITS_PER_LONG < 64) */
254 * library wrappers to be used by non-LGPL compatible source code.
257 void rcu_read_lock(void)
262 void rcu_read_unlock(void)
267 void *rcu_dereference(void *p
)
269 return _rcu_dereference(p
);
272 void *rcu_assign_pointer_sym(void **p
, void *v
)
275 return STORE_SHARED(p
, v
);
278 void *rcu_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
281 return uatomic_cmpxchg(p
, old
, _new
);
284 void *rcu_xchg_pointer_sym(void **p
, void *v
)
287 return uatomic_xchg(p
, v
);
290 void *rcu_publish_content_sym(void **p
, void *v
)
294 oldptr
= _rcu_xchg_pointer(p
, v
);
299 void rcu_quiescent_state(void)
301 _rcu_quiescent_state();
304 void rcu_thread_offline(void)
306 _rcu_thread_offline();
309 void rcu_thread_online(void)
311 _rcu_thread_online();
314 void rcu_register_thread(void)
316 urcu_reader
.tid
= pthread_self();
317 assert(urcu_reader
.ctr
== 0);
319 internal_urcu_lock();
320 list_add(&urcu_reader
.head
, ®istry
);
321 internal_urcu_unlock();
322 _rcu_thread_online();
325 void rcu_unregister_thread(void)
328 * We have to make the thread offline otherwise we end up dealocking
329 * with a waiting writer.
331 _rcu_thread_offline();
332 internal_urcu_lock();
333 list_del(&urcu_reader
.head
);
334 internal_urcu_unlock();