Commit | Line | Data |
---|---|---|
9f1621ca | 1 | /* |
7ac06cef | 2 | * urcu-qsbr.c |
9f1621ca | 3 | * |
7ac06cef | 4 | * Userspace RCU QSBR library |
9f1621ca MD |
5 | * |
6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
7 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | * | |
23 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. | |
24 | */ | |
25 | ||
26 | #include <stdio.h> | |
27 | #include <pthread.h> | |
28 | #include <signal.h> | |
29 | #include <assert.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | #include <errno.h> | |
33 | #include <poll.h> | |
34 | ||
727f819d | 35 | #define BUILD_QSBR_LIB |
7ac06cef | 36 | #include "urcu-qsbr-static.h" |
9f1621ca | 37 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
7ac06cef | 38 | #include "urcu-qsbr.h" |
9f1621ca | 39 | |
81d1f1f5 | 40 | static pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
9f1621ca MD |
41 | |
42 | /* | |
43 | * Global grace period counter. | |
44 | */ | |
ac258107 | 45 | unsigned long urcu_gp_ctr = RCU_GP_ONLINE; |
9f1621ca MD |
46 | |
47 | /* | |
48 | * Written to only by each individual reader. Read by both the reader and the | |
49 | * writers. | |
50 | */ | |
f0f7dbdd | 51 | unsigned long __thread rcu_reader_qs_gp; |
9f1621ca MD |
52 | |
53 | /* Thread IDs of registered readers */ | |
54 | #define INIT_NUM_THREADS 4 | |
55 | ||
56 | struct reader_registry { | |
57 | pthread_t tid; | |
f0f7dbdd | 58 | unsigned long *rcu_reader_qs_gp; |
9f1621ca MD |
59 | }; |
60 | ||
61 | #ifdef DEBUG_YIELD | |
62 | unsigned int yield_active; | |
63 | unsigned int __thread rand_yield; | |
64 | #endif | |
65 | ||
66 | static struct reader_registry *registry; | |
9f1621ca MD |
67 | static int num_readers, alloc_readers; |
68 | ||
90c1618a | 69 | static void internal_urcu_lock(void) |
9f1621ca MD |
70 | { |
71 | int ret; | |
72 | ||
73 | #ifndef DISTRUST_SIGNALS_EXTREME | |
74 | ret = pthread_mutex_lock(&urcu_mutex); | |
75 | if (ret) { | |
76 | perror("Error in pthread mutex lock"); | |
77 | exit(-1); | |
78 | } | |
79 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ | |
80 | while ((ret = pthread_mutex_trylock(&urcu_mutex)) != 0) { | |
81 | if (ret != EBUSY && ret != EINTR) { | |
82 | printf("ret = %d, errno = %d\n", ret, errno); | |
83 | perror("Error in pthread mutex lock"); | |
84 | exit(-1); | |
85 | } | |
9f1621ca MD |
86 | poll(NULL,0,10); |
87 | } | |
88 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
89 | } | |
90 | ||
90c1618a | 91 | static void internal_urcu_unlock(void) |
9f1621ca MD |
92 | { |
93 | int ret; | |
94 | ||
95 | ret = pthread_mutex_unlock(&urcu_mutex); | |
96 | if (ret) { | |
97 | perror("Error in pthread mutex unlock"); | |
98 | exit(-1); | |
99 | } | |
100 | } | |
101 | ||
bc6c15bb MD |
102 | /* |
103 | * synchronize_rcu() waiting. Single thread. | |
104 | */ | |
90c1618a | 105 | static void wait_for_quiescent_state(void) |
9f1621ca MD |
106 | { |
107 | struct reader_registry *index; | |
108 | ||
109 | if (!registry) | |
110 | return; | |
111 | /* | |
3395d46c | 112 | * Wait for each thread rcu_reader_qs_gp count to become 0. |
9f1621ca MD |
113 | */ |
114 | for (index = registry; index < registry + num_readers; index++) { | |
bc6c15bb MD |
115 | int wait_loops = 0; |
116 | ||
117 | while (rcu_gp_ongoing(index->rcu_reader_qs_gp)) { | |
118 | if (wait_loops++ == RCU_QS_ACTIVE_ATTEMPTS) { | |
ae62b5e8 | 119 | sched_yield(); /* ideally sched_yield_to() */ |
bc6c15bb | 120 | } else { |
9f1621ca | 121 | #ifndef HAS_INCOHERENT_CACHES |
bc6c15bb | 122 | cpu_relax(); |
9f1621ca | 123 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
bc6c15bb | 124 | smp_mb(); |
9f1621ca | 125 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
bc6c15bb MD |
126 | } |
127 | } | |
9f1621ca MD |
128 | } |
129 | } | |
130 | ||
47d2f29e MD |
131 | /* |
132 | * Using a two-subphases algorithm for architectures with smaller than 64-bit | |
133 | * long-size to ensure we do not encounter an overflow bug. | |
134 | */ | |
135 | ||
136 | #if (BITS_PER_LONG < 64) | |
137 | /* | |
138 | * called with urcu_mutex held. | |
139 | */ | |
140 | static void switch_next_urcu_qparity(void) | |
141 | { | |
142 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR); | |
143 | } | |
144 | ||
145 | void synchronize_rcu(void) | |
146 | { | |
bc49c323 MD |
147 | unsigned long was_online; |
148 | ||
149 | was_online = rcu_reader_qs_gp; | |
150 | ||
47d2f29e MD |
151 | /* All threads should read qparity before accessing data structure |
152 | * where new ptr points to. | |
153 | */ | |
154 | /* Write new ptr before changing the qparity */ | |
155 | smp_mb(); | |
156 | ||
bc49c323 MD |
157 | /* |
158 | * Mark the writer thread offline to make sure we don't wait for | |
159 | * our own quiescent state. This allows using synchronize_rcu() in | |
160 | * threads registered as readers. | |
161 | */ | |
162 | if (was_online) | |
163 | STORE_SHARED(rcu_reader_qs_gp, 0); | |
164 | ||
47d2f29e MD |
165 | internal_urcu_lock(); |
166 | ||
ae62b5e8 MD |
167 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
168 | ||
47d2f29e MD |
169 | switch_next_urcu_qparity(); /* 0 -> 1 */ |
170 | ||
171 | /* | |
172 | * Must commit qparity update to memory before waiting for parity | |
173 | * 0 quiescent state. Failure to do so could result in the writer | |
174 | * waiting forever while new readers are always accessing data (no | |
175 | * progress). | |
176 | * Ensured by STORE_SHARED and LOAD_SHARED. | |
177 | */ | |
178 | ||
179 | /* | |
180 | * Wait for previous parity to be empty of readers. | |
181 | */ | |
182 | wait_for_quiescent_state(); /* Wait readers in parity 0 */ | |
183 | ||
184 | /* | |
185 | * Must finish waiting for quiescent state for parity 0 before | |
186 | * committing qparity update to memory. Failure to do so could result in | |
187 | * the writer waiting forever while new readers are always accessing | |
188 | * data (no progress). | |
189 | * Ensured by STORE_SHARED and LOAD_SHARED. | |
190 | */ | |
191 | ||
192 | switch_next_urcu_qparity(); /* 1 -> 0 */ | |
193 | ||
194 | /* | |
195 | * Must commit qparity update to memory before waiting for parity | |
196 | * 1 quiescent state. Failure to do so could result in the writer | |
197 | * waiting forever while new readers are always accessing data (no | |
198 | * progress). | |
199 | * Ensured by STORE_SHARED and LOAD_SHARED. | |
200 | */ | |
201 | ||
202 | /* | |
203 | * Wait for previous parity to be empty of readers. | |
204 | */ | |
205 | wait_for_quiescent_state(); /* Wait readers in parity 1 */ | |
206 | ||
ae62b5e8 MD |
207 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
208 | ||
47d2f29e MD |
209 | internal_urcu_unlock(); |
210 | ||
bc49c323 MD |
211 | /* |
212 | * Finish waiting for reader threads before letting the old ptr being | |
47d2f29e MD |
213 | * freed. |
214 | */ | |
bc49c323 MD |
215 | if (was_online) |
216 | _STORE_SHARED(rcu_reader_qs_gp, LOAD_SHARED(urcu_gp_ctr)); | |
47d2f29e MD |
217 | smp_mb(); |
218 | } | |
219 | #else /* !(BITS_PER_LONG < 64) */ | |
9f1621ca MD |
220 | void synchronize_rcu(void) |
221 | { | |
f0f7dbdd | 222 | unsigned long was_online; |
ff2f67a0 | 223 | |
ab179a17 | 224 | was_online = rcu_reader_qs_gp; |
ff2f67a0 MD |
225 | |
226 | /* | |
227 | * Mark the writer thread offline to make sure we don't wait for | |
228 | * our own quiescent state. This allows using synchronize_rcu() in | |
229 | * threads registered as readers. | |
230 | */ | |
8f50d1ce | 231 | smp_mb(); |
ff2f67a0 | 232 | if (was_online) |
8f50d1ce | 233 | STORE_SHARED(rcu_reader_qs_gp, 0); |
ff2f67a0 | 234 | |
4e27f058 | 235 | internal_urcu_lock(); |
ae62b5e8 | 236 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
55570466 | 237 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr + RCU_GP_CTR); |
9f1621ca | 238 | wait_for_quiescent_state(); |
ae62b5e8 | 239 | STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING); |
9f1621ca | 240 | internal_urcu_unlock(); |
ff2f67a0 MD |
241 | |
242 | if (was_online) | |
ab179a17 | 243 | _STORE_SHARED(rcu_reader_qs_gp, LOAD_SHARED(urcu_gp_ctr)); |
8f50d1ce | 244 | smp_mb(); |
9f1621ca | 245 | } |
47d2f29e | 246 | #endif /* !(BITS_PER_LONG < 64) */ |
9f1621ca MD |
247 | |
248 | /* | |
249 | * library wrappers to be used by non-LGPL compatible source code. | |
250 | */ | |
251 | ||
252 | void rcu_read_lock(void) | |
253 | { | |
254 | _rcu_read_lock(); | |
255 | } | |
256 | ||
257 | void rcu_read_unlock(void) | |
258 | { | |
259 | _rcu_read_unlock(); | |
260 | } | |
261 | ||
262 | void *rcu_dereference(void *p) | |
263 | { | |
264 | return _rcu_dereference(p); | |
265 | } | |
266 | ||
267 | void *rcu_assign_pointer_sym(void **p, void *v) | |
268 | { | |
269 | wmb(); | |
270 | return STORE_SHARED(p, v); | |
271 | } | |
272 | ||
4d1ce26f MD |
273 | void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new) |
274 | { | |
275 | wmb(); | |
276 | return cmpxchg(p, old, _new); | |
277 | } | |
278 | ||
9f1621ca MD |
279 | void *rcu_xchg_pointer_sym(void **p, void *v) |
280 | { | |
281 | wmb(); | |
282 | return xchg(p, v); | |
283 | } | |
284 | ||
285 | void *rcu_publish_content_sym(void **p, void *v) | |
286 | { | |
287 | void *oldptr; | |
288 | ||
289 | oldptr = _rcu_xchg_pointer(p, v); | |
290 | synchronize_rcu(); | |
291 | return oldptr; | |
292 | } | |
293 | ||
7ac06cef MD |
294 | void rcu_quiescent_state(void) |
295 | { | |
296 | _rcu_quiescent_state(); | |
297 | } | |
298 | ||
299 | void rcu_thread_offline(void) | |
300 | { | |
301 | _rcu_thread_offline(); | |
302 | } | |
303 | ||
304 | void rcu_thread_online(void) | |
305 | { | |
306 | _rcu_thread_online(); | |
307 | } | |
308 | ||
9f1621ca MD |
309 | static void rcu_add_reader(pthread_t id) |
310 | { | |
311 | struct reader_registry *oldarray; | |
312 | ||
313 | if (!registry) { | |
314 | alloc_readers = INIT_NUM_THREADS; | |
315 | num_readers = 0; | |
316 | registry = | |
317 | malloc(sizeof(struct reader_registry) * alloc_readers); | |
318 | } | |
319 | if (alloc_readers < num_readers + 1) { | |
320 | oldarray = registry; | |
321 | registry = malloc(sizeof(struct reader_registry) | |
322 | * (alloc_readers << 1)); | |
323 | memcpy(registry, oldarray, | |
324 | sizeof(struct reader_registry) * alloc_readers); | |
325 | alloc_readers <<= 1; | |
326 | free(oldarray); | |
327 | } | |
328 | registry[num_readers].tid = id; | |
329 | /* reference to the TLS of _this_ reader thread. */ | |
3395d46c | 330 | registry[num_readers].rcu_reader_qs_gp = &rcu_reader_qs_gp; |
9f1621ca MD |
331 | num_readers++; |
332 | } | |
333 | ||
334 | /* | |
335 | * Never shrink (implementation limitation). | |
336 | * This is O(nb threads). Eventually use a hash table. | |
337 | */ | |
338 | static void rcu_remove_reader(pthread_t id) | |
339 | { | |
340 | struct reader_registry *index; | |
341 | ||
342 | assert(registry != NULL); | |
343 | for (index = registry; index < registry + num_readers; index++) { | |
344 | if (pthread_equal(index->tid, id)) { | |
345 | memcpy(index, ®istry[num_readers - 1], | |
346 | sizeof(struct reader_registry)); | |
347 | registry[num_readers - 1].tid = 0; | |
3395d46c | 348 | registry[num_readers - 1].rcu_reader_qs_gp = NULL; |
9f1621ca MD |
349 | num_readers--; |
350 | return; | |
351 | } | |
352 | } | |
353 | /* Hrm not found, forgot to register ? */ | |
354 | assert(0); | |
355 | } | |
356 | ||
357 | void rcu_register_thread(void) | |
358 | { | |
359 | internal_urcu_lock(); | |
9f1621ca MD |
360 | rcu_add_reader(pthread_self()); |
361 | internal_urcu_unlock(); | |
5f373c84 | 362 | _rcu_thread_online(); |
9f1621ca MD |
363 | } |
364 | ||
365 | void rcu_unregister_thread(void) | |
366 | { | |
76f3022f MD |
367 | /* |
368 | * We have to make the thread offline otherwise we end up dealocking | |
369 | * with a waiting writer. | |
370 | */ | |
371 | _rcu_thread_offline(); | |
9f1621ca MD |
372 | internal_urcu_lock(); |
373 | rcu_remove_reader(pthread_self()); | |
374 | internal_urcu_unlock(); | |
375 | } |