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 unsigned long __thread rcu_reader_qs_gp
;
55 /* Thread IDs of registered readers */
56 #define INIT_NUM_THREADS 4
58 struct reader_registry
{
60 unsigned long *rcu_reader_qs_gp
;
64 unsigned int yield_active
;
65 unsigned int __thread rand_yield
;
68 static struct reader_registry
*registry
;
69 static int num_readers
, alloc_readers
;
71 static void internal_urcu_lock(void)
75 #ifndef DISTRUST_SIGNALS_EXTREME
76 ret
= pthread_mutex_lock(&urcu_mutex
);
78 perror("Error in pthread mutex lock");
81 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
82 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
83 if (ret
!= EBUSY
&& ret
!= EINTR
) {
84 printf("ret = %d, errno = %d\n", ret
, errno
);
85 perror("Error in pthread mutex lock");
90 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
93 static void internal_urcu_unlock(void)
97 ret
= pthread_mutex_unlock(&urcu_mutex
);
99 perror("Error in pthread mutex unlock");
105 * synchronize_rcu() waiting. Single thread.
107 static void wait_gp(struct reader_registry
*index
)
109 atomic_dec(&gp_futex
);
110 smp_mb(); /* Write futex before read reader_gp */
111 if (!rcu_gp_ongoing(index
->rcu_reader_qs_gp
)) {
112 /* Read reader_gp before write futex */
114 /* Callbacks are queued, don't wait. */
115 atomic_set(&gp_futex
, 0);
117 /* Read reader_gp before read futex */
119 if (atomic_read(&gp_futex
) == -1)
120 futex(&gp_futex
, FUTEX_WAIT
, -1,
125 static void wait_for_quiescent_state(void)
127 struct reader_registry
*index
;
132 * Wait for each thread rcu_reader_qs_gp count to become 0.
134 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
137 while (rcu_gp_ongoing(index
->rcu_reader_qs_gp
)) {
138 if (wait_loops
++ == RCU_QS_ACTIVE_ATTEMPTS
) {
141 #ifndef HAS_INCOHERENT_CACHES
143 #else /* #ifndef HAS_INCOHERENT_CACHES */
145 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
152 * Using a two-subphases algorithm for architectures with smaller than 64-bit
153 * long-size to ensure we do not encounter an overflow bug.
156 #if (BITS_PER_LONG < 64)
158 * called with urcu_mutex held.
160 static void switch_next_urcu_qparity(void)
162 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR
);
165 void synchronize_rcu(void)
167 unsigned long was_online
;
169 was_online
= rcu_reader_qs_gp
;
171 /* All threads should read qparity before accessing data structure
172 * where new ptr points to.
174 /* Write new ptr before changing the qparity */
178 * Mark the writer thread offline to make sure we don't wait for
179 * our own quiescent state. This allows using synchronize_rcu() in
180 * threads registered as readers.
183 STORE_SHARED(rcu_reader_qs_gp
, 0);
185 internal_urcu_lock();
187 switch_next_urcu_qparity(); /* 0 -> 1 */
190 * Must commit qparity update to memory before waiting for parity
191 * 0 quiescent state. Failure to do so could result in the writer
192 * waiting forever while new readers are always accessing data (no
194 * Ensured by STORE_SHARED and LOAD_SHARED.
198 * Wait for previous parity to be empty of readers.
200 wait_for_quiescent_state(); /* Wait readers in parity 0 */
203 * Must finish waiting for quiescent state for parity 0 before
204 * committing qparity update to memory. Failure to do so could result in
205 * the writer waiting forever while new readers are always accessing
206 * data (no progress).
207 * Ensured by STORE_SHARED and LOAD_SHARED.
210 switch_next_urcu_qparity(); /* 1 -> 0 */
213 * Must commit qparity update to memory before waiting for parity
214 * 1 quiescent state. Failure to do so could result in the writer
215 * waiting forever while new readers are always accessing data (no
217 * Ensured by STORE_SHARED and LOAD_SHARED.
221 * Wait for previous parity to be empty of readers.
223 wait_for_quiescent_state(); /* Wait readers in parity 1 */
225 internal_urcu_unlock();
228 * Finish waiting for reader threads before letting the old ptr being
232 _STORE_SHARED(rcu_reader_qs_gp
, LOAD_SHARED(urcu_gp_ctr
));
235 #else /* !(BITS_PER_LONG < 64) */
236 void synchronize_rcu(void)
238 unsigned long was_online
;
240 was_online
= rcu_reader_qs_gp
;
243 * Mark the writer thread offline to make sure we don't wait for
244 * our own quiescent state. This allows using synchronize_rcu() in
245 * threads registered as readers.
249 STORE_SHARED(rcu_reader_qs_gp
, 0);
251 internal_urcu_lock();
252 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
+ RCU_GP_CTR
);
253 wait_for_quiescent_state();
254 internal_urcu_unlock();
257 _STORE_SHARED(rcu_reader_qs_gp
, LOAD_SHARED(urcu_gp_ctr
));
260 #endif /* !(BITS_PER_LONG < 64) */
263 * library wrappers to be used by non-LGPL compatible source code.
266 void rcu_read_lock(void)
271 void rcu_read_unlock(void)
276 void *rcu_dereference(void *p
)
278 return _rcu_dereference(p
);
281 void *rcu_assign_pointer_sym(void **p
, void *v
)
284 return STORE_SHARED(p
, v
);
287 void *rcu_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
290 return cmpxchg(p
, old
, _new
);
293 void *rcu_xchg_pointer_sym(void **p
, void *v
)
299 void *rcu_publish_content_sym(void **p
, void *v
)
303 oldptr
= _rcu_xchg_pointer(p
, v
);
308 void rcu_quiescent_state(void)
310 _rcu_quiescent_state();
313 void rcu_thread_offline(void)
315 _rcu_thread_offline();
318 void rcu_thread_online(void)
320 _rcu_thread_online();
323 static void rcu_add_reader(pthread_t id
)
325 struct reader_registry
*oldarray
;
328 alloc_readers
= INIT_NUM_THREADS
;
331 malloc(sizeof(struct reader_registry
) * alloc_readers
);
333 if (alloc_readers
< num_readers
+ 1) {
335 registry
= malloc(sizeof(struct reader_registry
)
336 * (alloc_readers
<< 1));
337 memcpy(registry
, oldarray
,
338 sizeof(struct reader_registry
) * alloc_readers
);
342 registry
[num_readers
].tid
= id
;
343 /* reference to the TLS of _this_ reader thread. */
344 registry
[num_readers
].rcu_reader_qs_gp
= &rcu_reader_qs_gp
;
349 * Never shrink (implementation limitation).
350 * This is O(nb threads). Eventually use a hash table.
352 static void rcu_remove_reader(pthread_t id
)
354 struct reader_registry
*index
;
356 assert(registry
!= NULL
);
357 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
358 if (pthread_equal(index
->tid
, id
)) {
359 memcpy(index
, ®istry
[num_readers
- 1],
360 sizeof(struct reader_registry
));
361 registry
[num_readers
- 1].tid
= 0;
362 registry
[num_readers
- 1].rcu_reader_qs_gp
= NULL
;
367 /* Hrm not found, forgot to register ? */
371 void rcu_register_thread(void)
373 internal_urcu_lock();
374 rcu_add_reader(pthread_self());
375 internal_urcu_unlock();
376 _rcu_thread_online();
379 void rcu_unregister_thread(void)
382 * We have to make the thread offline otherwise we end up dealocking
383 * with a waiting writer.
385 _rcu_thread_offline();
386 internal_urcu_lock();
387 rcu_remove_reader(pthread_self());
388 internal_urcu_unlock();