Commit | Line | Data |
---|---|---|
9f1621ca | 1 | /* |
7ac06cef | 2 | * urcu-qsbr.c |
9f1621ca | 3 | * |
7ac06cef | 4 | * Userspace RCU QSBR library |
9f1621ca | 5 | * |
6982d6d7 | 6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
9f1621ca MD |
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 | ||
c1d2c60b | 26 | #define _GNU_SOURCE |
71c811bf | 27 | #define _LGPL_SOURCE |
9f1621ca MD |
28 | #include <stdio.h> |
29 | #include <pthread.h> | |
30 | #include <signal.h> | |
31 | #include <assert.h> | |
32 | #include <stdlib.h> | |
6d841bc2 | 33 | #include <stdint.h> |
9f1621ca MD |
34 | #include <string.h> |
35 | #include <errno.h> | |
36 | #include <poll.h> | |
37 | ||
71c811bf | 38 | #include "urcu/wfqueue.h" |
57760d44 | 39 | #include "urcu/map/urcu-qsbr.h" |
727f819d | 40 | #define BUILD_QSBR_LIB |
af7c2dbe | 41 | #include "urcu/static/urcu-qsbr.h" |
618b2595 | 42 | #include "urcu-pointer.h" |
bd252a04 | 43 | #include "urcu/tls-compat.h" |
71c811bf | 44 | |
4a6d7378 MD |
45 | #include "urcu-die.h" |
46 | ||
9f1621ca | 47 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
71c811bf | 48 | #undef _LGPL_SOURCE |
7ac06cef | 49 | #include "urcu-qsbr.h" |
71c811bf | 50 | #define _LGPL_SOURCE |
9f1621ca | 51 | |
f6d18c64 MD |
52 | void __attribute__((destructor)) rcu_exit(void); |
53 | ||
6abb4bd5 | 54 | static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; |
9f1621ca | 55 | |
6d841bc2 | 56 | int32_t gp_futex; |
bc6c15bb | 57 | |
9f1621ca MD |
58 | /* |
59 | * Global grace period counter. | |
60 | */ | |
02be5561 | 61 | unsigned long rcu_gp_ctr = RCU_GP_ONLINE; |
9f1621ca | 62 | |
408f6d92 PB |
63 | /* |
64 | * Active attempts to check for reader Q.S. before calling futex(). | |
65 | */ | |
66 | #define RCU_QS_ACTIVE_ATTEMPTS 100 | |
67 | ||
9f1621ca MD |
68 | /* |
69 | * Written to only by each individual reader. Read by both the reader and the | |
70 | * writers. | |
71 | */ | |
1745be1a | 72 | __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader, rcu_reader); |
9f1621ca MD |
73 | |
74 | #ifdef DEBUG_YIELD | |
75 | unsigned int yield_active; | |
1745be1a | 76 | __DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield); |
9f1621ca MD |
77 | #endif |
78 | ||
16aa9ee8 | 79 | static CDS_LIST_HEAD(registry); |
9f1621ca | 80 | |
6abb4bd5 | 81 | static void mutex_lock(pthread_mutex_t *mutex) |
9f1621ca MD |
82 | { |
83 | int ret; | |
84 | ||
85 | #ifndef DISTRUST_SIGNALS_EXTREME | |
6abb4bd5 | 86 | ret = pthread_mutex_lock(mutex); |
4a6d7378 MD |
87 | if (ret) |
88 | urcu_die(ret); | |
9f1621ca | 89 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
6abb4bd5 | 90 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
4a6d7378 MD |
91 | if (ret != EBUSY && ret != EINTR) |
92 | urcu_die(ret); | |
9f1621ca MD |
93 | poll(NULL,0,10); |
94 | } | |
95 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
96 | } | |
97 | ||
6abb4bd5 | 98 | static void mutex_unlock(pthread_mutex_t *mutex) |
9f1621ca MD |
99 | { |
100 | int ret; | |
101 | ||
6abb4bd5 | 102 | ret = pthread_mutex_unlock(mutex); |
4a6d7378 MD |
103 | if (ret) |
104 | urcu_die(ret); | |
9f1621ca MD |
105 | } |
106 | ||
bc6c15bb MD |
107 | /* |
108 | * synchronize_rcu() waiting. Single thread. | |
109 | */ | |
4d703340 | 110 | static void wait_gp(void) |
bc6c15bb | 111 | { |
4d703340 | 112 | /* Read reader_gp before read futex */ |
5481ddb3 | 113 | cmm_smp_rmb(); |
4d703340 | 114 | if (uatomic_read(&gp_futex) == -1) |
0854ccff | 115 | futex_noasync(&gp_futex, FUTEX_WAIT, -1, |
4d703340 | 116 | NULL, NULL, 0); |
bc6c15bb MD |
117 | } |
118 | ||
2dfb8b5e | 119 | static void update_counter_and_wait(void) |
9f1621ca | 120 | { |
16aa9ee8 | 121 | CDS_LIST_HEAD(qsreaders); |
6b702fa4 | 122 | unsigned int wait_loops = 0; |
02be5561 | 123 | struct rcu_reader *index, *tmp; |
9f1621ca | 124 | |
b39e1761 | 125 | #if (CAA_BITS_PER_LONG < 64) |
32c15e4e | 126 | /* Switch parity: 0 -> 1, 1 -> 0 */ |
6cf3827c | 127 | CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR); |
b39e1761 | 128 | #else /* !(CAA_BITS_PER_LONG < 64) */ |
2dfb8b5e | 129 | /* Increment current G.P. */ |
6cf3827c | 130 | CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR); |
b39e1761 | 131 | #endif /* !(CAA_BITS_PER_LONG < 64) */ |
2dfb8b5e | 132 | |
7a5a38f5 | 133 | /* |
5e77fc1f PM |
134 | * Must commit rcu_gp_ctr update to memory before waiting for |
135 | * quiescent state. Failure to do so could result in the writer | |
136 | * waiting forever while new readers are always accessing data | |
137 | * (no progress). Enforce compiler-order of store to rcu_gp_ctr | |
bd252a04 | 138 | * before load URCU_TLS(rcu_reader).ctr. |
d40fde2c | 139 | */ |
5481ddb3 | 140 | cmm_barrier(); |
d40fde2c MD |
141 | |
142 | /* | |
5481ddb3 | 143 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
935b11ff MD |
144 | * model easier to understand. It does not have a big performance impact |
145 | * anyway, given this is the write-side. | |
7a5a38f5 | 146 | */ |
5481ddb3 | 147 | cmm_smp_mb(); |
7a5a38f5 | 148 | |
9f1621ca | 149 | /* |
3395d46c | 150 | * Wait for each thread rcu_reader_qs_gp count to become 0. |
9f1621ca | 151 | */ |
4d703340 | 152 | for (;;) { |
cca4c8dc MD |
153 | if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) |
154 | wait_loops++; | |
83a2c421 PB |
155 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
156 | uatomic_set(&gp_futex, -1); | |
157 | /* | |
158 | * Write futex before write waiting (the other side | |
159 | * reads them in the opposite order). | |
160 | */ | |
161 | cmm_smp_wmb(); | |
162 | cds_list_for_each_entry(index, ®istry, node) { | |
163 | _CMM_STORE_SHARED(index->waiting, 1); | |
164 | } | |
4d703340 | 165 | /* Write futex before read reader_gp */ |
5481ddb3 | 166 | cmm_smp_mb(); |
4d703340 | 167 | } |
16aa9ee8 | 168 | cds_list_for_each_entry_safe(index, tmp, ®istry, node) { |
4d703340 | 169 | if (!rcu_gp_ongoing(&index->ctr)) |
16aa9ee8 | 170 | cds_list_move(&index->node, &qsreaders); |
4d703340 | 171 | } |
bc6c15bb | 172 | |
16aa9ee8 | 173 | if (cds_list_empty(®istry)) { |
83a2c421 | 174 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
4d703340 | 175 | /* Read reader_gp before write futex */ |
5481ddb3 | 176 | cmm_smp_mb(); |
4d703340 MD |
177 | uatomic_set(&gp_futex, 0); |
178 | } | |
179 | break; | |
180 | } else { | |
83a2c421 | 181 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
4d703340 | 182 | wait_gp(); |
bc6c15bb | 183 | } else { |
9f1621ca | 184 | #ifndef HAS_INCOHERENT_CACHES |
06f22bdb | 185 | caa_cpu_relax(); |
9f1621ca | 186 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
5481ddb3 | 187 | cmm_smp_mb(); |
9f1621ca | 188 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
bc6c15bb MD |
189 | } |
190 | } | |
9f1621ca | 191 | } |
4d703340 | 192 | /* put back the reader list in the registry */ |
16aa9ee8 | 193 | cds_list_splice(&qsreaders, ®istry); |
9f1621ca MD |
194 | } |
195 | ||
47d2f29e MD |
196 | /* |
197 | * Using a two-subphases algorithm for architectures with smaller than 64-bit | |
198 | * long-size to ensure we do not encounter an overflow bug. | |
199 | */ | |
200 | ||
b39e1761 | 201 | #if (CAA_BITS_PER_LONG < 64) |
47d2f29e MD |
202 | void synchronize_rcu(void) |
203 | { | |
bc49c323 MD |
204 | unsigned long was_online; |
205 | ||
bd252a04 | 206 | was_online = URCU_TLS(rcu_reader).ctr; |
bc49c323 | 207 | |
47d2f29e | 208 | /* All threads should read qparity before accessing data structure |
27b940e7 PB |
209 | * where new ptr points to. In the "then" case, rcu_thread_offline |
210 | * includes a memory barrier. | |
211 | * | |
bc49c323 | 212 | * Mark the writer thread offline to make sure we don't wait for |
5e77fc1f PM |
213 | * our own quiescent state. This allows using synchronize_rcu() |
214 | * in threads registered as readers. | |
bc49c323 | 215 | */ |
27b940e7 PB |
216 | if (was_online) |
217 | rcu_thread_offline(); | |
218 | else | |
219 | cmm_smp_mb(); | |
bc49c323 | 220 | |
6abb4bd5 | 221 | mutex_lock(&rcu_gp_lock); |
47d2f29e | 222 | |
16aa9ee8 | 223 | if (cds_list_empty(®istry)) |
2dfb8b5e | 224 | goto out; |
47d2f29e MD |
225 | |
226 | /* | |
227 | * Wait for previous parity to be empty of readers. | |
228 | */ | |
2dfb8b5e | 229 | update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */ |
47d2f29e MD |
230 | |
231 | /* | |
232 | * Must finish waiting for quiescent state for parity 0 before | |
5e77fc1f PM |
233 | * committing next rcu_gp_ctr update to memory. Failure to |
234 | * do so could result in the writer waiting forever while new | |
235 | * readers are always accessing data (no progress). Enforce | |
bd252a04 | 236 | * compiler-order of load URCU_TLS(rcu_reader).ctr before store to |
5e77fc1f | 237 | * rcu_gp_ctr. |
47d2f29e | 238 | */ |
5481ddb3 | 239 | cmm_barrier(); |
47d2f29e | 240 | |
47d2f29e | 241 | /* |
5481ddb3 | 242 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
2dfb8b5e MD |
243 | * model easier to understand. It does not have a big performance impact |
244 | * anyway, given this is the write-side. | |
47d2f29e | 245 | */ |
5481ddb3 | 246 | cmm_smp_mb(); |
47d2f29e MD |
247 | |
248 | /* | |
249 | * Wait for previous parity to be empty of readers. | |
250 | */ | |
2dfb8b5e MD |
251 | update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */ |
252 | out: | |
6abb4bd5 | 253 | mutex_unlock(&rcu_gp_lock); |
47d2f29e | 254 | |
bc49c323 MD |
255 | /* |
256 | * Finish waiting for reader threads before letting the old ptr being | |
47d2f29e MD |
257 | * freed. |
258 | */ | |
bc49c323 | 259 | if (was_online) |
27b940e7 PB |
260 | rcu_thread_online(); |
261 | else | |
262 | cmm_smp_mb(); | |
47d2f29e | 263 | } |
b39e1761 | 264 | #else /* !(CAA_BITS_PER_LONG < 64) */ |
9f1621ca MD |
265 | void synchronize_rcu(void) |
266 | { | |
f0f7dbdd | 267 | unsigned long was_online; |
ff2f67a0 | 268 | |
bd252a04 | 269 | was_online = URCU_TLS(rcu_reader).ctr; |
ff2f67a0 MD |
270 | |
271 | /* | |
272 | * Mark the writer thread offline to make sure we don't wait for | |
5e77fc1f PM |
273 | * our own quiescent state. This allows using synchronize_rcu() |
274 | * in threads registered as readers. | |
ff2f67a0 | 275 | */ |
27b940e7 PB |
276 | if (was_online) |
277 | rcu_thread_offline(); | |
278 | else | |
279 | cmm_smp_mb(); | |
ff2f67a0 | 280 | |
6abb4bd5 | 281 | mutex_lock(&rcu_gp_lock); |
16aa9ee8 | 282 | if (cds_list_empty(®istry)) |
2dfb8b5e MD |
283 | goto out; |
284 | update_counter_and_wait(); | |
285 | out: | |
6abb4bd5 | 286 | mutex_unlock(&rcu_gp_lock); |
ff2f67a0 MD |
287 | |
288 | if (was_online) | |
27b940e7 PB |
289 | rcu_thread_online(); |
290 | else | |
291 | cmm_smp_mb(); | |
9f1621ca | 292 | } |
b39e1761 | 293 | #endif /* !(CAA_BITS_PER_LONG < 64) */ |
9f1621ca MD |
294 | |
295 | /* | |
296 | * library wrappers to be used by non-LGPL compatible source code. | |
297 | */ | |
298 | ||
299 | void rcu_read_lock(void) | |
300 | { | |
301 | _rcu_read_lock(); | |
302 | } | |
303 | ||
304 | void rcu_read_unlock(void) | |
305 | { | |
306 | _rcu_read_unlock(); | |
307 | } | |
308 | ||
7ac06cef MD |
309 | void rcu_quiescent_state(void) |
310 | { | |
311 | _rcu_quiescent_state(); | |
312 | } | |
313 | ||
314 | void rcu_thread_offline(void) | |
315 | { | |
316 | _rcu_thread_offline(); | |
317 | } | |
318 | ||
319 | void rcu_thread_online(void) | |
320 | { | |
321 | _rcu_thread_online(); | |
322 | } | |
323 | ||
9f1621ca MD |
324 | void rcu_register_thread(void) |
325 | { | |
bd252a04 MD |
326 | URCU_TLS(rcu_reader).tid = pthread_self(); |
327 | assert(URCU_TLS(rcu_reader).ctr == 0); | |
4f8e3380 | 328 | |
6abb4bd5 | 329 | mutex_lock(&rcu_gp_lock); |
bd252a04 | 330 | cds_list_add(&URCU_TLS(rcu_reader).node, ®istry); |
6abb4bd5 | 331 | mutex_unlock(&rcu_gp_lock); |
5f373c84 | 332 | _rcu_thread_online(); |
9f1621ca MD |
333 | } |
334 | ||
335 | void rcu_unregister_thread(void) | |
336 | { | |
76f3022f MD |
337 | /* |
338 | * We have to make the thread offline otherwise we end up dealocking | |
339 | * with a waiting writer. | |
340 | */ | |
341 | _rcu_thread_offline(); | |
6abb4bd5 | 342 | mutex_lock(&rcu_gp_lock); |
bd252a04 | 343 | cds_list_del(&URCU_TLS(rcu_reader).node); |
6abb4bd5 | 344 | mutex_unlock(&rcu_gp_lock); |
9f1621ca | 345 | } |
f6d18c64 MD |
346 | |
347 | void rcu_exit(void) | |
348 | { | |
01cadde4 MD |
349 | /* |
350 | * Assertion disabled because call_rcu threads are now rcu | |
351 | * readers, and left running at exit. | |
352 | * assert(cds_list_empty(®istry)); | |
353 | */ | |
f6d18c64 | 354 | } |
5e77fc1f | 355 | |
5e6b23a6 | 356 | DEFINE_RCU_FLAVOR(rcu_flavor); |
541d828d | 357 | |
5e77fc1f | 358 | #include "urcu-call-rcu-impl.h" |
0376e7b2 | 359 | #include "urcu-defer-impl.h" |