tests: update writer cs len script for 64-core ppc
[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@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
35#define BUILD_QSBR_LIB
36#include "urcu-qsbr-static.h"
37/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
38#include "urcu-qsbr.h"
39
40void __attribute__((destructor)) rcu_exit(void);
41
42static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
43
44int gp_futex;
45
46/*
47 * Global grace period counter.
48 */
49unsigned long rcu_gp_ctr = RCU_GP_ONLINE;
50
51/*
52 * Written to only by each individual reader. Read by both the reader and the
53 * writers.
54 */
55struct rcu_reader __thread rcu_reader;
56
57#ifdef DEBUG_YIELD
58unsigned int yield_active;
59unsigned int __thread rand_yield;
60#endif
61
62static LIST_HEAD(registry);
63
64static void mutex_lock(pthread_mutex_t *mutex)
65{
66 int ret;
67
68#ifndef DISTRUST_SIGNALS_EXTREME
69 ret = pthread_mutex_lock(mutex);
70 if (ret) {
71 perror("Error in pthread mutex lock");
72 exit(-1);
73 }
74#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
75 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
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 }
81 poll(NULL,0,10);
82 }
83#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
84}
85
86static void mutex_unlock(pthread_mutex_t *mutex)
87{
88 int ret;
89
90 ret = pthread_mutex_unlock(mutex);
91 if (ret) {
92 perror("Error in pthread mutex unlock");
93 exit(-1);
94 }
95}
96
97/*
98 * synchronize_rcu() waiting. Single thread.
99 */
100static void wait_gp(void)
101{
102 /* Read reader_gp before read futex */
103 smp_rmb();
104 if (uatomic_read(&gp_futex) == -1)
105 futex_noasync(&gp_futex, FUTEX_WAIT, -1,
106 NULL, NULL, 0);
107}
108
109static void update_counter_and_wait(void)
110{
111 LIST_HEAD(qsreaders);
112 int wait_loops = 0;
113 struct rcu_reader *index, *tmp;
114
115#if (BITS_PER_LONG < 64)
116 /* Switch parity: 0 -> 1, 1 -> 0 */
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
123 /*
124 * Wait for each thread rcu_reader_qs_gp count to become 0.
125 */
126 for (;;) {
127 wait_loops++;
128 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
129 uatomic_dec(&gp_futex);
130 /* Write futex before read reader_gp */
131 smp_mb();
132 }
133
134 list_for_each_entry_safe(index, tmp, &registry, head) {
135 if (!rcu_gp_ongoing(&index->ctr))
136 list_move(&index->head, &qsreaders);
137 }
138
139 if (list_empty(&registry)) {
140 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
141 /* Read reader_gp before write futex */
142 smp_mb();
143 uatomic_set(&gp_futex, 0);
144 }
145 break;
146 } else {
147 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
148 wait_gp();
149 } else {
150#ifndef HAS_INCOHERENT_CACHES
151 cpu_relax();
152#else /* #ifndef HAS_INCOHERENT_CACHES */
153 smp_mb();
154#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
155 }
156 }
157 }
158 /* put back the reader list in the registry */
159 list_splice(&qsreaders, &registry);
160}
161
162/*
163 * Using a two-subphases algorithm for architectures with smaller than 64-bit
164 * long-size to ensure we do not encounter an overflow bug.
165 */
166
167#if (BITS_PER_LONG < 64)
168void synchronize_rcu(void)
169{
170 unsigned long was_online;
171
172 was_online = rcu_reader.ctr;
173
174 /* All threads should read qparity before accessing data structure
175 * where new ptr points to.
176 */
177 /* Write new ptr before changing the qparity */
178 smp_mb();
179
180 /*
181 * Mark the writer thread offline to make sure we don't wait for
182 * our own quiescent state. This allows using synchronize_rcu() in
183 * threads registered as readers.
184 */
185 if (was_online)
186 STORE_SHARED(rcu_reader.ctr, 0);
187
188 mutex_lock(&rcu_gp_lock);
189
190 if (list_empty(&registry))
191 goto out;
192
193 /*
194 * Wait for previous parity to be empty of readers.
195 */
196 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
197
198 /*
199 * Must finish waiting for quiescent state for parity 0 before
200 * committing qparity update to memory. Failure to do so could result in
201 * the writer waiting forever while new readers are always accessing
202 * data (no progress).
203 * Ensured by STORE_SHARED and LOAD_SHARED.
204 */
205
206 /*
207 * Adding a smp_mb() which is _not_ formally required, but makes the
208 * model easier to understand. It does not have a big performance impact
209 * anyway, given this is the write-side.
210 */
211 smp_mb();
212
213 /*
214 * Wait for previous parity to be empty of readers.
215 */
216 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
217out:
218 mutex_unlock(&rcu_gp_lock);
219
220 /*
221 * Finish waiting for reader threads before letting the old ptr being
222 * freed.
223 */
224 if (was_online)
225 _STORE_SHARED(rcu_reader.ctr, LOAD_SHARED(rcu_gp_ctr));
226 smp_mb();
227}
228#else /* !(BITS_PER_LONG < 64) */
229void synchronize_rcu(void)
230{
231 unsigned long was_online;
232
233 was_online = rcu_reader.ctr;
234
235 /*
236 * Mark the writer thread offline to make sure we don't wait for
237 * our own quiescent state. This allows using synchronize_rcu() in
238 * threads registered as readers.
239 */
240 smp_mb();
241 if (was_online)
242 STORE_SHARED(rcu_reader.ctr, 0);
243
244 mutex_lock(&rcu_gp_lock);
245 if (list_empty(&registry))
246 goto out;
247 update_counter_and_wait();
248out:
249 mutex_unlock(&rcu_gp_lock);
250
251 if (was_online)
252 _STORE_SHARED(rcu_reader.ctr, LOAD_SHARED(rcu_gp_ctr));
253 smp_mb();
254}
255#endif /* !(BITS_PER_LONG < 64) */
256
257/*
258 * library wrappers to be used by non-LGPL compatible source code.
259 */
260
261void rcu_read_lock(void)
262{
263 _rcu_read_lock();
264}
265
266void rcu_read_unlock(void)
267{
268 _rcu_read_unlock();
269}
270
271void rcu_quiescent_state(void)
272{
273 _rcu_quiescent_state();
274}
275
276void rcu_thread_offline(void)
277{
278 _rcu_thread_offline();
279}
280
281void rcu_thread_online(void)
282{
283 _rcu_thread_online();
284}
285
286void rcu_register_thread(void)
287{
288 rcu_reader.tid = pthread_self();
289 assert(rcu_reader.ctr == 0);
290
291 mutex_lock(&rcu_gp_lock);
292 list_add(&rcu_reader.head, &registry);
293 mutex_unlock(&rcu_gp_lock);
294 _rcu_thread_online();
295}
296
297void rcu_unregister_thread(void)
298{
299 /*
300 * We have to make the thread offline otherwise we end up dealocking
301 * with a waiting writer.
302 */
303 _rcu_thread_offline();
304 mutex_lock(&rcu_gp_lock);
305 list_del(&rcu_reader.head);
306 mutex_unlock(&rcu_gp_lock);
307}
308
309void rcu_exit(void)
310{
311 assert(list_empty(&registry));
312}
This page took 0.022259 seconds and 4 git commands to generate.