tests: use standard malloc/free for synchronize_rcu()
[urcu.git] / urcu-qsbr.c
... / ...
CommitLineData
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/wfcqueue.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
52void __attribute__((destructor)) rcu_exit(void);
53
54static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
55
56int32_t rcu_gp_futex;
57
58/*
59 * Global grace period counter.
60 */
61unsigned 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 */
72DEFINE_URCU_TLS(struct rcu_reader, rcu_reader);
73
74#ifdef DEBUG_YIELD
75unsigned int rcu_yield_active;
76DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
77#endif
78
79static CDS_LIST_HEAD(registry);
80
81static 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
98static 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 */
110static void wait_gp(void)
111{
112 /* Read reader_gp before read futex */
113 cmm_smp_rmb();
114 if (uatomic_read(&rcu_gp_futex) == -1)
115 futex_noasync(&rcu_gp_futex, FUTEX_WAIT, -1,
116 NULL, NULL, 0);
117}
118
119static void wait_for_readers(struct cds_list_head *input_readers,
120 struct cds_list_head *cur_snap_readers,
121 struct cds_list_head *qsreaders)
122{
123 int wait_loops = 0;
124 struct rcu_reader *index, *tmp;
125
126 /*
127 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
128 * indicate quiescence (offline), or for them to observe the
129 * current rcu_gp_ctr value.
130 */
131 for (;;) {
132 wait_loops++;
133 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
134 uatomic_set(&rcu_gp_futex, -1);
135 /*
136 * Write futex before write waiting (the other side
137 * reads them in the opposite order).
138 */
139 cmm_smp_wmb();
140 cds_list_for_each_entry(index, input_readers, node) {
141 _CMM_STORE_SHARED(index->waiting, 1);
142 }
143 /* Write futex before read reader_gp */
144 cmm_smp_mb();
145 }
146 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
147 switch (rcu_reader_state(&index->ctr)) {
148 case RCU_READER_ACTIVE_CURRENT:
149 if (cur_snap_readers) {
150 cds_list_move(&index->node,
151 cur_snap_readers);
152 break;
153 }
154 /* Fall-through */
155 case RCU_READER_INACTIVE:
156 cds_list_move(&index->node, qsreaders);
157 break;
158 case RCU_READER_ACTIVE_OLD:
159 /*
160 * Old snapshot. Leaving node in
161 * input_readers will make us busy-loop
162 * until the snapshot becomes current or
163 * the reader becomes inactive.
164 */
165 break;
166 }
167 }
168
169 if (cds_list_empty(input_readers)) {
170 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
171 /* Read reader_gp before write futex */
172 cmm_smp_mb();
173 uatomic_set(&rcu_gp_futex, 0);
174 }
175 break;
176 } else {
177 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
178 wait_gp();
179 } else {
180#ifndef HAS_INCOHERENT_CACHES
181 caa_cpu_relax();
182#else /* #ifndef HAS_INCOHERENT_CACHES */
183 cmm_smp_mb();
184#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
185 }
186 }
187 }
188}
189
190/*
191 * Using a two-subphases algorithm for architectures with smaller than 64-bit
192 * long-size to ensure we do not encounter an overflow bug.
193 */
194
195#if (CAA_BITS_PER_LONG < 64)
196void synchronize_rcu(void)
197{
198 CDS_LIST_HEAD(cur_snap_readers);
199 CDS_LIST_HEAD(qsreaders);
200 unsigned long was_online;
201
202 was_online = URCU_TLS(rcu_reader).ctr;
203
204 /* All threads should read qparity before accessing data structure
205 * where new ptr points to. In the "then" case, rcu_thread_offline
206 * includes a memory barrier.
207 *
208 * Mark the writer thread offline to make sure we don't wait for
209 * our own quiescent state. This allows using synchronize_rcu()
210 * in threads registered as readers.
211 */
212 if (was_online)
213 rcu_thread_offline();
214 else
215 cmm_smp_mb();
216
217 mutex_lock(&rcu_gp_lock);
218
219 if (cds_list_empty(&registry))
220 goto out;
221
222 /*
223 * Wait for readers to observe original parity or be quiescent.
224 */
225 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
226
227 /*
228 * Must finish waiting for quiescent state for original parity
229 * before committing next rcu_gp_ctr update to memory. Failure
230 * to do so could result in the writer waiting forever while new
231 * readers are always accessing data (no progress). Enforce
232 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
233 * to rcu_gp_ctr.
234 */
235 cmm_barrier();
236
237 /*
238 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
239 * model easier to understand. It does not have a big performance impact
240 * anyway, given this is the write-side.
241 */
242 cmm_smp_mb();
243
244 /* Switch parity: 0 -> 1, 1 -> 0 */
245 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
246
247 /*
248 * Must commit rcu_gp_ctr update to memory before waiting for
249 * quiescent state. Failure to do so could result in the writer
250 * waiting forever while new readers are always accessing data
251 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
252 * before load URCU_TLS(rcu_reader).ctr.
253 */
254 cmm_barrier();
255
256 /*
257 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
258 * model easier to understand. It does not have a big performance impact
259 * anyway, given this is the write-side.
260 */
261 cmm_smp_mb();
262
263 /*
264 * Wait for readers to observe new parity or be quiescent.
265 */
266 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
267
268 /*
269 * Put quiescent reader list back into registry.
270 */
271 cds_list_splice(&qsreaders, &registry);
272out:
273 mutex_unlock(&rcu_gp_lock);
274
275 /*
276 * Finish waiting for reader threads before letting the old ptr being
277 * freed.
278 */
279 if (was_online)
280 rcu_thread_online();
281 else
282 cmm_smp_mb();
283}
284#else /* !(CAA_BITS_PER_LONG < 64) */
285void synchronize_rcu(void)
286{
287 CDS_LIST_HEAD(qsreaders);
288 unsigned long was_online;
289
290 was_online = URCU_TLS(rcu_reader).ctr;
291
292 /*
293 * Mark the writer thread offline to make sure we don't wait for
294 * our own quiescent state. This allows using synchronize_rcu()
295 * in threads registered as readers.
296 */
297 if (was_online)
298 rcu_thread_offline();
299 else
300 cmm_smp_mb();
301
302 mutex_lock(&rcu_gp_lock);
303 if (cds_list_empty(&registry))
304 goto out;
305
306 /* Increment current G.P. */
307 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
308
309 /*
310 * Must commit rcu_gp_ctr update to memory before waiting for
311 * quiescent state. Failure to do so could result in the writer
312 * waiting forever while new readers are always accessing data
313 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
314 * before load URCU_TLS(rcu_reader).ctr.
315 */
316 cmm_barrier();
317
318 /*
319 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
320 * model easier to understand. It does not have a big performance impact
321 * anyway, given this is the write-side.
322 */
323 cmm_smp_mb();
324
325 /*
326 * Wait for readers to observe new count of be quiescent.
327 */
328 wait_for_readers(&registry, NULL, &qsreaders);
329
330 /*
331 * Put quiescent reader list back into registry.
332 */
333 cds_list_splice(&qsreaders, &registry);
334out:
335 mutex_unlock(&rcu_gp_lock);
336
337 if (was_online)
338 rcu_thread_online();
339 else
340 cmm_smp_mb();
341}
342#endif /* !(CAA_BITS_PER_LONG < 64) */
343
344/*
345 * library wrappers to be used by non-LGPL compatible source code.
346 */
347
348void rcu_read_lock(void)
349{
350 _rcu_read_lock();
351}
352
353void rcu_read_unlock(void)
354{
355 _rcu_read_unlock();
356}
357
358void rcu_quiescent_state(void)
359{
360 _rcu_quiescent_state();
361}
362
363void rcu_thread_offline(void)
364{
365 _rcu_thread_offline();
366}
367
368void rcu_thread_online(void)
369{
370 _rcu_thread_online();
371}
372
373void rcu_register_thread(void)
374{
375 URCU_TLS(rcu_reader).tid = pthread_self();
376 assert(URCU_TLS(rcu_reader).ctr == 0);
377
378 mutex_lock(&rcu_gp_lock);
379 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
380 mutex_unlock(&rcu_gp_lock);
381 _rcu_thread_online();
382}
383
384void rcu_unregister_thread(void)
385{
386 /*
387 * We have to make the thread offline otherwise we end up dealocking
388 * with a waiting writer.
389 */
390 _rcu_thread_offline();
391 mutex_lock(&rcu_gp_lock);
392 cds_list_del(&URCU_TLS(rcu_reader).node);
393 mutex_unlock(&rcu_gp_lock);
394}
395
396void rcu_exit(void)
397{
398 /*
399 * Assertion disabled because call_rcu threads are now rcu
400 * readers, and left running at exit.
401 * assert(cds_list_empty(&registry));
402 */
403}
404
405DEFINE_RCU_FLAVOR(rcu_flavor);
406
407#include "urcu-call-rcu-impl.h"
408#include "urcu-defer-impl.h"
This page took 0.023032 seconds and 4 git commands to generate.