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