Support kernels with broken signal delivery
[urcu.git] / urcu.c
1 /*
2 * urcu.c
3 *
4 * Userspace RCU library
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * Distributed under GPLv2
9 */
10
11 #include <stdio.h>
12 #include <pthread.h>
13 #include <signal.h>
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18
19 #include "urcu.h"
20
21 pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
22
23 /*
24 * Global grace period counter.
25 * Contains the current RCU_GP_CTR_BIT.
26 * Also has a RCU_GP_CTR_BIT of 1, to accelerate the reader fast path.
27 * Written to only by writer with mutex taken. Read by both writer and readers.
28 */
29 long urcu_gp_ctr = RCU_GP_COUNT;
30
31 /*
32 * Written to only by each individual reader. Read by both the reader and the
33 * writers.
34 */
35 long __thread urcu_active_readers;
36
37 /* Thread IDs of registered readers */
38 #define INIT_NUM_THREADS 4
39
40 struct reader_registry {
41 pthread_t tid;
42 long *urcu_active_readers;
43 char *need_mb;
44 };
45
46 #ifdef DEBUG_YIELD
47 unsigned int yield_active;
48 unsigned int __thread rand_yield;
49 #endif
50
51 static struct reader_registry *registry;
52 static char __thread need_mb;
53 static int num_readers, alloc_readers;
54
55 void internal_urcu_lock(void)
56 {
57 int ret;
58
59 #ifndef DISTRUST_SIGNALS_EXTREME
60 ret = pthread_mutex_lock(&urcu_mutex);
61 if (ret) {
62 perror("Error in pthread mutex lock");
63 exit(-1);
64 }
65 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
66 while ((ret = pthread_mutex_trylock(&urcu_mutex)) != 0) {
67 if (ret != EBUSY && ret != EINTR) {
68 printf("ret = %d, errno = %d\n", ret, errno);
69 perror("Error in pthread mutex lock");
70 exit(-1);
71 }
72 if (need_mb) {
73 smp_mb();
74 need_mb = 0;
75 smp_mb();
76 }
77 poll(NULL,0,10);
78 }
79 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
80 }
81
82 void internal_urcu_unlock(void)
83 {
84 int ret;
85
86 ret = pthread_mutex_unlock(&urcu_mutex);
87 if (ret) {
88 perror("Error in pthread mutex unlock");
89 exit(-1);
90 }
91 }
92
93 /*
94 * called with urcu_mutex held.
95 */
96 static void switch_next_urcu_qparity(void)
97 {
98 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR_BIT);
99 }
100
101 #ifdef DEBUG_FULL_MB
102 static void force_mb_single_thread(struct reader_registry *index)
103 {
104 smp_mb();
105 }
106
107 static void force_mb_all_threads(void)
108 {
109 smp_mb();
110 }
111 #else
112
113 static void force_mb_single_thread(struct reader_registry *index)
114 {
115 assert(registry);
116 /*
117 * pthread_kill has a smp_mb(). But beware, we assume it performs
118 * a cache flush on architectures with non-coherent cache. Let's play
119 * safe and don't assume anything : we use smp_mc() to make sure the
120 * cache flush is enforced.
121 */
122 *index->need_mb = 1;
123 smp_mc(); /* write ->need_mb before sending the signals */
124 pthread_kill(index->tid, SIGURCU);
125 smp_mb();
126 /*
127 * Wait for sighandler (and thus mb()) to execute on every thread.
128 * BUSY-LOOP.
129 */
130 while (*index->need_mb) {
131 poll(NULL, 0, 1);
132 }
133 smp_mb(); /* read ->need_mb before ending the barrier */
134 }
135
136 static void force_mb_all_threads(void)
137 {
138 struct reader_registry *index;
139 /*
140 * Ask for each threads to execute a smp_mb() so we can consider the
141 * compiler barriers around rcu read lock as real memory barriers.
142 */
143 if (!registry)
144 return;
145 /*
146 * pthread_kill has a smp_mb(). But beware, we assume it performs
147 * a cache flush on architectures with non-coherent cache. Let's play
148 * safe and don't assume anything : we use smp_mc() to make sure the
149 * cache flush is enforced.
150 */
151 for (index = registry; index < registry + num_readers; index++) {
152 *index->need_mb = 1;
153 smp_mc(); /* write need_mb before sending the signal */
154 pthread_kill(index->tid, SIGURCU);
155 }
156 /*
157 * Wait for sighandler (and thus mb()) to execute on every thread.
158 *
159 * Note that the pthread_kill() will never be executed on systems
160 * that correctly deliver signals in a timely manner. However, it
161 * is not uncommon for kernels to have bugs that can result in
162 * lost or unduly delayed signals.
163 *
164 * If you are seeing the below pthread_kill() executing much at
165 * all, we suggest testing the underlying kernel and filing the
166 * relevant bug report. For Linux kernels, we recommend getting
167 * the Linux Test Project (LTP).
168 */
169 for (index = registry; index < registry + num_readers; index++) {
170 while (*index->need_mb) {
171 pthread_kill(index->tid, SIGURCU);
172 poll(NULL, 0, 1);
173 }
174 }
175 smp_mb(); /* read ->need_mb before ending the barrier */
176 }
177 #endif
178
179 void wait_for_quiescent_state(void)
180 {
181 struct reader_registry *index;
182
183 if (!registry)
184 return;
185 /*
186 * Wait for each thread urcu_active_readers count to become 0.
187 */
188 for (index = registry; index < registry + num_readers; index++) {
189 int wait_loops = 0;
190 /*
191 * BUSY-LOOP. Force the reader thread to commit its
192 * urcu_active_readers update to memory if we wait for too long.
193 */
194 while (rcu_old_gp_ongoing(index->urcu_active_readers)) {
195 if (wait_loops++ == KICK_READER_LOOPS) {
196 force_mb_single_thread(index);
197 wait_loops = 0;
198 } else {
199 cpu_relax();
200 }
201 }
202 }
203 }
204
205 void synchronize_rcu(void)
206 {
207 internal_urcu_lock();
208
209 /* All threads should read qparity before accessing data structure
210 * where new ptr points to. Must be done within internal_urcu_lock
211 * because it iterates on reader threads.*/
212 /* Write new ptr before changing the qparity */
213 force_mb_all_threads();
214
215 switch_next_urcu_qparity(); /* 0 -> 1 */
216
217 /*
218 * Must commit qparity update to memory before waiting for parity
219 * 0 quiescent state. Failure to do so could result in the writer
220 * waiting forever while new readers are always accessing data (no
221 * progress).
222 * Ensured by STORE_SHARED and LOAD_SHARED.
223 */
224
225 /*
226 * Wait for previous parity to be empty of readers.
227 */
228 wait_for_quiescent_state(); /* Wait readers in parity 0 */
229
230 /*
231 * Must finish waiting for quiescent state for parity 0 before
232 * committing qparity update to memory. Failure to do so could result in
233 * the writer waiting forever while new readers are always accessing
234 * data (no progress).
235 * Ensured by STORE_SHARED and LOAD_SHARED.
236 */
237
238 switch_next_urcu_qparity(); /* 1 -> 0 */
239
240 /*
241 * Must commit qparity update to memory before waiting for parity
242 * 1 quiescent state. Failure to do so could result in the writer
243 * waiting forever while new readers are always accessing data (no
244 * progress).
245 * Ensured by STORE_SHARED and LOAD_SHARED.
246 */
247
248 /*
249 * Wait for previous parity to be empty of readers.
250 */
251 wait_for_quiescent_state(); /* Wait readers in parity 1 */
252
253 /* Finish waiting for reader threads before letting the old ptr being
254 * freed. Must be done within internal_urcu_lock because it iterates on
255 * reader threads. */
256 force_mb_all_threads();
257
258 internal_urcu_unlock();
259 }
260
261 void urcu_add_reader(pthread_t id)
262 {
263 struct reader_registry *oldarray;
264
265 if (!registry) {
266 alloc_readers = INIT_NUM_THREADS;
267 num_readers = 0;
268 registry =
269 malloc(sizeof(struct reader_registry) * alloc_readers);
270 }
271 if (alloc_readers < num_readers + 1) {
272 oldarray = registry;
273 registry = malloc(sizeof(struct reader_registry)
274 * (alloc_readers << 1));
275 memcpy(registry, oldarray,
276 sizeof(struct reader_registry) * alloc_readers);
277 alloc_readers <<= 1;
278 free(oldarray);
279 }
280 registry[num_readers].tid = id;
281 /* reference to the TLS of _this_ reader thread. */
282 registry[num_readers].urcu_active_readers = &urcu_active_readers;
283 registry[num_readers].need_mb = &need_mb;
284 num_readers++;
285 }
286
287 /*
288 * Never shrink (implementation limitation).
289 * This is O(nb threads). Eventually use a hash table.
290 */
291 void urcu_remove_reader(pthread_t id)
292 {
293 struct reader_registry *index;
294
295 assert(registry != NULL);
296 for (index = registry; index < registry + num_readers; index++) {
297 if (pthread_equal(index->tid, id)) {
298 memcpy(index, &registry[num_readers - 1],
299 sizeof(struct reader_registry));
300 registry[num_readers - 1].tid = 0;
301 registry[num_readers - 1].urcu_active_readers = NULL;
302 num_readers--;
303 return;
304 }
305 }
306 /* Hrm not found, forgot to register ? */
307 assert(0);
308 }
309
310 void urcu_register_thread(void)
311 {
312 internal_urcu_lock();
313 urcu_add_reader(pthread_self());
314 internal_urcu_unlock();
315 }
316
317 void urcu_unregister_thread(void)
318 {
319 internal_urcu_lock();
320 urcu_remove_reader(pthread_self());
321 internal_urcu_unlock();
322 }
323
324 #ifndef DEBUG_FULL_MB
325 void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
326 {
327 /*
328 * Executing this smp_mb() is the only purpose of this signal handler.
329 * It punctually promotes barrier() into smp_mb() on every thread it is
330 * executed on.
331 */
332 smp_mb();
333 need_mb = 0;
334 smp_mb();
335 }
336
337 void __attribute__((constructor)) urcu_init(void)
338 {
339 struct sigaction act;
340 int ret;
341
342 act.sa_sigaction = sigurcu_handler;
343 ret = sigaction(SIGURCU, &act, NULL);
344 if (ret) {
345 perror("Error in sigaction");
346 exit(-1);
347 }
348 }
349
350 void __attribute__((destructor)) urcu_exit(void)
351 {
352 struct sigaction act;
353 int ret;
354
355 ret = sigaction(SIGURCU, NULL, &act);
356 if (ret) {
357 perror("Error in sigaction");
358 exit(-1);
359 }
360 assert(act.sa_sigaction == sigurcu_handler);
361 free(registry);
362 }
363 #endif
This page took 0.035847 seconds and 5 git commands to generate.