4 * Userspace RCU 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.h"
36 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
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
;
62 unsigned int yield_active
;
63 unsigned int __thread rand_yield
;
66 static struct reader_registry
*registry
;
67 static char __thread need_mb
;
68 static int num_readers
, alloc_readers
;
70 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");
94 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
97 void internal_urcu_unlock(void)
101 ret
= pthread_mutex_unlock(&urcu_mutex
);
103 perror("Error in pthread mutex unlock");
108 #ifdef HAS_INCOHERENT_CACHES
109 static void force_mb_single_thread(struct reader_registry
*index
)
113 #endif /* #ifdef HAS_INCOHERENT_CACHES */
115 static void force_mb_all_threads(void)
120 void wait_for_quiescent_state(void)
122 struct reader_registry
*index
;
127 * Wait for each thread rcu_reader_qs_gp count to become 0.
129 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
130 #ifndef HAS_INCOHERENT_CACHES
131 while (rcu_gp_ongoing(index
->rcu_reader_qs_gp
) &&
132 (*index
->rcu_reader_qs_gp
- urcu_gp_ctr
< 0))
134 #else /* #ifndef HAS_INCOHERENT_CACHES */
137 * BUSY-LOOP. Force the reader thread to commit its
138 * rcu_reader_qs_gp update to memory if we wait for too long.
140 while (rcu_gp_ongoing(index
->rcu_reader_qs_gp
) &&
141 (*index
->rcu_reader_qs_gp
- urcu_gp_ctr
< 0)) {
142 if (wait_loops
++ == KICK_READER_LOOPS
) {
143 force_mb_single_thread(index
);
149 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
153 void synchronize_rcu(void)
155 internal_urcu_lock();
156 force_mb_all_threads();
158 wait_for_quiescent_state();
159 force_mb_all_threads();
160 internal_urcu_unlock();
164 * library wrappers to be used by non-LGPL compatible source code.
167 void rcu_read_lock(void)
172 void rcu_read_unlock(void)
177 void *rcu_dereference(void *p
)
179 return _rcu_dereference(p
);
182 void *rcu_assign_pointer_sym(void **p
, void *v
)
185 return STORE_SHARED(p
, v
);
188 void *rcu_xchg_pointer_sym(void **p
, void *v
)
194 void *rcu_publish_content_sym(void **p
, void *v
)
198 oldptr
= _rcu_xchg_pointer(p
, v
);
203 static void rcu_add_reader(pthread_t id
)
205 struct reader_registry
*oldarray
;
208 alloc_readers
= INIT_NUM_THREADS
;
211 malloc(sizeof(struct reader_registry
) * alloc_readers
);
213 if (alloc_readers
< num_readers
+ 1) {
215 registry
= malloc(sizeof(struct reader_registry
)
216 * (alloc_readers
<< 1));
217 memcpy(registry
, oldarray
,
218 sizeof(struct reader_registry
) * alloc_readers
);
222 registry
[num_readers
].tid
= id
;
223 /* reference to the TLS of _this_ reader thread. */
224 registry
[num_readers
].rcu_reader_qs_gp
= &rcu_reader_qs_gp
;
225 registry
[num_readers
].need_mb
= &need_mb
;
230 * Never shrink (implementation limitation).
231 * This is O(nb threads). Eventually use a hash table.
233 static void rcu_remove_reader(pthread_t id
)
235 struct reader_registry
*index
;
237 assert(registry
!= NULL
);
238 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
239 if (pthread_equal(index
->tid
, id
)) {
240 memcpy(index
, ®istry
[num_readers
- 1],
241 sizeof(struct reader_registry
));
242 registry
[num_readers
- 1].tid
= 0;
243 registry
[num_readers
- 1].rcu_reader_qs_gp
= NULL
;
248 /* Hrm not found, forgot to register ? */
252 void rcu_register_thread(void)
254 _rcu_thread_online();
255 internal_urcu_lock();
256 rcu_add_reader(pthread_self());
257 internal_urcu_unlock();
260 void rcu_unregister_thread(void)
263 * We have to make the thread offline otherwise we end up dealocking
264 * with a waiting writer.
266 _rcu_thread_offline();
267 internal_urcu_lock();
268 rcu_remove_reader(pthread_self());
269 internal_urcu_unlock();