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 pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
43 * Global grace period counter.
48 * Written to only by each individual reader. Read by both the reader and the
51 long __thread rcu_reader_qs_gp
;
53 /* Thread IDs of registered readers */
54 #define INIT_NUM_THREADS 4
56 struct reader_registry
{
58 long *rcu_reader_qs_gp
;
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");
102 #ifdef HAS_INCOHERENT_CACHES
103 static void force_mb_single_thread(struct reader_registry
*index
)
107 #endif /* #ifdef HAS_INCOHERENT_CACHES */
109 static void force_mb_all_threads(void)
114 static void wait_for_quiescent_state(void)
116 struct reader_registry
*index
;
121 * Wait for each thread rcu_reader_qs_gp count to become 0.
123 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
124 #ifndef HAS_INCOHERENT_CACHES
125 while (rcu_gp_ongoing(index
->rcu_reader_qs_gp
))
127 #else /* #ifndef HAS_INCOHERENT_CACHES */
130 * BUSY-LOOP. Force the reader thread to commit its
131 * rcu_reader_qs_gp update to memory if we wait for too long.
133 while (rcu_gp_ongoing(index
->rcu_reader_qs_gp
)) {
134 if (wait_loops
++ == KICK_READER_LOOPS
) {
135 force_mb_single_thread(index
);
141 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
145 void synchronize_rcu(void)
149 was_online
= rcu_reader_qs_gp
& 1;
152 * Mark the writer thread offline to make sure we don't wait for
153 * our own quiescent state. This allows using synchronize_rcu() in
154 * threads registered as readers.
157 _rcu_thread_offline();
159 force_mb_all_threads();
160 internal_urcu_lock();
161 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
+ 2);
162 wait_for_quiescent_state();
163 internal_urcu_unlock();
164 force_mb_all_threads();
167 _rcu_thread_online();
171 * library wrappers to be used by non-LGPL compatible source code.
174 void rcu_read_lock(void)
179 void rcu_read_unlock(void)
184 void *rcu_dereference(void *p
)
186 return _rcu_dereference(p
);
189 void *rcu_assign_pointer_sym(void **p
, void *v
)
192 return STORE_SHARED(p
, v
);
195 void *rcu_xchg_pointer_sym(void **p
, void *v
)
201 void *rcu_publish_content_sym(void **p
, void *v
)
205 oldptr
= _rcu_xchg_pointer(p
, v
);
210 void rcu_quiescent_state(void)
212 _rcu_quiescent_state();
215 void rcu_thread_offline(void)
217 _rcu_thread_offline();
220 void rcu_thread_online(void)
222 _rcu_thread_online();
225 static void rcu_add_reader(pthread_t id
)
227 struct reader_registry
*oldarray
;
230 alloc_readers
= INIT_NUM_THREADS
;
233 malloc(sizeof(struct reader_registry
) * alloc_readers
);
235 if (alloc_readers
< num_readers
+ 1) {
237 registry
= malloc(sizeof(struct reader_registry
)
238 * (alloc_readers
<< 1));
239 memcpy(registry
, oldarray
,
240 sizeof(struct reader_registry
) * alloc_readers
);
244 registry
[num_readers
].tid
= id
;
245 /* reference to the TLS of _this_ reader thread. */
246 registry
[num_readers
].rcu_reader_qs_gp
= &rcu_reader_qs_gp
;
251 * Never shrink (implementation limitation).
252 * This is O(nb threads). Eventually use a hash table.
254 static void rcu_remove_reader(pthread_t id
)
256 struct reader_registry
*index
;
258 assert(registry
!= NULL
);
259 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
260 if (pthread_equal(index
->tid
, id
)) {
261 memcpy(index
, ®istry
[num_readers
- 1],
262 sizeof(struct reader_registry
));
263 registry
[num_readers
- 1].tid
= 0;
264 registry
[num_readers
- 1].rcu_reader_qs_gp
= NULL
;
269 /* Hrm not found, forgot to register ? */
273 void rcu_register_thread(void)
275 internal_urcu_lock();
276 rcu_add_reader(pthread_self());
277 internal_urcu_unlock();
278 _rcu_thread_online();
281 void rcu_unregister_thread(void)
284 * We have to make the thread offline otherwise we end up dealocking
285 * with a waiting writer.
287 _rcu_thread_offline();
288 internal_urcu_lock();
289 rcu_remove_reader(pthread_self());
290 internal_urcu_unlock();