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 #include "urcu-qsbr-static.h"
36 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
37 #include "urcu-qsbr.h"
39 pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
42 * Global grace period counter.
47 * Written to only by each individual reader. Read by both the reader and the
50 long __thread rcu_reader_qs_gp
;
52 /* Thread IDs of registered readers */
53 #define INIT_NUM_THREADS 4
55 struct reader_registry
{
57 long *rcu_reader_qs_gp
;
61 unsigned int yield_active
;
62 unsigned int __thread rand_yield
;
65 static struct reader_registry
*registry
;
66 static int num_readers
, alloc_readers
;
68 static void internal_urcu_lock(void)
72 #ifndef DISTRUST_SIGNALS_EXTREME
73 ret
= pthread_mutex_lock(&urcu_mutex
);
75 perror("Error in pthread mutex lock");
78 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
79 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
80 if (ret
!= EBUSY
&& ret
!= EINTR
) {
81 printf("ret = %d, errno = %d\n", ret
, errno
);
82 perror("Error in pthread mutex lock");
87 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
90 static void internal_urcu_unlock(void)
94 ret
= pthread_mutex_unlock(&urcu_mutex
);
96 perror("Error in pthread mutex unlock");
101 #ifdef HAS_INCOHERENT_CACHES
102 static void force_mb_single_thread(struct reader_registry
*index
)
106 #endif /* #ifdef HAS_INCOHERENT_CACHES */
108 static void force_mb_all_threads(void)
113 static void wait_for_quiescent_state(void)
115 struct reader_registry
*index
;
117 if (rcu_reader_qs_gp
& 1)
118 rcu_reader_qs_gp
= urcu_gp_ctr
+ 1;
123 * Wait for each thread rcu_reader_qs_gp count to become 0.
125 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
126 #ifndef HAS_INCOHERENT_CACHES
127 while (rcu_gp_ongoing(index
->rcu_reader_qs_gp
) &&
128 (*index
->rcu_reader_qs_gp
- urcu_gp_ctr
< 0))
130 #else /* #ifndef HAS_INCOHERENT_CACHES */
133 * BUSY-LOOP. Force the reader thread to commit its
134 * rcu_reader_qs_gp update to memory if we wait for too long.
136 while (rcu_gp_ongoing(index
->rcu_reader_qs_gp
) &&
137 (*index
->rcu_reader_qs_gp
- urcu_gp_ctr
< 0)) {
138 if (wait_loops
++ == KICK_READER_LOOPS
) {
139 force_mb_single_thread(index
);
145 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
149 void synchronize_rcu(void)
153 was_online
= rcu_reader_qs_gp
& 1;
156 * Mark the writer thread offline to make sure we don't wait for
157 * our own quiescent state. This allows using synchronize_rcu() in
158 * threads registered as readers.
161 _rcu_thread_offline();
163 internal_urcu_lock();
164 force_mb_all_threads();
166 wait_for_quiescent_state();
167 force_mb_all_threads();
168 internal_urcu_unlock();
171 _rcu_thread_online();
175 * library wrappers to be used by non-LGPL compatible source code.
178 void rcu_read_lock(void)
183 void rcu_read_unlock(void)
188 void *rcu_dereference(void *p
)
190 return _rcu_dereference(p
);
193 void *rcu_assign_pointer_sym(void **p
, void *v
)
196 return STORE_SHARED(p
, v
);
199 void *rcu_xchg_pointer_sym(void **p
, void *v
)
205 void *rcu_publish_content_sym(void **p
, void *v
)
209 oldptr
= _rcu_xchg_pointer(p
, v
);
214 void rcu_quiescent_state(void)
216 _rcu_quiescent_state();
219 void rcu_thread_offline(void)
221 _rcu_thread_offline();
224 void rcu_thread_online(void)
226 _rcu_thread_online();
229 static void rcu_add_reader(pthread_t id
)
231 struct reader_registry
*oldarray
;
234 alloc_readers
= INIT_NUM_THREADS
;
237 malloc(sizeof(struct reader_registry
) * alloc_readers
);
239 if (alloc_readers
< num_readers
+ 1) {
241 registry
= malloc(sizeof(struct reader_registry
)
242 * (alloc_readers
<< 1));
243 memcpy(registry
, oldarray
,
244 sizeof(struct reader_registry
) * alloc_readers
);
248 registry
[num_readers
].tid
= id
;
249 /* reference to the TLS of _this_ reader thread. */
250 registry
[num_readers
].rcu_reader_qs_gp
= &rcu_reader_qs_gp
;
255 * Never shrink (implementation limitation).
256 * This is O(nb threads). Eventually use a hash table.
258 static void rcu_remove_reader(pthread_t id
)
260 struct reader_registry
*index
;
262 assert(registry
!= NULL
);
263 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
264 if (pthread_equal(index
->tid
, id
)) {
265 memcpy(index
, ®istry
[num_readers
- 1],
266 sizeof(struct reader_registry
));
267 registry
[num_readers
- 1].tid
= 0;
268 registry
[num_readers
- 1].rcu_reader_qs_gp
= NULL
;
273 /* Hrm not found, forgot to register ? */
277 void rcu_register_thread(void)
279 internal_urcu_lock();
280 rcu_add_reader(pthread_self());
281 internal_urcu_unlock();
282 _rcu_thread_online();
285 void rcu_unregister_thread(void)
288 * We have to make the thread offline otherwise we end up dealocking
289 * with a waiting writer.
291 _rcu_thread_offline();
292 internal_urcu_lock();
293 rcu_remove_reader(pthread_self());
294 internal_urcu_unlock();