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