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
;
43 * Global grace period counter.
45 unsigned long urcu_gp_ctr
= RCU_GP_ONLINE
;
48 * Written to only by each individual reader. Read by both the reader and the
51 struct urcu_reader_status __thread urcu_reader_status
;
53 /* Thread IDs of registered readers */
54 #define INIT_NUM_THREADS 4
56 struct reader_registry
{
58 struct urcu_reader_status
*urcu_reader_status
;
62 unsigned int yield_active
;
63 unsigned int __thread rand_yield
;
66 static struct reader_registry
*registry
;
67 static int num_readers
, alloc_readers
;
69 static void internal_urcu_lock(void)
73 #ifndef DISTRUST_SIGNALS_EXTREME
74 ret
= pthread_mutex_lock(&urcu_mutex
);
76 perror("Error in pthread mutex lock");
79 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
80 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
81 if (ret
!= EBUSY
&& ret
!= EINTR
) {
82 printf("ret = %d, errno = %d\n", ret
, errno
);
83 perror("Error in pthread mutex lock");
88 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
91 static void internal_urcu_unlock(void)
95 ret
= pthread_mutex_unlock(&urcu_mutex
);
97 perror("Error in pthread mutex unlock");
103 * synchronize_rcu() waiting. Single thread.
105 static void wait_for_quiescent_state(void)
107 struct reader_registry
*index
;
112 * Wait for each thread rcu_reader qs_gp count to become 0.
114 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
117 if (likely(!rcu_gp_ongoing(&index
->urcu_reader_status
->qs_gp
)))
120 index
->urcu_reader_status
->gp_waiting
= 1;
121 while (rcu_gp_ongoing(&index
->urcu_reader_status
->qs_gp
)) {
122 if (wait_loops
++ == RCU_QS_ACTIVE_ATTEMPTS
) {
123 sched_yield(); /* ideally sched_yield_to() */
126 #ifndef HAS_INCOHERENT_CACHES
128 #else /* #ifndef HAS_INCOHERENT_CACHES */
130 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
133 index
->urcu_reader_status
->gp_waiting
= 0;
138 * Using a two-subphases algorithm for architectures with smaller than 64-bit
139 * long-size to ensure we do not encounter an overflow bug.
142 #if (BITS_PER_LONG < 64)
144 * called with urcu_mutex held.
146 static void switch_next_urcu_qparity(void)
148 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR
);
151 void synchronize_rcu(void)
153 unsigned long was_online
;
155 was_online
= urcu_reader_status
.qs_gp
;
157 /* All threads should read qparity before accessing data structure
158 * where new ptr points to.
160 /* Write new ptr before changing the qparity */
164 * Mark the writer thread offline to make sure we don't wait for
165 * our own quiescent state. This allows using synchronize_rcu() in
166 * threads registered as readers.
169 STORE_SHARED(urcu_reader_status
.qs_gp
, 0);
171 internal_urcu_lock();
173 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
175 switch_next_urcu_qparity(); /* 0 -> 1 */
178 * Must commit qparity update to memory before waiting for parity
179 * 0 quiescent state. Failure to do so could result in the writer
180 * waiting forever while new readers are always accessing data (no
182 * Ensured by STORE_SHARED and LOAD_SHARED.
186 * Wait for previous parity to be empty of readers.
188 wait_for_quiescent_state(); /* Wait readers in parity 0 */
191 * Must finish waiting for quiescent state for parity 0 before
192 * committing qparity update to memory. Failure to do so could result in
193 * the writer waiting forever while new readers are always accessing
194 * data (no progress).
195 * Ensured by STORE_SHARED and LOAD_SHARED.
198 switch_next_urcu_qparity(); /* 1 -> 0 */
201 * Must commit qparity update to memory before waiting for parity
202 * 1 quiescent state. Failure to do so could result in the writer
203 * waiting forever while new readers are always accessing data (no
205 * Ensured by STORE_SHARED and LOAD_SHARED.
209 * Wait for previous parity to be empty of readers.
211 wait_for_quiescent_state(); /* Wait readers in parity 1 */
213 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
215 internal_urcu_unlock();
218 * Finish waiting for reader threads before letting the old ptr being
222 _STORE_SHARED(urcu_reader_status
.qs_gp
,
223 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_status
.qs_gp
;
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_status
.qs_gp
, 0);
242 internal_urcu_lock();
243 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
244 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
+ RCU_GP_CTR
);
245 wait_for_quiescent_state();
246 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
247 internal_urcu_unlock();
250 _STORE_SHARED(urcu_reader_status
.qs_gp
,
251 LOAD_SHARED(urcu_gp_ctr
));
254 #endif /* !(BITS_PER_LONG < 64) */
257 * library wrappers to be used by non-LGPL compatible source code.
260 void rcu_read_lock(void)
265 void rcu_read_unlock(void)
270 void *rcu_dereference(void *p
)
272 return _rcu_dereference(p
);
275 void *rcu_assign_pointer_sym(void **p
, void *v
)
278 return STORE_SHARED(p
, v
);
281 void *rcu_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
284 return cmpxchg(p
, old
, _new
);
287 void *rcu_xchg_pointer_sym(void **p
, void *v
)
293 void *rcu_publish_content_sym(void **p
, void *v
)
297 oldptr
= _rcu_xchg_pointer(p
, v
);
302 void rcu_quiescent_state(void)
304 _rcu_quiescent_state();
307 void rcu_thread_offline(void)
309 _rcu_thread_offline();
312 void rcu_thread_online(void)
314 _rcu_thread_online();
317 static void rcu_add_reader(pthread_t id
)
319 struct reader_registry
*oldarray
;
322 alloc_readers
= INIT_NUM_THREADS
;
325 malloc(sizeof(struct reader_registry
) * alloc_readers
);
327 if (alloc_readers
< num_readers
+ 1) {
329 registry
= malloc(sizeof(struct reader_registry
)
330 * (alloc_readers
<< 1));
331 memcpy(registry
, oldarray
,
332 sizeof(struct reader_registry
) * alloc_readers
);
336 registry
[num_readers
].tid
= id
;
337 /* reference to the TLS of _this_ reader thread. */
338 registry
[num_readers
].urcu_reader_status
= &urcu_reader_status
;
343 * Never shrink (implementation limitation).
344 * This is O(nb threads). Eventually use a hash table.
346 static void rcu_remove_reader(pthread_t id
)
348 struct reader_registry
*index
;
350 assert(registry
!= NULL
);
351 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
352 if (pthread_equal(index
->tid
, id
)) {
353 memcpy(index
, ®istry
[num_readers
- 1],
354 sizeof(struct reader_registry
));
355 registry
[num_readers
- 1].tid
= 0;
356 registry
[num_readers
- 1].urcu_reader_status
= NULL
;
361 /* Hrm not found, forgot to register ? */
365 void rcu_register_thread(void)
367 internal_urcu_lock();
368 rcu_add_reader(pthread_self());
369 internal_urcu_unlock();
370 _rcu_thread_online();
373 void rcu_unregister_thread(void)
376 * We have to make the thread offline otherwise we end up dealocking
377 * with a waiting writer.
379 _rcu_thread_offline();
380 internal_urcu_lock();
381 rcu_remove_reader(pthread_self());
382 internal_urcu_unlock();