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 | |
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 | ||
4f8e3380 | 62 | static 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 MD |
102 | /* Read reader_gp before read futex */ |
103 | smp_rmb(); | |
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 | { |
4d703340 MD |
111 | LIST_HEAD(qsreaders); |
112 | int wait_loops = 0; | |
02be5561 | 113 | struct rcu_reader *index, *tmp; |
9f1621ca | 114 | |
2dfb8b5e | 115 | #if (BITS_PER_LONG < 64) |
32c15e4e | 116 | /* Switch parity: 0 -> 1, 1 -> 0 */ |
2dfb8b5e MD |
117 | STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR); |
118 | #else /* !(BITS_PER_LONG < 64) */ | |
119 | /* Increment current G.P. */ | |
120 | STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR); | |
121 | #endif /* !(BITS_PER_LONG < 64) */ | |
122 | ||
7a5a38f5 MD |
123 | /* |
124 | * Enforce compiler-order of store to rcu_gp_ctr before before | |
125 | * load rcu_reader ctr. | |
126 | * This ensures synchronize_rcu() cannot be starved by readers. | |
127 | */ | |
128 | barrier(); | |
129 | ||
9f1621ca | 130 | /* |
3395d46c | 131 | * Wait for each thread rcu_reader_qs_gp count to become 0. |
9f1621ca | 132 | */ |
4d703340 MD |
133 | for (;;) { |
134 | wait_loops++; | |
135 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
136 | uatomic_dec(&gp_futex); | |
137 | /* Write futex before read reader_gp */ | |
138 | smp_mb(); | |
139 | } | |
140 | ||
141 | list_for_each_entry_safe(index, tmp, ®istry, head) { | |
142 | if (!rcu_gp_ongoing(&index->ctr)) | |
143 | list_move(&index->head, &qsreaders); | |
144 | } | |
bc6c15bb | 145 | |
4d703340 MD |
146 | if (list_empty(®istry)) { |
147 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
148 | /* Read reader_gp before write futex */ | |
149 | smp_mb(); | |
150 | uatomic_set(&gp_futex, 0); | |
151 | } | |
152 | break; | |
153 | } else { | |
154 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
155 | wait_gp(); | |
bc6c15bb | 156 | } else { |
9f1621ca | 157 | #ifndef HAS_INCOHERENT_CACHES |
bc6c15bb | 158 | cpu_relax(); |
9f1621ca | 159 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
bc6c15bb | 160 | smp_mb(); |
9f1621ca | 161 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
bc6c15bb MD |
162 | } |
163 | } | |
9f1621ca | 164 | } |
4d703340 MD |
165 | /* put back the reader list in the registry */ |
166 | list_splice(&qsreaders, ®istry); | |
9f1621ca MD |
167 | } |
168 | ||
47d2f29e MD |
169 | /* |
170 | * Using a two-subphases algorithm for architectures with smaller than 64-bit | |
171 | * long-size to ensure we do not encounter an overflow bug. | |
172 | */ | |
173 | ||
174 | #if (BITS_PER_LONG < 64) | |
47d2f29e MD |
175 | void synchronize_rcu(void) |
176 | { | |
bc49c323 MD |
177 | unsigned long was_online; |
178 | ||
02be5561 | 179 | was_online = rcu_reader.ctr; |
bc49c323 | 180 | |
47d2f29e MD |
181 | /* All threads should read qparity before accessing data structure |
182 | * where new ptr points to. | |
183 | */ | |
184 | /* Write new ptr before changing the qparity */ | |
185 | smp_mb(); | |
186 | ||
bc49c323 MD |
187 | /* |
188 | * Mark the writer thread offline to make sure we don't wait for | |
189 | * our own quiescent state. This allows using synchronize_rcu() in | |
190 | * threads registered as readers. | |
191 | */ | |
192 | if (was_online) | |
02be5561 | 193 | STORE_SHARED(rcu_reader.ctr, 0); |
bc49c323 | 194 | |
6abb4bd5 | 195 | mutex_lock(&rcu_gp_lock); |
47d2f29e | 196 | |
2dfb8b5e MD |
197 | if (list_empty(®istry)) |
198 | goto out; | |
47d2f29e MD |
199 | |
200 | /* | |
201 | * Wait for previous parity to be empty of readers. | |
202 | */ | |
2dfb8b5e | 203 | update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */ |
47d2f29e MD |
204 | |
205 | /* | |
206 | * Must finish waiting for quiescent state for parity 0 before | |
207 | * committing qparity update to memory. Failure to do so could result in | |
208 | * the writer waiting forever while new readers are always accessing | |
209 | * data (no progress). | |
210 | * Ensured by STORE_SHARED and LOAD_SHARED. | |
211 | */ | |
212 | ||
47d2f29e | 213 | /* |
2dfb8b5e MD |
214 | * Adding a smp_mb() which is _not_ formally required, but makes the |
215 | * model easier to understand. It does not have a big performance impact | |
216 | * anyway, given this is the write-side. | |
47d2f29e | 217 | */ |
2dfb8b5e | 218 | smp_mb(); |
47d2f29e MD |
219 | |
220 | /* | |
221 | * Wait for previous parity to be empty of readers. | |
222 | */ | |
2dfb8b5e MD |
223 | update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */ |
224 | out: | |
6abb4bd5 | 225 | mutex_unlock(&rcu_gp_lock); |
47d2f29e | 226 | |
bc49c323 MD |
227 | /* |
228 | * Finish waiting for reader threads before letting the old ptr being | |
47d2f29e MD |
229 | * freed. |
230 | */ | |
bc49c323 | 231 | if (was_online) |
02be5561 | 232 | _STORE_SHARED(rcu_reader.ctr, LOAD_SHARED(rcu_gp_ctr)); |
47d2f29e MD |
233 | smp_mb(); |
234 | } | |
235 | #else /* !(BITS_PER_LONG < 64) */ | |
9f1621ca MD |
236 | void synchronize_rcu(void) |
237 | { | |
f0f7dbdd | 238 | unsigned long was_online; |
ff2f67a0 | 239 | |
02be5561 | 240 | was_online = rcu_reader.ctr; |
ff2f67a0 MD |
241 | |
242 | /* | |
243 | * Mark the writer thread offline to make sure we don't wait for | |
244 | * our own quiescent state. This allows using synchronize_rcu() in | |
245 | * threads registered as readers. | |
246 | */ | |
8f50d1ce | 247 | smp_mb(); |
ff2f67a0 | 248 | if (was_online) |
02be5561 | 249 | STORE_SHARED(rcu_reader.ctr, 0); |
ff2f67a0 | 250 | |
6abb4bd5 | 251 | mutex_lock(&rcu_gp_lock); |
2dfb8b5e MD |
252 | if (list_empty(®istry)) |
253 | goto out; | |
254 | update_counter_and_wait(); | |
255 | out: | |
6abb4bd5 | 256 | mutex_unlock(&rcu_gp_lock); |
ff2f67a0 MD |
257 | |
258 | if (was_online) | |
02be5561 | 259 | _STORE_SHARED(rcu_reader.ctr, LOAD_SHARED(rcu_gp_ctr)); |
8f50d1ce | 260 | smp_mb(); |
9f1621ca | 261 | } |
47d2f29e | 262 | #endif /* !(BITS_PER_LONG < 64) */ |
9f1621ca MD |
263 | |
264 | /* | |
265 | * library wrappers to be used by non-LGPL compatible source code. | |
266 | */ | |
267 | ||
268 | void rcu_read_lock(void) | |
269 | { | |
270 | _rcu_read_lock(); | |
271 | } | |
272 | ||
273 | void rcu_read_unlock(void) | |
274 | { | |
275 | _rcu_read_unlock(); | |
276 | } | |
277 | ||
7ac06cef MD |
278 | void rcu_quiescent_state(void) |
279 | { | |
280 | _rcu_quiescent_state(); | |
281 | } | |
282 | ||
283 | void rcu_thread_offline(void) | |
284 | { | |
285 | _rcu_thread_offline(); | |
286 | } | |
287 | ||
288 | void rcu_thread_online(void) | |
289 | { | |
290 | _rcu_thread_online(); | |
291 | } | |
292 | ||
9f1621ca MD |
293 | void rcu_register_thread(void) |
294 | { | |
02be5561 MD |
295 | rcu_reader.tid = pthread_self(); |
296 | assert(rcu_reader.ctr == 0); | |
4f8e3380 | 297 | |
6abb4bd5 | 298 | mutex_lock(&rcu_gp_lock); |
02be5561 | 299 | list_add(&rcu_reader.head, ®istry); |
6abb4bd5 | 300 | mutex_unlock(&rcu_gp_lock); |
5f373c84 | 301 | _rcu_thread_online(); |
9f1621ca MD |
302 | } |
303 | ||
304 | void rcu_unregister_thread(void) | |
305 | { | |
76f3022f MD |
306 | /* |
307 | * We have to make the thread offline otherwise we end up dealocking | |
308 | * with a waiting writer. | |
309 | */ | |
310 | _rcu_thread_offline(); | |
6abb4bd5 | 311 | mutex_lock(&rcu_gp_lock); |
02be5561 | 312 | list_del(&rcu_reader.head); |
6abb4bd5 | 313 | mutex_unlock(&rcu_gp_lock); |
9f1621ca | 314 | } |
f6d18c64 MD |
315 | |
316 | void rcu_exit(void) | |
317 | { | |
318 | assert(list_empty(®istry)); | |
319 | } |