urcu: Add extra "engineering safety factor" memory barrier in update_counter_and_wait()
[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
6abb4bd5 42static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
9f1621ca 43
bc6c15bb
MD
44int gp_futex;
45
9f1621ca
MD
46/*
47 * Global grace period counter.
48 */
02be5561 49unsigned long rcu_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 */
02be5561 55struct rcu_reader __thread rcu_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
6abb4bd5 64static void mutex_lock(pthread_mutex_t *mutex)
9f1621ca
MD
65{
66 int ret;
67
68#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 69 ret = pthread_mutex_lock(mutex);
9f1621ca
MD
70 if (ret) {
71 perror("Error in pthread mutex lock");
72 exit(-1);
73 }
74#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 75 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
9f1621ca
MD
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
6abb4bd5 86static void mutex_unlock(pthread_mutex_t *mutex)
9f1621ca
MD
87{
88 int ret;
89
6abb4bd5 90 ret = pthread_mutex_unlock(mutex);
9f1621ca
MD
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)
0854ccff 105 futex_noasync(&gp_futex, FUTEX_WAIT, -1,
4d703340 106 NULL, NULL, 0);
bc6c15bb
MD
107}
108
2dfb8b5e 109static void update_counter_and_wait(void)
9f1621ca 110{
4d703340
MD
111 LIST_HEAD(qsreaders);
112 int wait_loops = 0;
02be5561 113 struct rcu_reader *index, *tmp;
9f1621ca 114
2dfb8b5e 115#if (BITS_PER_LONG < 64)
32c15e4e 116 /* Switch parity: 0 -> 1, 1 -> 0 */
2dfb8b5e
MD
117 STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
118#else /* !(BITS_PER_LONG < 64) */
119 /* Increment current G.P. */
120 STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
121#endif /* !(BITS_PER_LONG < 64) */
122
7a5a38f5
MD
123 /*
124 * Enforce compiler-order of store to rcu_gp_ctr before before
125 * load rcu_reader ctr.
126 * This ensures synchronize_rcu() cannot be starved by readers.
935b11ff
MD
127 *
128 * Adding a smp_mb() which is _not_ formally required, but makes the
129 * model easier to understand. It does not have a big performance impact
130 * anyway, given this is the write-side.
7a5a38f5 131 */
935b11ff 132 smp_mb();
7a5a38f5 133
9f1621ca 134 /*
3395d46c 135 * Wait for each thread rcu_reader_qs_gp count to become 0.
9f1621ca 136 */
4d703340
MD
137 for (;;) {
138 wait_loops++;
139 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
140 uatomic_dec(&gp_futex);
141 /* Write futex before read reader_gp */
142 smp_mb();
143 }
144
145 list_for_each_entry_safe(index, tmp, &registry, head) {
146 if (!rcu_gp_ongoing(&index->ctr))
147 list_move(&index->head, &qsreaders);
148 }
bc6c15bb 149
4d703340
MD
150 if (list_empty(&registry)) {
151 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
152 /* Read reader_gp before write futex */
153 smp_mb();
154 uatomic_set(&gp_futex, 0);
155 }
156 break;
157 } else {
158 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
159 wait_gp();
bc6c15bb 160 } else {
9f1621ca 161#ifndef HAS_INCOHERENT_CACHES
bc6c15bb 162 cpu_relax();
9f1621ca 163#else /* #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb 164 smp_mb();
9f1621ca 165#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb
MD
166 }
167 }
9f1621ca 168 }
4d703340
MD
169 /* put back the reader list in the registry */
170 list_splice(&qsreaders, &registry);
9f1621ca
MD
171}
172
47d2f29e
MD
173/*
174 * Using a two-subphases algorithm for architectures with smaller than 64-bit
175 * long-size to ensure we do not encounter an overflow bug.
176 */
177
178#if (BITS_PER_LONG < 64)
47d2f29e
MD
179void synchronize_rcu(void)
180{
bc49c323
MD
181 unsigned long was_online;
182
02be5561 183 was_online = rcu_reader.ctr;
bc49c323 184
47d2f29e
MD
185 /* All threads should read qparity before accessing data structure
186 * where new ptr points to.
187 */
188 /* Write new ptr before changing the qparity */
189 smp_mb();
190
bc49c323
MD
191 /*
192 * Mark the writer thread offline to make sure we don't wait for
193 * our own quiescent state. This allows using synchronize_rcu() in
194 * threads registered as readers.
195 */
196 if (was_online)
02be5561 197 STORE_SHARED(rcu_reader.ctr, 0);
bc49c323 198
6abb4bd5 199 mutex_lock(&rcu_gp_lock);
47d2f29e 200
2dfb8b5e
MD
201 if (list_empty(&registry))
202 goto out;
47d2f29e
MD
203
204 /*
205 * Wait for previous parity to be empty of readers.
206 */
2dfb8b5e 207 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
47d2f29e
MD
208
209 /*
210 * Must finish waiting for quiescent state for parity 0 before
211 * committing qparity update to memory. Failure to do so could result in
212 * the writer waiting forever while new readers are always accessing
213 * data (no progress).
214 * Ensured by STORE_SHARED and LOAD_SHARED.
215 */
216
47d2f29e 217 /*
2dfb8b5e
MD
218 * Adding a smp_mb() which is _not_ formally required, but makes the
219 * model easier to understand. It does not have a big performance impact
220 * anyway, given this is the write-side.
47d2f29e 221 */
2dfb8b5e 222 smp_mb();
47d2f29e
MD
223
224 /*
225 * Wait for previous parity to be empty of readers.
226 */
2dfb8b5e
MD
227 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
228out:
6abb4bd5 229 mutex_unlock(&rcu_gp_lock);
47d2f29e 230
bc49c323
MD
231 /*
232 * Finish waiting for reader threads before letting the old ptr being
47d2f29e
MD
233 * freed.
234 */
bc49c323 235 if (was_online)
02be5561 236 _STORE_SHARED(rcu_reader.ctr, LOAD_SHARED(rcu_gp_ctr));
47d2f29e
MD
237 smp_mb();
238}
239#else /* !(BITS_PER_LONG < 64) */
9f1621ca
MD
240void synchronize_rcu(void)
241{
f0f7dbdd 242 unsigned long was_online;
ff2f67a0 243
02be5561 244 was_online = rcu_reader.ctr;
ff2f67a0
MD
245
246 /*
247 * Mark the writer thread offline to make sure we don't wait for
248 * our own quiescent state. This allows using synchronize_rcu() in
249 * threads registered as readers.
250 */
8f50d1ce 251 smp_mb();
ff2f67a0 252 if (was_online)
02be5561 253 STORE_SHARED(rcu_reader.ctr, 0);
ff2f67a0 254
6abb4bd5 255 mutex_lock(&rcu_gp_lock);
2dfb8b5e
MD
256 if (list_empty(&registry))
257 goto out;
258 update_counter_and_wait();
259out:
6abb4bd5 260 mutex_unlock(&rcu_gp_lock);
ff2f67a0
MD
261
262 if (was_online)
02be5561 263 _STORE_SHARED(rcu_reader.ctr, LOAD_SHARED(rcu_gp_ctr));
8f50d1ce 264 smp_mb();
9f1621ca 265}
47d2f29e 266#endif /* !(BITS_PER_LONG < 64) */
9f1621ca
MD
267
268/*
269 * library wrappers to be used by non-LGPL compatible source code.
270 */
271
272void rcu_read_lock(void)
273{
274 _rcu_read_lock();
275}
276
277void rcu_read_unlock(void)
278{
279 _rcu_read_unlock();
280}
281
7ac06cef
MD
282void rcu_quiescent_state(void)
283{
284 _rcu_quiescent_state();
285}
286
287void rcu_thread_offline(void)
288{
289 _rcu_thread_offline();
290}
291
292void rcu_thread_online(void)
293{
294 _rcu_thread_online();
295}
296
9f1621ca
MD
297void rcu_register_thread(void)
298{
02be5561
MD
299 rcu_reader.tid = pthread_self();
300 assert(rcu_reader.ctr == 0);
4f8e3380 301
6abb4bd5 302 mutex_lock(&rcu_gp_lock);
02be5561 303 list_add(&rcu_reader.head, &registry);
6abb4bd5 304 mutex_unlock(&rcu_gp_lock);
5f373c84 305 _rcu_thread_online();
9f1621ca
MD
306}
307
308void rcu_unregister_thread(void)
309{
76f3022f
MD
310 /*
311 * We have to make the thread offline otherwise we end up dealocking
312 * with a waiting writer.
313 */
314 _rcu_thread_offline();
6abb4bd5 315 mutex_lock(&rcu_gp_lock);
02be5561 316 list_del(&rcu_reader.head);
6abb4bd5 317 mutex_unlock(&rcu_gp_lock);
9f1621ca 318}
f6d18c64
MD
319
320void rcu_exit(void)
321{
322 assert(list_empty(&registry));
323}
This page took 0.038711 seconds and 4 git commands to generate.