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