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.
36 #define BUILD_QSBR_LIB
37 #include "urcu-qsbr-static.h"
38 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
39 #include "urcu-qsbr.h"
41 static pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
44 * Global grace period counter.
46 unsigned long urcu_gp_ctr
= RCU_GP_ONLINE
;
49 * Written to only by each individual reader. Read by both the reader and the
52 struct urcu_reader_status __thread urcu_reader_status
;
54 /* Thread IDs of registered readers */
55 #define INIT_NUM_THREADS 4
57 struct reader_registry
{
59 struct urcu_reader_status
*urcu_reader_status
;
63 unsigned int yield_active
;
64 unsigned int __thread rand_yield
;
67 static struct reader_registry
*registry
;
68 static int num_readers
, alloc_readers
;
70 static void internal_urcu_lock(void)
74 #ifndef DISTRUST_SIGNALS_EXTREME
75 ret
= pthread_mutex_lock(&urcu_mutex
);
77 perror("Error in pthread mutex lock");
80 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
81 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
82 if (ret
!= EBUSY
&& ret
!= EINTR
) {
83 printf("ret = %d, errno = %d\n", ret
, errno
);
84 perror("Error in pthread mutex lock");
89 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
92 static void internal_urcu_unlock(void)
96 ret
= pthread_mutex_unlock(&urcu_mutex
);
98 perror("Error in pthread mutex unlock");
104 * synchronize_rcu() waiting. Single thread.
106 static void wait_for_quiescent_state(void)
108 struct reader_registry
*index
;
113 * Wait for each thread rcu_reader qs_gp count to become 0.
115 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
118 index
->urcu_reader_status
->gp_waiting
= 1;
119 while (rcu_gp_ongoing(&index
->urcu_reader_status
->qs_gp
)) {
120 if (wait_loops
++ == RCU_QS_ACTIVE_ATTEMPTS
) {
121 /* adapted wait time, in us */
122 usleep(LOAD_SHARED(index
->urcu_reader_status
->qs_time_delta_usec
) / 4);
125 #ifndef HAS_INCOHERENT_CACHES
127 #else /* #ifndef HAS_INCOHERENT_CACHES */
129 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
132 index
->urcu_reader_status
->gp_waiting
= 0;
137 * Using a two-subphases algorithm for architectures with smaller than 64-bit
138 * long-size to ensure we do not encounter an overflow bug.
141 #if (BITS_PER_LONG < 64)
143 * called with urcu_mutex held.
145 static void switch_next_urcu_qparity(void)
147 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR
);
150 void synchronize_rcu(void)
152 unsigned long was_online
;
154 was_online
= urcu_reader_status
.qs_gp
;
156 /* All threads should read qparity before accessing data structure
157 * where new ptr points to.
159 /* Write new ptr before changing the qparity */
163 * Mark the writer thread offline to make sure we don't wait for
164 * our own quiescent state. This allows using synchronize_rcu() in
165 * threads registered as readers.
168 STORE_SHARED(urcu_reader_status
.qs_gp
, 0);
170 internal_urcu_lock();
172 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
174 switch_next_urcu_qparity(); /* 0 -> 1 */
177 * Must commit qparity update to memory before waiting for parity
178 * 0 quiescent state. Failure to do so could result in the writer
179 * waiting forever while new readers are always accessing data (no
181 * Ensured by STORE_SHARED and LOAD_SHARED.
185 * Wait for previous parity to be empty of readers.
187 wait_for_quiescent_state(); /* Wait readers in parity 0 */
190 * Must finish waiting for quiescent state for parity 0 before
191 * committing qparity update to memory. Failure to do so could result in
192 * the writer waiting forever while new readers are always accessing
193 * data (no progress).
194 * Ensured by STORE_SHARED and LOAD_SHARED.
197 switch_next_urcu_qparity(); /* 1 -> 0 */
200 * Must commit qparity update to memory before waiting for parity
201 * 1 quiescent state. Failure to do so could result in the writer
202 * waiting forever while new readers are always accessing data (no
204 * Ensured by STORE_SHARED and LOAD_SHARED.
208 * Wait for previous parity to be empty of readers.
210 wait_for_quiescent_state(); /* Wait readers in parity 1 */
212 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
214 internal_urcu_unlock();
217 * Finish waiting for reader threads before letting the old ptr being
221 _STORE_SHARED(urcu_reader_status
.qs_gp
,
222 LOAD_SHARED(urcu_gp_ctr
));
225 #else /* !(BITS_PER_LONG < 64) */
226 void synchronize_rcu(void)
228 unsigned long was_online
;
230 was_online
= urcu_reader_status
.qs_gp
;
233 * Mark the writer thread offline to make sure we don't wait for
234 * our own quiescent state. This allows using synchronize_rcu() in
235 * threads registered as readers.
239 STORE_SHARED(urcu_reader_status
.qs_gp
, 0);
241 internal_urcu_lock();
242 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
243 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
+ RCU_GP_CTR
);
244 wait_for_quiescent_state();
245 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
246 internal_urcu_unlock();
249 _STORE_SHARED(urcu_reader_status
.qs_gp
,
250 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 cmpxchg(p
, old
, _new
);
286 void *rcu_xchg_pointer_sym(void **p
, void *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 static void rcu_add_reader(pthread_t id
)
318 struct reader_registry
*oldarray
;
321 alloc_readers
= INIT_NUM_THREADS
;
324 malloc(sizeof(struct reader_registry
) * alloc_readers
);
326 if (alloc_readers
< num_readers
+ 1) {
328 registry
= malloc(sizeof(struct reader_registry
)
329 * (alloc_readers
<< 1));
330 memcpy(registry
, oldarray
,
331 sizeof(struct reader_registry
) * alloc_readers
);
335 registry
[num_readers
].tid
= id
;
336 /* reference to the TLS of _this_ reader thread. */
337 registry
[num_readers
].urcu_reader_status
= &urcu_reader_status
;
342 * Never shrink (implementation limitation).
343 * This is O(nb threads). Eventually use a hash table.
345 static void rcu_remove_reader(pthread_t id
)
347 struct reader_registry
*index
;
349 assert(registry
!= NULL
);
350 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
351 if (pthread_equal(index
->tid
, id
)) {
352 memcpy(index
, ®istry
[num_readers
- 1],
353 sizeof(struct reader_registry
));
354 registry
[num_readers
- 1].tid
= 0;
355 registry
[num_readers
- 1].urcu_reader_status
= NULL
;
360 /* Hrm not found, forgot to register ? */
364 void rcu_register_thread(void)
366 internal_urcu_lock();
367 rcu_add_reader(pthread_self());
368 internal_urcu_unlock();
369 _rcu_thread_online();
372 void rcu_unregister_thread(void)
375 * We have to make the thread offline otherwise we end up dealocking
376 * with a waiting writer.
378 _rcu_thread_offline();
379 internal_urcu_lock();
380 rcu_remove_reader(pthread_self());
381 internal_urcu_unlock();