QSBR: Implement 2-phase grace period for 32-bit arch
[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
9f1621ca
MD
40pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42/*
43 * Global grace period counter.
44 */
ac258107 45unsigned long urcu_gp_ctr = RCU_GP_ONLINE;
9f1621ca
MD
46
47/*
48 * Written to only by each individual reader. Read by both the reader and the
49 * writers.
50 */
f0f7dbdd 51unsigned long __thread rcu_reader_qs_gp;
9f1621ca
MD
52
53/* Thread IDs of registered readers */
54#define INIT_NUM_THREADS 4
55
56struct reader_registry {
57 pthread_t tid;
f0f7dbdd 58 unsigned long *rcu_reader_qs_gp;
9f1621ca
MD
59};
60
61#ifdef DEBUG_YIELD
62unsigned int yield_active;
63unsigned int __thread rand_yield;
64#endif
65
66static struct reader_registry *registry;
9f1621ca
MD
67static int num_readers, alloc_readers;
68
90c1618a 69static void internal_urcu_lock(void)
9f1621ca
MD
70{
71 int ret;
72
73#ifndef DISTRUST_SIGNALS_EXTREME
74 ret = pthread_mutex_lock(&urcu_mutex);
75 if (ret) {
76 perror("Error in pthread mutex lock");
77 exit(-1);
78 }
79#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
80 while ((ret = pthread_mutex_trylock(&urcu_mutex)) != 0) {
81 if (ret != EBUSY && ret != EINTR) {
82 printf("ret = %d, errno = %d\n", ret, errno);
83 perror("Error in pthread mutex lock");
84 exit(-1);
85 }
9f1621ca
MD
86 poll(NULL,0,10);
87 }
88#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
89}
90
90c1618a 91static void internal_urcu_unlock(void)
9f1621ca
MD
92{
93 int ret;
94
95 ret = pthread_mutex_unlock(&urcu_mutex);
96 if (ret) {
97 perror("Error in pthread mutex unlock");
98 exit(-1);
99 }
100}
101
90c1618a 102static void wait_for_quiescent_state(void)
9f1621ca
MD
103{
104 struct reader_registry *index;
105
106 if (!registry)
107 return;
108 /*
3395d46c 109 * Wait for each thread rcu_reader_qs_gp count to become 0.
9f1621ca
MD
110 */
111 for (index = registry; index < registry + num_readers; index++) {
112#ifndef HAS_INCOHERENT_CACHES
4e560c17 113 while (rcu_gp_ongoing(index->rcu_reader_qs_gp))
9f1621ca
MD
114 cpu_relax();
115#else /* #ifndef HAS_INCOHERENT_CACHES */
47d2f29e
MD
116 while (rcu_gp_ongoing(index->rcu_reader_qs_gp))
117 smp_mb();
9f1621ca
MD
118#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
119 }
120}
121
47d2f29e
MD
122/*
123 * Using a two-subphases algorithm for architectures with smaller than 64-bit
124 * long-size to ensure we do not encounter an overflow bug.
125 */
126
127#if (BITS_PER_LONG < 64)
128/*
129 * called with urcu_mutex held.
130 */
131static void switch_next_urcu_qparity(void)
132{
133 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR);
134}
135
136void synchronize_rcu(void)
137{
138 /* All threads should read qparity before accessing data structure
139 * where new ptr points to.
140 */
141 /* Write new ptr before changing the qparity */
142 smp_mb();
143
144 internal_urcu_lock();
145
146 switch_next_urcu_qparity(); /* 0 -> 1 */
147
148 /*
149 * Must commit qparity update to memory before waiting for parity
150 * 0 quiescent state. Failure to do so could result in the writer
151 * waiting forever while new readers are always accessing data (no
152 * progress).
153 * Ensured by STORE_SHARED and LOAD_SHARED.
154 */
155
156 /*
157 * Wait for previous parity to be empty of readers.
158 */
159 wait_for_quiescent_state(); /* Wait readers in parity 0 */
160
161 /*
162 * Must finish waiting for quiescent state for parity 0 before
163 * committing qparity update to memory. Failure to do so could result in
164 * the writer waiting forever while new readers are always accessing
165 * data (no progress).
166 * Ensured by STORE_SHARED and LOAD_SHARED.
167 */
168
169 switch_next_urcu_qparity(); /* 1 -> 0 */
170
171 /*
172 * Must commit qparity update to memory before waiting for parity
173 * 1 quiescent state. Failure to do so could result in the writer
174 * waiting forever while new readers are always accessing data (no
175 * progress).
176 * Ensured by STORE_SHARED and LOAD_SHARED.
177 */
178
179 /*
180 * Wait for previous parity to be empty of readers.
181 */
182 wait_for_quiescent_state(); /* Wait readers in parity 1 */
183
184 internal_urcu_unlock();
185
186 /* Finish waiting for reader threads before letting the old ptr being
187 * freed.
188 */
189 smp_mb();
190}
191#else /* !(BITS_PER_LONG < 64) */
9f1621ca
MD
192void synchronize_rcu(void)
193{
f0f7dbdd 194 unsigned long was_online;
ff2f67a0 195
ab179a17 196 was_online = rcu_reader_qs_gp;
ff2f67a0
MD
197
198 /*
199 * Mark the writer thread offline to make sure we don't wait for
200 * our own quiescent state. This allows using synchronize_rcu() in
201 * threads registered as readers.
202 */
8f50d1ce 203 smp_mb();
ff2f67a0 204 if (was_online)
8f50d1ce 205 STORE_SHARED(rcu_reader_qs_gp, 0);
ff2f67a0 206
4e27f058 207 internal_urcu_lock();
55570466 208 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr + RCU_GP_CTR);
9f1621ca 209 wait_for_quiescent_state();
9f1621ca 210 internal_urcu_unlock();
ff2f67a0
MD
211
212 if (was_online)
ab179a17 213 _STORE_SHARED(rcu_reader_qs_gp, LOAD_SHARED(urcu_gp_ctr));
8f50d1ce 214 smp_mb();
9f1621ca 215}
47d2f29e 216#endif /* !(BITS_PER_LONG < 64) */
9f1621ca
MD
217
218/*
219 * library wrappers to be used by non-LGPL compatible source code.
220 */
221
222void rcu_read_lock(void)
223{
224 _rcu_read_lock();
225}
226
227void rcu_read_unlock(void)
228{
229 _rcu_read_unlock();
230}
231
232void *rcu_dereference(void *p)
233{
234 return _rcu_dereference(p);
235}
236
237void *rcu_assign_pointer_sym(void **p, void *v)
238{
239 wmb();
240 return STORE_SHARED(p, v);
241}
242
243void *rcu_xchg_pointer_sym(void **p, void *v)
244{
245 wmb();
246 return xchg(p, v);
247}
248
249void *rcu_publish_content_sym(void **p, void *v)
250{
251 void *oldptr;
252
253 oldptr = _rcu_xchg_pointer(p, v);
254 synchronize_rcu();
255 return oldptr;
256}
257
7ac06cef
MD
258void rcu_quiescent_state(void)
259{
260 _rcu_quiescent_state();
261}
262
263void rcu_thread_offline(void)
264{
265 _rcu_thread_offline();
266}
267
268void rcu_thread_online(void)
269{
270 _rcu_thread_online();
271}
272
9f1621ca
MD
273static void rcu_add_reader(pthread_t id)
274{
275 struct reader_registry *oldarray;
276
277 if (!registry) {
278 alloc_readers = INIT_NUM_THREADS;
279 num_readers = 0;
280 registry =
281 malloc(sizeof(struct reader_registry) * alloc_readers);
282 }
283 if (alloc_readers < num_readers + 1) {
284 oldarray = registry;
285 registry = malloc(sizeof(struct reader_registry)
286 * (alloc_readers << 1));
287 memcpy(registry, oldarray,
288 sizeof(struct reader_registry) * alloc_readers);
289 alloc_readers <<= 1;
290 free(oldarray);
291 }
292 registry[num_readers].tid = id;
293 /* reference to the TLS of _this_ reader thread. */
3395d46c 294 registry[num_readers].rcu_reader_qs_gp = &rcu_reader_qs_gp;
9f1621ca
MD
295 num_readers++;
296}
297
298/*
299 * Never shrink (implementation limitation).
300 * This is O(nb threads). Eventually use a hash table.
301 */
302static void rcu_remove_reader(pthread_t id)
303{
304 struct reader_registry *index;
305
306 assert(registry != NULL);
307 for (index = registry; index < registry + num_readers; index++) {
308 if (pthread_equal(index->tid, id)) {
309 memcpy(index, &registry[num_readers - 1],
310 sizeof(struct reader_registry));
311 registry[num_readers - 1].tid = 0;
3395d46c 312 registry[num_readers - 1].rcu_reader_qs_gp = NULL;
9f1621ca
MD
313 num_readers--;
314 return;
315 }
316 }
317 /* Hrm not found, forgot to register ? */
318 assert(0);
319}
320
321void rcu_register_thread(void)
322{
323 internal_urcu_lock();
9f1621ca
MD
324 rcu_add_reader(pthread_self());
325 internal_urcu_unlock();
5f373c84 326 _rcu_thread_online();
9f1621ca
MD
327}
328
329void rcu_unregister_thread(void)
330{
76f3022f
MD
331 /*
332 * We have to make the thread offline otherwise we end up dealocking
333 * with a waiting writer.
334 */
335 _rcu_thread_offline();
9f1621ca
MD
336 internal_urcu_lock();
337 rcu_remove_reader(pthread_self());
338 internal_urcu_unlock();
339}
This page took 0.035232 seconds and 4 git commands to generate.