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_all_threads(void)
85 static void force_mb_all_threads(void)
87 struct reader_data
*index
;
89 * Ask for each threads to execute a smp_mb() so we can consider the
90 * compiler barriers around rcu read lock as real memory barriers.
97 smp_mb(); /* write sig_done before sending the signals */
99 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
100 pthread_kill(index
->tid
, SIGURCU
);
104 * Wait for sighandler (and thus mb()) to execute on every thread.
107 while (sig_done
< num_readers
)
110 smp_mb(); /* read sig_done before ending the barrier */
115 void wait_for_quiescent_state(void)
117 struct reader_data
*index
;
121 /* Wait for each thread urcu_active_readers count to become 0.
123 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
127 while (rcu_old_gp_ongoing(index
->urcu_active_readers
))
132 void synchronize_rcu(void)
134 /* All threads should read qparity before accessing data structure
135 * where new ptr points to. */
136 /* Write new ptr before changing the qparity */
137 force_mb_all_threads();
140 internal_urcu_lock();
143 switch_next_urcu_qparity(); /* 0 -> 1 */
147 * Must commit qparity update to memory before waiting for parity
148 * 0 quiescent state. Failure to do so could result in the writer
149 * waiting forever while new readers are always accessing data (no
155 * Wait for previous parity to be empty of readers.
157 wait_for_quiescent_state(); /* Wait readers in parity 0 */
161 * Must finish waiting for quiescent state for parity 0 before
162 * committing qparity update to memory. Failure to do so could result in
163 * the writer waiting forever while new readers are always accessing
164 * data (no progress).
168 switch_next_urcu_qparity(); /* 1 -> 0 */
172 * Must commit qparity update to memory before waiting for parity
173 * 1 quiescent state. Failure to do so could result in the writer
174 * waiting forever while new readers are always accessing data (no
180 * Wait for previous parity to be empty of readers.
182 wait_for_quiescent_state(); /* Wait readers in parity 1 */
185 internal_urcu_unlock();
188 /* All threads should finish using the data referred to by old ptr
189 * before decrementing their urcu_active_readers count */
190 /* Finish waiting for reader threads before letting the old ptr being
192 force_mb_all_threads();
196 void urcu_add_reader(pthread_t id
)
198 struct reader_data
*oldarray
;
201 alloc_readers
= INIT_NUM_THREADS
;
204 malloc(sizeof(struct reader_data
) * alloc_readers
);
206 if (alloc_readers
< num_readers
+ 1) {
207 oldarray
= reader_data
;
208 reader_data
= malloc(sizeof(struct reader_data
)
209 * (alloc_readers
<< 1));
210 memcpy(reader_data
, oldarray
,
211 sizeof(struct reader_data
) * alloc_readers
);
215 reader_data
[num_readers
].tid
= id
;
216 /* reference to the TLS of _this_ reader thread. */
217 reader_data
[num_readers
].urcu_active_readers
= &urcu_active_readers
;
222 * Never shrink (implementation limitation).
223 * This is O(nb threads). Eventually use a hash table.
225 void urcu_remove_reader(pthread_t id
)
227 struct reader_data
*index
;
229 assert(reader_data
!= NULL
);
230 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
231 if (pthread_equal(index
->tid
, id
)) {
232 memcpy(index
, &reader_data
[num_readers
- 1],
233 sizeof(struct reader_data
));
234 reader_data
[num_readers
- 1].tid
= 0;
235 reader_data
[num_readers
- 1].urcu_active_readers
= NULL
;
240 /* Hrm not found, forgot to register ? */
244 void urcu_register_thread(void)
246 internal_urcu_lock();
247 urcu_add_reader(pthread_self());
248 internal_urcu_unlock();
251 void urcu_unregister_thread(void)
253 internal_urcu_lock();
254 urcu_remove_reader(pthread_self());
255 internal_urcu_unlock();
258 #ifndef DEBUG_FULL_MB
259 void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
262 atomic_inc(&sig_done
);
265 void __attribute__((constructor
)) urcu_init(void)
267 struct sigaction act
;
270 act
.sa_sigaction
= sigurcu_handler
;
271 ret
= sigaction(SIGURCU
, &act
, NULL
);
273 perror("Error in sigaction");
278 void __attribute__((destructor
)) urcu_exit(void)
280 struct sigaction act
;
283 ret
= sigaction(SIGURCU
, NULL
, &act
);
285 perror("Error in sigaction");
288 assert(act
.sa_sigaction
== sigurcu_handler
);