Add smp_mc() to force_mb_single_thread so we don't assume anything from the OS.
[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
18 #include "urcu.h"
19
20 pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
21
22 /*
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.
26 */
27 long urcu_gp_ctr = RCU_GP_COUNT;
28
29 long __thread urcu_active_readers;
30
31 /* Thread IDs of registered readers */
32 #define INIT_NUM_THREADS 4
33
34 struct reader_data {
35 pthread_t tid;
36 long *urcu_active_readers;
37 };
38
39 #ifdef DEBUG_YIELD
40 unsigned int yield_active;
41 unsigned int __thread rand_yield;
42 #endif
43
44 static struct reader_data *reader_data;
45 static int num_readers, alloc_readers;
46 #ifndef DEBUG_FULL_MB
47 static int sig_done;
48 #endif
49
50 void internal_urcu_lock(void)
51 {
52 int ret;
53 ret = pthread_mutex_lock(&urcu_mutex);
54 if (ret) {
55 perror("Error in pthread mutex lock");
56 exit(-1);
57 }
58 }
59
60 void internal_urcu_unlock(void)
61 {
62 int ret;
63
64 ret = pthread_mutex_unlock(&urcu_mutex);
65 if (ret) {
66 perror("Error in pthread mutex unlock");
67 exit(-1);
68 }
69 }
70
71 /*
72 * called with urcu_mutex held.
73 */
74 static void switch_next_urcu_qparity(void)
75 {
76 urcu_gp_ctr ^= RCU_GP_CTR_BIT;
77 }
78
79 #ifdef DEBUG_FULL_MB
80 static void force_mb_single_thread(pthread_t tid)
81 {
82 smp_mb();
83 }
84
85 static void force_mb_all_threads(void)
86 {
87 smp_mb();
88 }
89 #else
90
91 static void force_mb_single_thread(pthread_t tid)
92 {
93 assert(reader_data);
94 sig_done = 0;
95 /*
96 * pthread_kill has a smp_mb(). But beware, we assume it performs
97 * a cache flush on architectures with non-coherent cache. Let's play
98 * safe and don't assume anything : we use smp_mc() to make sure the
99 * cache flush is enforced.
100 * smp_mb(); write sig_done before sending the signals
101 */
102 smp_mc(); /* write sig_done before sending the signals */
103 pthread_kill(tid, SIGURCU);
104 /*
105 * Wait for sighandler (and thus mb()) to execute on every thread.
106 * BUSY-LOOP.
107 */
108 while (LOAD_REMOTE(sig_done) < 1)
109 cpu_relax();
110 smp_mb(); /* read sig_done before ending the barrier */
111 }
112
113 static void force_mb_all_threads(void)
114 {
115 struct reader_data *index;
116 /*
117 * Ask for each threads to execute a smp_mb() so we can consider the
118 * compiler barriers around rcu read lock as real memory barriers.
119 */
120 if (!reader_data)
121 return;
122 sig_done = 0;
123 /*
124 * pthread_kill has a smp_mb(). But beware, we assume it performs
125 * a cache flush on architectures with non-coherent cache. Let's play
126 * safe and don't assume anything : we use smp_mc() to make sure the
127 * cache flush is enforced.
128 * smp_mb(); write sig_done before sending the signals
129 */
130 smp_mc(); /* write sig_done before sending the signals */
131 for (index = reader_data; index < reader_data + num_readers; index++)
132 pthread_kill(index->tid, SIGURCU);
133 /*
134 * Wait for sighandler (and thus mb()) to execute on every thread.
135 * BUSY-LOOP.
136 */
137 while (LOAD_REMOTE(sig_done) < num_readers)
138 cpu_relax();
139 smp_mb(); /* read sig_done before ending the barrier */
140 }
141 #endif
142
143 void wait_for_quiescent_state(void)
144 {
145 struct reader_data *index;
146
147 if (!reader_data)
148 return;
149 /*
150 * Wait for each thread urcu_active_readers count to become 0.
151 */
152 for (index = reader_data; index < reader_data + num_readers; index++) {
153 int wait_loops = 0;
154 /*
155 * BUSY-LOOP. Force the reader thread to commit its
156 * urcu_active_readers update to memory if we wait for too long.
157 */
158 while (rcu_old_gp_ongoing(index->urcu_active_readers)) {
159 if (wait_loops++ == KICK_READER_LOOPS) {
160 force_mb_single_thread(index->tid);
161 wait_loops = 0;
162 }
163 }
164 }
165 }
166
167 void synchronize_rcu(void)
168 {
169 internal_urcu_lock();
170
171 /* All threads should read qparity before accessing data structure
172 * where new ptr points to. Must be done within internal_urcu_lock
173 * because it iterates on reader threads.*/
174 /* Write new ptr before changing the qparity */
175 force_mb_all_threads();
176
177 switch_next_urcu_qparity(); /* 0 -> 1 */
178
179 /*
180 * Must commit qparity update to memory before waiting for parity
181 * 0 quiescent state. Failure to do so could result in the writer
182 * waiting forever while new readers are always accessing data (no
183 * progress).
184 */
185 smp_mc();
186
187 /*
188 * Wait for previous parity to be empty of readers.
189 */
190 wait_for_quiescent_state(); /* Wait readers in parity 0 */
191
192 /*
193 * Must finish waiting for quiescent state for parity 0 before
194 * committing qparity update to memory. Failure to do so could result in
195 * the writer waiting forever while new readers are always accessing
196 * data (no progress).
197 */
198 smp_mc();
199
200 switch_next_urcu_qparity(); /* 1 -> 0 */
201
202 /*
203 * Must commit qparity update to memory before waiting for parity
204 * 1 quiescent state. Failure to do so could result in the writer
205 * waiting forever while new readers are always accessing data (no
206 * progress).
207 */
208 smp_mc();
209
210 /*
211 * Wait for previous parity to be empty of readers.
212 */
213 wait_for_quiescent_state(); /* Wait readers in parity 1 */
214
215 /* Finish waiting for reader threads before letting the old ptr being
216 * freed. Must be done within internal_urcu_lock because it iterates on
217 * reader threads. */
218 force_mb_all_threads();
219
220 internal_urcu_unlock();
221 }
222
223 void urcu_add_reader(pthread_t id)
224 {
225 struct reader_data *oldarray;
226
227 if (!reader_data) {
228 alloc_readers = INIT_NUM_THREADS;
229 num_readers = 0;
230 reader_data =
231 malloc(sizeof(struct reader_data) * alloc_readers);
232 }
233 if (alloc_readers < num_readers + 1) {
234 oldarray = reader_data;
235 reader_data = malloc(sizeof(struct reader_data)
236 * (alloc_readers << 1));
237 memcpy(reader_data, oldarray,
238 sizeof(struct reader_data) * alloc_readers);
239 alloc_readers <<= 1;
240 free(oldarray);
241 }
242 reader_data[num_readers].tid = id;
243 /* reference to the TLS of _this_ reader thread. */
244 reader_data[num_readers].urcu_active_readers = &urcu_active_readers;
245 num_readers++;
246 }
247
248 /*
249 * Never shrink (implementation limitation).
250 * This is O(nb threads). Eventually use a hash table.
251 */
252 void urcu_remove_reader(pthread_t id)
253 {
254 struct reader_data *index;
255
256 assert(reader_data != NULL);
257 for (index = reader_data; index < reader_data + num_readers; index++) {
258 if (pthread_equal(index->tid, id)) {
259 memcpy(index, &reader_data[num_readers - 1],
260 sizeof(struct reader_data));
261 reader_data[num_readers - 1].tid = 0;
262 reader_data[num_readers - 1].urcu_active_readers = NULL;
263 num_readers--;
264 return;
265 }
266 }
267 /* Hrm not found, forgot to register ? */
268 assert(0);
269 }
270
271 void urcu_register_thread(void)
272 {
273 internal_urcu_lock();
274 urcu_add_reader(pthread_self());
275 internal_urcu_unlock();
276 }
277
278 void urcu_unregister_thread(void)
279 {
280 internal_urcu_lock();
281 urcu_remove_reader(pthread_self());
282 internal_urcu_unlock();
283 }
284
285 #ifndef DEBUG_FULL_MB
286 void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
287 {
288 /*
289 * Executing this smp_mb() is the only purpose of this signal handler.
290 * It punctually promotes barrier() into smp_mb() on every thread it is
291 * executed on.
292 */
293 smp_mb();
294 atomic_inc(&sig_done);
295 }
296
297 void __attribute__((constructor)) urcu_init(void)
298 {
299 struct sigaction act;
300 int ret;
301
302 act.sa_sigaction = sigurcu_handler;
303 ret = sigaction(SIGURCU, &act, NULL);
304 if (ret) {
305 perror("Error in sigaction");
306 exit(-1);
307 }
308 }
309
310 void __attribute__((destructor)) urcu_exit(void)
311 {
312 struct sigaction act;
313 int ret;
314
315 ret = sigaction(SIGURCU, NULL, &act);
316 if (ret) {
317 perror("Error in sigaction");
318 exit(-1);
319 }
320 assert(act.sa_sigaction == sigurcu_handler);
321 free(reader_data);
322 }
323 #endif
This page took 0.035976 seconds and 5 git commands to generate.