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