4 * Userspace RCU library
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * Distributed under GPLv2
20 pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
23 * Global grace period counter.
24 * Contains the current RCU_GP_CTR_BIT.
25 * Also has a RCU_GP_CTR_BIT of 1, to accelerate the reader fast path.
27 long urcu_gp_ctr
= RCU_GP_COUNT
;
29 long __thread urcu_active_readers
;
31 /* Thread IDs of registered readers */
32 #define INIT_NUM_THREADS 4
36 long *urcu_active_readers
;
40 unsigned int yield_active
;
41 unsigned int __thread rand_yield
;
44 static struct reader_data
*reader_data
;
45 static int num_readers
, alloc_readers
;
50 void internal_urcu_lock(void)
53 ret
= pthread_mutex_lock(&urcu_mutex
);
55 perror("Error in pthread mutex lock");
60 void internal_urcu_unlock(void)
64 ret
= pthread_mutex_unlock(&urcu_mutex
);
66 perror("Error in pthread mutex unlock");
72 * called with urcu_mutex held.
74 static void switch_next_urcu_qparity(void)
76 urcu_gp_ctr
^= RCU_GP_CTR_BIT
;
80 static void force_mb_single_thread(pthread_t tid
)
85 static void force_mb_all_threads(void)
91 static void force_mb_single_thread(pthread_t tid
)
95 smp_mb(); /* write sig_done before sending the signals */
96 pthread_kill(tid
, SIGURCU
);
98 * Wait for sighandler (and thus mb()) to execute on every thread.
101 while (LOAD_REMOTE(sig_done
) < 1)
103 smp_mb(); /* read sig_done before ending the barrier */
106 static void force_mb_all_threads(void)
108 struct reader_data
*index
;
110 * Ask for each threads to execute a smp_mb() so we can consider the
111 * compiler barriers around rcu read lock as real memory barriers.
117 * pthread_kill has a smp_mb(). But beware, we assume it performs
118 * a cache flush on architectures with non-coherent cache.
119 * smp_mb(); write sig_done before sending the signals
121 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++)
122 pthread_kill(index
->tid
, SIGURCU
);
124 * Wait for sighandler (and thus mb()) to execute on every thread.
127 while (LOAD_REMOTE(sig_done
) < num_readers
)
129 smp_mb(); /* read sig_done before ending the barrier */
133 void wait_for_quiescent_state(void)
135 struct reader_data
*index
;
140 * Wait for each thread urcu_active_readers count to become 0.
142 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
145 * BUSY-LOOP. Force the reader thread to commit its
146 * urcu_active_readers update to memory if we wait for too long.
148 while (rcu_old_gp_ongoing(index
->urcu_active_readers
)) {
149 if (wait_loops
++ == KICK_READER_LOOPS
) {
150 force_mb_single_thread(index
->tid
);
157 void synchronize_rcu(void)
159 internal_urcu_lock();
161 /* All threads should read qparity before accessing data structure
162 * where new ptr points to. Must be done within internal_urcu_lock
163 * because it iterates on reader threads.*/
164 /* Write new ptr before changing the qparity */
165 force_mb_all_threads();
167 switch_next_urcu_qparity(); /* 0 -> 1 */
170 * Must commit qparity update to memory before waiting for parity
171 * 0 quiescent state. Failure to do so could result in the writer
172 * waiting forever while new readers are always accessing data (no
178 * Wait for previous parity to be empty of readers.
180 wait_for_quiescent_state(); /* Wait readers in parity 0 */
183 * Must finish waiting for quiescent state for parity 0 before
184 * committing qparity update to memory. Failure to do so could result in
185 * the writer waiting forever while new readers are always accessing
186 * data (no progress).
190 switch_next_urcu_qparity(); /* 1 -> 0 */
193 * Must commit qparity update to memory before waiting for parity
194 * 1 quiescent state. Failure to do so could result in the writer
195 * waiting forever while new readers are always accessing data (no
201 * Wait for previous parity to be empty of readers.
203 wait_for_quiescent_state(); /* Wait readers in parity 1 */
205 /* Finish waiting for reader threads before letting the old ptr being
206 * freed. Must be done within internal_urcu_lock because it iterates on
208 force_mb_all_threads();
210 internal_urcu_unlock();
213 void urcu_add_reader(pthread_t id
)
215 struct reader_data
*oldarray
;
218 alloc_readers
= INIT_NUM_THREADS
;
221 malloc(sizeof(struct reader_data
) * alloc_readers
);
223 if (alloc_readers
< num_readers
+ 1) {
224 oldarray
= reader_data
;
225 reader_data
= malloc(sizeof(struct reader_data
)
226 * (alloc_readers
<< 1));
227 memcpy(reader_data
, oldarray
,
228 sizeof(struct reader_data
) * alloc_readers
);
232 reader_data
[num_readers
].tid
= id
;
233 /* reference to the TLS of _this_ reader thread. */
234 reader_data
[num_readers
].urcu_active_readers
= &urcu_active_readers
;
239 * Never shrink (implementation limitation).
240 * This is O(nb threads). Eventually use a hash table.
242 void urcu_remove_reader(pthread_t id
)
244 struct reader_data
*index
;
246 assert(reader_data
!= NULL
);
247 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
248 if (pthread_equal(index
->tid
, id
)) {
249 memcpy(index
, &reader_data
[num_readers
- 1],
250 sizeof(struct reader_data
));
251 reader_data
[num_readers
- 1].tid
= 0;
252 reader_data
[num_readers
- 1].urcu_active_readers
= NULL
;
257 /* Hrm not found, forgot to register ? */
261 void urcu_register_thread(void)
263 internal_urcu_lock();
264 urcu_add_reader(pthread_self());
265 internal_urcu_unlock();
268 void urcu_unregister_thread(void)
270 internal_urcu_lock();
271 urcu_remove_reader(pthread_self());
272 internal_urcu_unlock();
275 #ifndef DEBUG_FULL_MB
276 void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
279 * Executing this smp_mb() is the only purpose of this signal handler.
280 * It punctually promotes barrier() into smp_mb() on every thread it is
284 atomic_inc(&sig_done
);
287 void __attribute__((constructor
)) urcu_init(void)
289 struct sigaction act
;
292 act
.sa_sigaction
= sigurcu_handler
;
293 ret
= sigaction(SIGURCU
, &act
, NULL
);
295 perror("Error in sigaction");
300 void __attribute__((destructor
)) urcu_exit(void)
302 struct sigaction act
;
305 ret
= sigaction(SIGURCU
, NULL
, &act
);
307 perror("Error in sigaction");
310 assert(act
.sa_sigaction
== sigurcu_handler
);