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