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