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
;
48 void internal_urcu_lock(void)
51 ret
= pthread_mutex_lock(&urcu_mutex
);
53 perror("Error in pthread mutex lock");
58 void internal_urcu_unlock(void)
62 ret
= pthread_mutex_unlock(&urcu_mutex
);
64 perror("Error in pthread mutex unlock");
70 * called with urcu_mutex held.
72 static void switch_next_urcu_qparity(void)
74 urcu_gp_ctr
^= RCU_GP_CTR_BIT
;
77 static void force_mb_all_threads(void)
79 struct reader_data
*index
;
81 * Ask for each threads to execute a mb() so we can consider the
82 * compiler barriers around rcu read lock as real memory barriers.
89 mb(); /* write sig_done before sending the signals */
91 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
92 pthread_kill(index
->tid
, SIGURCU
);
96 * Wait for sighandler (and thus mb()) to execute on every thread.
99 while (sig_done
< num_readers
)
102 mb(); /* read sig_done before ending the barrier */
106 void wait_for_quiescent_state(void)
108 struct reader_data
*index
;
112 /* Wait for each thread urcu_active_readers count to become 0.
114 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
118 while (rcu_old_gp_ongoing(index
->urcu_active_readers
))
122 * Locally : read *index->urcu_active_readers before freeing old
124 * Remote (reader threads) : Order urcu_qparity update and other
125 * thread's quiescent state counter read.
127 force_mb_all_threads();
130 static void switch_qparity(void)
132 /* All threads should read qparity before accessing data structure. */
133 /* Write ptr before changing the qparity */
134 force_mb_all_threads();
136 switch_next_urcu_qparity();
140 * Wait for previous parity to be empty of readers.
142 wait_for_quiescent_state();
145 void synchronize_rcu(void)
148 internal_urcu_lock();
154 internal_urcu_unlock();
158 void urcu_add_reader(pthread_t id
)
160 struct reader_data
*oldarray
;
163 alloc_readers
= INIT_NUM_THREADS
;
166 malloc(sizeof(struct reader_data
) * alloc_readers
);
168 if (alloc_readers
< num_readers
+ 1) {
169 oldarray
= reader_data
;
170 reader_data
= malloc(sizeof(struct reader_data
)
171 * (alloc_readers
<< 1));
172 memcpy(reader_data
, oldarray
,
173 sizeof(struct reader_data
) * alloc_readers
);
177 reader_data
[num_readers
].tid
= id
;
178 /* reference to the TLS of _this_ reader thread. */
179 reader_data
[num_readers
].urcu_active_readers
= &urcu_active_readers
;
184 * Never shrink (implementation limitation).
185 * This is O(nb threads). Eventually use a hash table.
187 void urcu_remove_reader(pthread_t id
)
189 struct reader_data
*index
;
191 assert(reader_data
!= NULL
);
192 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
193 if (pthread_equal(index
->tid
, id
)) {
194 memcpy(index
, &reader_data
[num_readers
- 1],
195 sizeof(struct reader_data
));
196 reader_data
[num_readers
- 1].tid
= 0;
197 reader_data
[num_readers
- 1].urcu_active_readers
= NULL
;
202 /* Hrm not found, forgot to register ? */
206 void urcu_register_thread(void)
208 internal_urcu_lock();
209 urcu_add_reader(pthread_self());
210 internal_urcu_unlock();
213 void urcu_unregister_thread(void)
215 internal_urcu_lock();
216 urcu_remove_reader(pthread_self());
217 internal_urcu_unlock();
220 void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
223 atomic_inc(&sig_done
);
226 void __attribute__((constructor
)) urcu_init(void)
228 struct sigaction act
;
231 act
.sa_sigaction
= sigurcu_handler
;
232 ret
= sigaction(SIGURCU
, &act
, NULL
);
234 perror("Error in sigaction");
239 void __attribute__((destructor
)) urcu_exit(void)
241 struct sigaction act
;
244 ret
= sigaction(SIGURCU
, NULL
, &act
);
246 perror("Error in sigaction");
249 assert(act
.sa_sigaction
== sigurcu_handler
);