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