fix api.h generation on x86 and powerpc
[urcu.git] / urcu-qsbr.c
CommitLineData
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
40void __attribute__((destructor)) rcu_exit(void);
41
81d1f1f5 42static pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
9f1621ca 43
bc6c15bb
MD
44int gp_futex;
45
9f1621ca
MD
46/*
47 * Global grace period counter.
48 */
ac258107 49unsigned long urcu_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 */
4f8e3380 55struct urcu_reader __thread urcu_reader;
9f1621ca
MD
56
57#ifdef DEBUG_YIELD
58unsigned int yield_active;
59unsigned int __thread rand_yield;
60#endif
61
4f8e3380 62static LIST_HEAD(registry);
9f1621ca 63
90c1618a 64static void internal_urcu_lock(void)
9f1621ca
MD
65{
66 int ret;
67
68#ifndef DISTRUST_SIGNALS_EXTREME
69 ret = pthread_mutex_lock(&urcu_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(&urcu_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 }
9f1621ca
MD
81 poll(NULL,0,10);
82 }
83#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
84}
85
90c1618a 86static void internal_urcu_unlock(void)
9f1621ca
MD
87{
88 int ret;
89
90 ret = pthread_mutex_unlock(&urcu_mutex);
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 100static 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)
105 futex(&gp_futex, FUTEX_WAIT, -1,
106 NULL, NULL, 0);
bc6c15bb
MD
107}
108
90c1618a 109static void wait_for_quiescent_state(void)
9f1621ca 110{
4d703340
MD
111 LIST_HEAD(qsreaders);
112 int wait_loops = 0;
113 struct urcu_reader *index, *tmp;
9f1621ca 114
4f8e3380 115 if (list_empty(&registry))
9f1621ca
MD
116 return;
117 /*
3395d46c 118 * Wait for each thread rcu_reader_qs_gp count to become 0.
9f1621ca 119 */
4d703340
MD
120 for (;;) {
121 wait_loops++;
122 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
123 uatomic_dec(&gp_futex);
124 /* Write futex before read reader_gp */
125 smp_mb();
126 }
127
128 list_for_each_entry_safe(index, tmp, &registry, head) {
129 if (!rcu_gp_ongoing(&index->ctr))
130 list_move(&index->head, &qsreaders);
131 }
bc6c15bb 132
4d703340
MD
133 if (list_empty(&registry)) {
134 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
135 /* Read reader_gp before write futex */
136 smp_mb();
137 uatomic_set(&gp_futex, 0);
138 }
139 break;
140 } else {
141 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
142 wait_gp();
bc6c15bb 143 } else {
9f1621ca 144#ifndef HAS_INCOHERENT_CACHES
bc6c15bb 145 cpu_relax();
9f1621ca 146#else /* #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb 147 smp_mb();
9f1621ca 148#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb
MD
149 }
150 }
9f1621ca 151 }
4d703340
MD
152 /* put back the reader list in the registry */
153 list_splice(&qsreaders, &registry);
9f1621ca
MD
154}
155
47d2f29e
MD
156/*
157 * Using a two-subphases algorithm for architectures with smaller than 64-bit
158 * long-size to ensure we do not encounter an overflow bug.
159 */
160
161#if (BITS_PER_LONG < 64)
162/*
163 * called with urcu_mutex held.
164 */
165static void switch_next_urcu_qparity(void)
166{
167 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR);
168}
169
170void synchronize_rcu(void)
171{
bc49c323
MD
172 unsigned long was_online;
173
4f8e3380 174 was_online = urcu_reader.ctr;
bc49c323 175
47d2f29e
MD
176 /* All threads should read qparity before accessing data structure
177 * where new ptr points to.
178 */
179 /* Write new ptr before changing the qparity */
180 smp_mb();
181
bc49c323
MD
182 /*
183 * Mark the writer thread offline to make sure we don't wait for
184 * our own quiescent state. This allows using synchronize_rcu() in
185 * threads registered as readers.
186 */
187 if (was_online)
4f8e3380 188 STORE_SHARED(urcu_reader.ctr, 0);
bc49c323 189
47d2f29e
MD
190 internal_urcu_lock();
191
192 switch_next_urcu_qparity(); /* 0 -> 1 */
193
194 /*
195 * Must commit qparity update to memory before waiting for parity
196 * 0 quiescent state. Failure to do so could result in the writer
197 * waiting forever while new readers are always accessing data (no
198 * progress).
199 * Ensured by STORE_SHARED and LOAD_SHARED.
200 */
201
202 /*
203 * Wait for previous parity to be empty of readers.
204 */
205 wait_for_quiescent_state(); /* Wait readers in parity 0 */
206
207 /*
208 * Must finish waiting for quiescent state for parity 0 before
209 * committing qparity update to memory. Failure to do so could result in
210 * the writer waiting forever while new readers are always accessing
211 * data (no progress).
212 * Ensured by STORE_SHARED and LOAD_SHARED.
213 */
214
215 switch_next_urcu_qparity(); /* 1 -> 0 */
216
217 /*
218 * Must commit qparity update to memory before waiting for parity
219 * 1 quiescent state. Failure to do so could result in the writer
220 * waiting forever while new readers are always accessing data (no
221 * progress).
222 * Ensured by STORE_SHARED and LOAD_SHARED.
223 */
224
225 /*
226 * Wait for previous parity to be empty of readers.
227 */
228 wait_for_quiescent_state(); /* Wait readers in parity 1 */
229
230 internal_urcu_unlock();
231
bc49c323
MD
232 /*
233 * Finish waiting for reader threads before letting the old ptr being
47d2f29e
MD
234 * freed.
235 */
bc49c323 236 if (was_online)
4f8e3380 237 _STORE_SHARED(urcu_reader.ctr, LOAD_SHARED(urcu_gp_ctr));
47d2f29e
MD
238 smp_mb();
239}
240#else /* !(BITS_PER_LONG < 64) */
9f1621ca
MD
241void synchronize_rcu(void)
242{
f0f7dbdd 243 unsigned long was_online;
ff2f67a0 244
4f8e3380 245 was_online = urcu_reader.ctr;
ff2f67a0
MD
246
247 /*
248 * Mark the writer thread offline to make sure we don't wait for
249 * our own quiescent state. This allows using synchronize_rcu() in
250 * threads registered as readers.
251 */
8f50d1ce 252 smp_mb();
ff2f67a0 253 if (was_online)
4f8e3380 254 STORE_SHARED(urcu_reader.ctr, 0);
ff2f67a0 255
4e27f058 256 internal_urcu_lock();
55570466 257 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr + RCU_GP_CTR);
9f1621ca 258 wait_for_quiescent_state();
9f1621ca 259 internal_urcu_unlock();
ff2f67a0
MD
260
261 if (was_online)
4f8e3380 262 _STORE_SHARED(urcu_reader.ctr, LOAD_SHARED(urcu_gp_ctr));
8f50d1ce 263 smp_mb();
9f1621ca 264}
47d2f29e 265#endif /* !(BITS_PER_LONG < 64) */
9f1621ca
MD
266
267/*
268 * library wrappers to be used by non-LGPL compatible source code.
269 */
270
271void rcu_read_lock(void)
272{
273 _rcu_read_lock();
274}
275
276void rcu_read_unlock(void)
277{
278 _rcu_read_unlock();
279}
280
7ac06cef
MD
281void rcu_quiescent_state(void)
282{
283 _rcu_quiescent_state();
284}
285
286void rcu_thread_offline(void)
287{
288 _rcu_thread_offline();
289}
290
291void rcu_thread_online(void)
292{
293 _rcu_thread_online();
294}
295
9f1621ca
MD
296void rcu_register_thread(void)
297{
4f8e3380
MD
298 urcu_reader.tid = pthread_self();
299 assert(urcu_reader.ctr == 0);
300
9f1621ca 301 internal_urcu_lock();
4f8e3380 302 list_add(&urcu_reader.head, &registry);
9f1621ca 303 internal_urcu_unlock();
5f373c84 304 _rcu_thread_online();
9f1621ca
MD
305}
306
307void rcu_unregister_thread(void)
308{
76f3022f
MD
309 /*
310 * We have to make the thread offline otherwise we end up dealocking
311 * with a waiting writer.
312 */
313 _rcu_thread_offline();
9f1621ca 314 internal_urcu_lock();
4f8e3380 315 list_del(&urcu_reader.head);
9f1621ca
MD
316 internal_urcu_unlock();
317}
f6d18c64
MD
318
319void rcu_exit(void)
320{
321 assert(list_empty(&registry));
322}
This page took 0.036395 seconds and 4 git commands to generate.