Only make the threads for which we are waiting call sched_yield()
[userspace-rcu.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
81d1f1f5 40static pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
9f1621ca
MD
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 */
8b25e300 51struct urcu_reader_status __thread urcu_reader_status;
9f1621ca
MD
52
53/* Thread IDs of registered readers */
54#define INIT_NUM_THREADS 4
55
56struct reader_registry {
57 pthread_t tid;
8b25e300 58 struct urcu_reader_status *urcu_reader_status;
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
bc6c15bb
MD
102/*
103 * synchronize_rcu() waiting. Single thread.
104 */
90c1618a 105static void wait_for_quiescent_state(void)
9f1621ca
MD
106{
107 struct reader_registry *index;
108
109 if (!registry)
110 return;
111 /*
8b25e300 112 * Wait for each thread rcu_reader qs_gp count to become 0.
9f1621ca
MD
113 */
114 for (index = registry; index < registry + num_readers; index++) {
bc6c15bb
MD
115 int wait_loops = 0;
116
8b25e300
MD
117 index->urcu_reader_status->gp_waiting = 1;
118 while (rcu_gp_ongoing(&index->urcu_reader_status->qs_gp)) {
bc6c15bb 119 if (wait_loops++ == RCU_QS_ACTIVE_ATTEMPTS) {
ae62b5e8 120 sched_yield(); /* ideally sched_yield_to() */
bc6c15bb 121 } else {
9f1621ca 122#ifndef HAS_INCOHERENT_CACHES
bc6c15bb 123 cpu_relax();
9f1621ca 124#else /* #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb 125 smp_mb();
9f1621ca 126#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb
MD
127 }
128 }
8b25e300 129 index->urcu_reader_status->gp_waiting = 0;
9f1621ca
MD
130 }
131}
132
47d2f29e
MD
133/*
134 * Using a two-subphases algorithm for architectures with smaller than 64-bit
135 * long-size to ensure we do not encounter an overflow bug.
136 */
137
138#if (BITS_PER_LONG < 64)
139/*
140 * called with urcu_mutex held.
141 */
142static void switch_next_urcu_qparity(void)
143{
144 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR);
145}
146
147void synchronize_rcu(void)
148{
bc49c323
MD
149 unsigned long was_online;
150
8b25e300 151 was_online = urcu_reader_status.qs_gp;
bc49c323 152
47d2f29e
MD
153 /* All threads should read qparity before accessing data structure
154 * where new ptr points to.
155 */
156 /* Write new ptr before changing the qparity */
157 smp_mb();
158
bc49c323
MD
159 /*
160 * Mark the writer thread offline to make sure we don't wait for
161 * our own quiescent state. This allows using synchronize_rcu() in
162 * threads registered as readers.
163 */
164 if (was_online)
8b25e300 165 STORE_SHARED(urcu_reader_status.qs_gp, 0);
bc49c323 166
47d2f29e
MD
167 internal_urcu_lock();
168
ae62b5e8
MD
169 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
170
47d2f29e
MD
171 switch_next_urcu_qparity(); /* 0 -> 1 */
172
173 /*
174 * Must commit qparity update to memory before waiting for parity
175 * 0 quiescent state. Failure to do so could result in the writer
176 * waiting forever while new readers are always accessing data (no
177 * progress).
178 * Ensured by STORE_SHARED and LOAD_SHARED.
179 */
180
181 /*
182 * Wait for previous parity to be empty of readers.
183 */
184 wait_for_quiescent_state(); /* Wait readers in parity 0 */
185
186 /*
187 * Must finish waiting for quiescent state for parity 0 before
188 * committing qparity update to memory. Failure to do so could result in
189 * the writer waiting forever while new readers are always accessing
190 * data (no progress).
191 * Ensured by STORE_SHARED and LOAD_SHARED.
192 */
193
194 switch_next_urcu_qparity(); /* 1 -> 0 */
195
196 /*
197 * Must commit qparity update to memory before waiting for parity
198 * 1 quiescent state. Failure to do so could result in the writer
199 * waiting forever while new readers are always accessing data (no
200 * progress).
201 * Ensured by STORE_SHARED and LOAD_SHARED.
202 */
203
204 /*
205 * Wait for previous parity to be empty of readers.
206 */
207 wait_for_quiescent_state(); /* Wait readers in parity 1 */
208
ae62b5e8
MD
209 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
210
47d2f29e
MD
211 internal_urcu_unlock();
212
bc49c323
MD
213 /*
214 * Finish waiting for reader threads before letting the old ptr being
47d2f29e
MD
215 * freed.
216 */
bc49c323 217 if (was_online)
8b25e300
MD
218 _STORE_SHARED(urcu_reader_status.qs_gp,
219 LOAD_SHARED(urcu_gp_ctr));
47d2f29e
MD
220 smp_mb();
221}
222#else /* !(BITS_PER_LONG < 64) */
9f1621ca
MD
223void synchronize_rcu(void)
224{
f0f7dbdd 225 unsigned long was_online;
ff2f67a0 226
8b25e300 227 was_online = urcu_reader_status.qs_gp;
ff2f67a0
MD
228
229 /*
230 * Mark the writer thread offline to make sure we don't wait for
231 * our own quiescent state. This allows using synchronize_rcu() in
232 * threads registered as readers.
233 */
8f50d1ce 234 smp_mb();
ff2f67a0 235 if (was_online)
8b25e300 236 STORE_SHARED(urcu_reader_status.qs_gp, 0);
ff2f67a0 237
4e27f058 238 internal_urcu_lock();
ae62b5e8 239 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
55570466 240 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr + RCU_GP_CTR);
9f1621ca 241 wait_for_quiescent_state();
ae62b5e8 242 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
9f1621ca 243 internal_urcu_unlock();
ff2f67a0
MD
244
245 if (was_online)
8b25e300
MD
246 _STORE_SHARED(urcu_reader_status.qs_gp,
247 LOAD_SHARED(urcu_gp_ctr));
8f50d1ce 248 smp_mb();
9f1621ca 249}
47d2f29e 250#endif /* !(BITS_PER_LONG < 64) */
9f1621ca
MD
251
252/*
253 * library wrappers to be used by non-LGPL compatible source code.
254 */
255
256void rcu_read_lock(void)
257{
258 _rcu_read_lock();
259}
260
261void rcu_read_unlock(void)
262{
263 _rcu_read_unlock();
264}
265
266void *rcu_dereference(void *p)
267{
268 return _rcu_dereference(p);
269}
270
271void *rcu_assign_pointer_sym(void **p, void *v)
272{
273 wmb();
274 return STORE_SHARED(p, v);
275}
276
4d1ce26f
MD
277void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new)
278{
279 wmb();
280 return cmpxchg(p, old, _new);
281}
282
9f1621ca
MD
283void *rcu_xchg_pointer_sym(void **p, void *v)
284{
285 wmb();
286 return xchg(p, v);
287}
288
289void *rcu_publish_content_sym(void **p, void *v)
290{
291 void *oldptr;
292
293 oldptr = _rcu_xchg_pointer(p, v);
294 synchronize_rcu();
295 return oldptr;
296}
297
7ac06cef
MD
298void rcu_quiescent_state(void)
299{
300 _rcu_quiescent_state();
301}
302
303void rcu_thread_offline(void)
304{
305 _rcu_thread_offline();
306}
307
308void rcu_thread_online(void)
309{
310 _rcu_thread_online();
311}
312
9f1621ca
MD
313static void rcu_add_reader(pthread_t id)
314{
315 struct reader_registry *oldarray;
316
317 if (!registry) {
318 alloc_readers = INIT_NUM_THREADS;
319 num_readers = 0;
320 registry =
321 malloc(sizeof(struct reader_registry) * alloc_readers);
322 }
323 if (alloc_readers < num_readers + 1) {
324 oldarray = registry;
325 registry = malloc(sizeof(struct reader_registry)
326 * (alloc_readers << 1));
327 memcpy(registry, oldarray,
328 sizeof(struct reader_registry) * alloc_readers);
329 alloc_readers <<= 1;
330 free(oldarray);
331 }
332 registry[num_readers].tid = id;
333 /* reference to the TLS of _this_ reader thread. */
8b25e300 334 registry[num_readers].urcu_reader_status = &urcu_reader_status;
9f1621ca
MD
335 num_readers++;
336}
337
338/*
339 * Never shrink (implementation limitation).
340 * This is O(nb threads). Eventually use a hash table.
341 */
342static void rcu_remove_reader(pthread_t id)
343{
344 struct reader_registry *index;
345
346 assert(registry != NULL);
347 for (index = registry; index < registry + num_readers; index++) {
348 if (pthread_equal(index->tid, id)) {
349 memcpy(index, &registry[num_readers - 1],
350 sizeof(struct reader_registry));
351 registry[num_readers - 1].tid = 0;
8b25e300 352 registry[num_readers - 1].urcu_reader_status = NULL;
9f1621ca
MD
353 num_readers--;
354 return;
355 }
356 }
357 /* Hrm not found, forgot to register ? */
358 assert(0);
359}
360
361void rcu_register_thread(void)
362{
363 internal_urcu_lock();
9f1621ca
MD
364 rcu_add_reader(pthread_self());
365 internal_urcu_unlock();
5f373c84 366 _rcu_thread_online();
9f1621ca
MD
367}
368
369void rcu_unregister_thread(void)
370{
76f3022f
MD
371 /*
372 * We have to make the thread offline otherwise we end up dealocking
373 * with a waiting writer.
374 */
375 _rcu_thread_offline();
9f1621ca
MD
376 internal_urcu_lock();
377 rcu_remove_reader(pthread_self());
378 internal_urcu_unlock();
379}
This page took 0.038535 seconds and 4 git commands to generate.