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 void __attribute__((destructor
)) rcu_exit(void);
42 static pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
47 * Global grace period counter.
49 unsigned long urcu_gp_ctr
= RCU_GP_ONLINE
;
52 * Written to only by each individual reader. Read by both the reader and the
55 struct urcu_reader __thread urcu_reader
;
58 unsigned int yield_active
;
59 unsigned int __thread rand_yield
;
62 static LIST_HEAD(registry
);
64 static void internal_urcu_lock(void)
68 #ifndef DISTRUST_SIGNALS_EXTREME
69 ret
= pthread_mutex_lock(&urcu_mutex
);
71 perror("Error in pthread mutex lock");
74 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
75 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
76 if (ret
!= EBUSY
&& ret
!= EINTR
) {
77 printf("ret = %d, errno = %d\n", ret
, errno
);
78 perror("Error in pthread mutex lock");
83 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
86 static void internal_urcu_unlock(void)
90 ret
= pthread_mutex_unlock(&urcu_mutex
);
92 perror("Error in pthread mutex unlock");
98 * synchronize_rcu() waiting. Single thread.
100 static void wait_gp(struct urcu_reader
*index
)
102 uatomic_dec(&gp_futex
);
103 smp_mb(); /* Write futex before read reader_gp */
104 if (!rcu_gp_ongoing(&index
->ctr
)) {
105 /* Read reader_gp before write futex */
107 /* Callbacks are queued, don't wait. */
108 uatomic_set(&gp_futex
, 0);
110 /* Read reader_gp before read futex */
112 if (uatomic_read(&gp_futex
) == -1)
113 futex(&gp_futex
, FUTEX_WAIT
, -1,
118 static void wait_for_quiescent_state(void)
120 struct urcu_reader
*index
;
122 if (list_empty(®istry
))
125 * Wait for each thread rcu_reader_qs_gp count to become 0.
127 list_for_each_entry(index
, ®istry
, head
) {
130 while (rcu_gp_ongoing(&index
->ctr
)) {
131 if (wait_loops
++ == RCU_QS_ACTIVE_ATTEMPTS
) {
134 #ifndef HAS_INCOHERENT_CACHES
136 #else /* #ifndef HAS_INCOHERENT_CACHES */
138 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
145 * Using a two-subphases algorithm for architectures with smaller than 64-bit
146 * long-size to ensure we do not encounter an overflow bug.
149 #if (BITS_PER_LONG < 64)
151 * called with urcu_mutex held.
153 static void switch_next_urcu_qparity(void)
155 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR
);
158 void synchronize_rcu(void)
160 unsigned long was_online
;
162 was_online
= urcu_reader
.ctr
;
164 /* All threads should read qparity before accessing data structure
165 * where new ptr points to.
167 /* Write new ptr before changing the qparity */
171 * Mark the writer thread offline to make sure we don't wait for
172 * our own quiescent state. This allows using synchronize_rcu() in
173 * threads registered as readers.
176 STORE_SHARED(urcu_reader
.ctr
, 0);
178 internal_urcu_lock();
180 switch_next_urcu_qparity(); /* 0 -> 1 */
183 * Must commit qparity update to memory before waiting for parity
184 * 0 quiescent state. Failure to do so could result in the writer
185 * waiting forever while new readers are always accessing data (no
187 * Ensured by STORE_SHARED and LOAD_SHARED.
191 * Wait for previous parity to be empty of readers.
193 wait_for_quiescent_state(); /* Wait readers in parity 0 */
196 * Must finish waiting for quiescent state for parity 0 before
197 * committing qparity update to memory. Failure to do so could result in
198 * the writer waiting forever while new readers are always accessing
199 * data (no progress).
200 * Ensured by STORE_SHARED and LOAD_SHARED.
203 switch_next_urcu_qparity(); /* 1 -> 0 */
206 * Must commit qparity update to memory before waiting for parity
207 * 1 quiescent state. Failure to do so could result in the writer
208 * waiting forever while new readers are always accessing data (no
210 * Ensured by STORE_SHARED and LOAD_SHARED.
214 * Wait for previous parity to be empty of readers.
216 wait_for_quiescent_state(); /* Wait readers in parity 1 */
218 internal_urcu_unlock();
221 * Finish waiting for reader threads before letting the old ptr being
225 _STORE_SHARED(urcu_reader
.ctr
, LOAD_SHARED(urcu_gp_ctr
));
228 #else /* !(BITS_PER_LONG < 64) */
229 void synchronize_rcu(void)
231 unsigned long was_online
;
233 was_online
= urcu_reader
.ctr
;
236 * Mark the writer thread offline to make sure we don't wait for
237 * our own quiescent state. This allows using synchronize_rcu() in
238 * threads registered as readers.
242 STORE_SHARED(urcu_reader
.ctr
, 0);
244 internal_urcu_lock();
245 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
+ RCU_GP_CTR
);
246 wait_for_quiescent_state();
247 internal_urcu_unlock();
250 _STORE_SHARED(urcu_reader
.ctr
, LOAD_SHARED(urcu_gp_ctr
));
253 #endif /* !(BITS_PER_LONG < 64) */
256 * library wrappers to be used by non-LGPL compatible source code.
259 void rcu_read_lock(void)
264 void rcu_read_unlock(void)
269 void *rcu_dereference(void *p
)
271 return _rcu_dereference(p
);
274 void *rcu_assign_pointer_sym(void **p
, void *v
)
277 return STORE_SHARED(p
, v
);
280 void *rcu_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
283 return uatomic_cmpxchg(p
, old
, _new
);
286 void *rcu_xchg_pointer_sym(void **p
, void *v
)
289 return uatomic_xchg(p
, v
);
292 void *rcu_publish_content_sym(void **p
, void *v
)
296 oldptr
= _rcu_xchg_pointer(p
, v
);
301 void rcu_quiescent_state(void)
303 _rcu_quiescent_state();
306 void rcu_thread_offline(void)
308 _rcu_thread_offline();
311 void rcu_thread_online(void)
313 _rcu_thread_online();
316 void rcu_register_thread(void)
318 urcu_reader
.tid
= pthread_self();
319 assert(urcu_reader
.ctr
== 0);
321 internal_urcu_lock();
322 list_add(&urcu_reader
.head
, ®istry
);
323 internal_urcu_unlock();
324 _rcu_thread_online();
327 void rcu_unregister_thread(void)
330 * We have to make the thread offline otherwise we end up dealocking
331 * with a waiting writer.
333 _rcu_thread_offline();
334 internal_urcu_lock();
335 list_del(&urcu_reader
.head
);
336 internal_urcu_unlock();
341 assert(list_empty(®istry
));