Merge branch 'master' into lfqueue-dev
[urcu.git] / urcu-qsbr.c
1 /*
2 * urcu-qsbr.c
3 *
4 * Userspace RCU QSBR library
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <poll.h>
36
37 #include "urcu/map/urcu-qsbr.h"
38
39 #define BUILD_QSBR_LIB
40 #include "urcu/static/urcu-qsbr.h"
41 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42 #include "urcu-qsbr.h"
43
44 void __attribute__((destructor)) rcu_exit(void);
45
46 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
47
48 int32_t gp_futex;
49
50 /*
51 * Global grace period counter.
52 */
53 unsigned long rcu_gp_ctr = RCU_GP_ONLINE;
54
55 /*
56 * Active attempts to check for reader Q.S. before calling futex().
57 */
58 #define RCU_QS_ACTIVE_ATTEMPTS 100
59
60 /*
61 * Written to only by each individual reader. Read by both the reader and the
62 * writers.
63 */
64 struct rcu_reader __thread rcu_reader;
65
66 #ifdef DEBUG_YIELD
67 unsigned int yield_active;
68 unsigned int __thread rand_yield;
69 #endif
70
71 static CDS_LIST_HEAD(registry);
72
73 static void mutex_lock(pthread_mutex_t *mutex)
74 {
75 int ret;
76
77 #ifndef DISTRUST_SIGNALS_EXTREME
78 ret = pthread_mutex_lock(mutex);
79 if (ret) {
80 perror("Error in pthread mutex lock");
81 exit(-1);
82 }
83 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
84 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
85 if (ret != EBUSY && ret != EINTR) {
86 printf("ret = %d, errno = %d\n", ret, errno);
87 perror("Error in pthread mutex lock");
88 exit(-1);
89 }
90 poll(NULL,0,10);
91 }
92 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
93 }
94
95 static void mutex_unlock(pthread_mutex_t *mutex)
96 {
97 int ret;
98
99 ret = pthread_mutex_unlock(mutex);
100 if (ret) {
101 perror("Error in pthread mutex unlock");
102 exit(-1);
103 }
104 }
105
106 /*
107 * synchronize_rcu() waiting. Single thread.
108 */
109 static void wait_gp(void)
110 {
111 /* Read reader_gp before read futex */
112 cmm_smp_rmb();
113 if (uatomic_read(&gp_futex) == -1)
114 futex_noasync(&gp_futex, FUTEX_WAIT, -1,
115 NULL, NULL, 0);
116 }
117
118 static void update_counter_and_wait(void)
119 {
120 CDS_LIST_HEAD(qsreaders);
121 int wait_loops = 0;
122 struct rcu_reader *index, *tmp;
123
124 #if (CAA_BITS_PER_LONG < 64)
125 /* Switch parity: 0 -> 1, 1 -> 0 */
126 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
127 #else /* !(CAA_BITS_PER_LONG < 64) */
128 /* Increment current G.P. */
129 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
130 #endif /* !(CAA_BITS_PER_LONG < 64) */
131
132 /*
133 * Must commit rcu_gp_ctr update to memory before waiting for
134 * quiescent state. Failure to do so could result in the writer
135 * waiting forever while new readers are always accessing data
136 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
137 * before load rcu_reader ctr.
138 */
139 cmm_barrier();
140
141 /*
142 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
143 * model easier to understand. It does not have a big performance impact
144 * anyway, given this is the write-side.
145 */
146 cmm_smp_mb();
147
148 /*
149 * Wait for each thread rcu_reader_qs_gp count to become 0.
150 */
151 for (;;) {
152 wait_loops++;
153 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
154 uatomic_set(&gp_futex, -1);
155 /*
156 * Write futex before write waiting (the other side
157 * reads them in the opposite order).
158 */
159 cmm_smp_wmb();
160 cds_list_for_each_entry(index, &registry, node) {
161 _CMM_STORE_SHARED(index->waiting, 1);
162 }
163 /* Write futex before read reader_gp */
164 cmm_smp_mb();
165 }
166 cds_list_for_each_entry_safe(index, tmp, &registry, node) {
167 if (!rcu_gp_ongoing(&index->ctr))
168 cds_list_move(&index->node, &qsreaders);
169 }
170
171 if (cds_list_empty(&registry)) {
172 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
173 /* Read reader_gp before write futex */
174 cmm_smp_mb();
175 uatomic_set(&gp_futex, 0);
176 }
177 break;
178 } else {
179 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
180 wait_gp();
181 } else {
182 #ifndef HAS_INCOHERENT_CACHES
183 caa_cpu_relax();
184 #else /* #ifndef HAS_INCOHERENT_CACHES */
185 cmm_smp_mb();
186 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
187 }
188 }
189 }
190 /* put back the reader list in the registry */
191 cds_list_splice(&qsreaders, &registry);
192 }
193
194 /*
195 * Using a two-subphases algorithm for architectures with smaller than 64-bit
196 * long-size to ensure we do not encounter an overflow bug.
197 */
198
199 #if (CAA_BITS_PER_LONG < 64)
200 void synchronize_rcu(void)
201 {
202 unsigned long was_online;
203
204 was_online = rcu_reader.ctr;
205
206 /* All threads should read qparity before accessing data structure
207 * where new ptr points to.
208 */
209 /* Write new ptr before changing the qparity */
210 cmm_smp_mb();
211
212 /*
213 * Mark the writer thread offline to make sure we don't wait for
214 * our own quiescent state. This allows using synchronize_rcu()
215 * in threads registered as readers.
216 */
217 if (was_online)
218 CMM_STORE_SHARED(rcu_reader.ctr, 0);
219
220 mutex_lock(&rcu_gp_lock);
221
222 if (cds_list_empty(&registry))
223 goto out;
224
225 /*
226 * Wait for previous parity to be empty of readers.
227 */
228 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
229
230 /*
231 * Must finish waiting for quiescent state for parity 0 before
232 * committing next rcu_gp_ctr update to memory. Failure to
233 * do so could result in the writer waiting forever while new
234 * readers are always accessing data (no progress). Enforce
235 * compiler-order of load rcu_reader ctr before store to
236 * rcu_gp_ctr.
237 */
238 cmm_barrier();
239
240 /*
241 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
242 * model easier to understand. It does not have a big performance impact
243 * anyway, given this is the write-side.
244 */
245 cmm_smp_mb();
246
247 /*
248 * Wait for previous parity to be empty of readers.
249 */
250 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
251 out:
252 mutex_unlock(&rcu_gp_lock);
253
254 /*
255 * Finish waiting for reader threads before letting the old ptr being
256 * freed.
257 */
258 if (was_online)
259 _CMM_STORE_SHARED(rcu_reader.ctr,
260 CMM_LOAD_SHARED(rcu_gp_ctr));
261 cmm_smp_mb();
262 }
263 #else /* !(CAA_BITS_PER_LONG < 64) */
264 void synchronize_rcu(void)
265 {
266 unsigned long was_online;
267
268 was_online = rcu_reader.ctr;
269
270 /*
271 * Mark the writer thread offline to make sure we don't wait for
272 * our own quiescent state. This allows using synchronize_rcu()
273 * in threads registered as readers.
274 */
275 cmm_smp_mb();
276 if (was_online)
277 CMM_STORE_SHARED(rcu_reader.ctr, 0);
278
279 mutex_lock(&rcu_gp_lock);
280 if (cds_list_empty(&registry))
281 goto out;
282 update_counter_and_wait();
283 out:
284 mutex_unlock(&rcu_gp_lock);
285
286 if (was_online)
287 _CMM_STORE_SHARED(rcu_reader.ctr,
288 CMM_LOAD_SHARED(rcu_gp_ctr));
289 cmm_smp_mb();
290 }
291 #endif /* !(CAA_BITS_PER_LONG < 64) */
292
293 /*
294 * library wrappers to be used by non-LGPL compatible source code.
295 */
296
297 void rcu_read_lock(void)
298 {
299 _rcu_read_lock();
300 }
301
302 void rcu_read_unlock(void)
303 {
304 _rcu_read_unlock();
305 }
306
307 void rcu_quiescent_state(void)
308 {
309 _rcu_quiescent_state();
310 }
311
312 void rcu_thread_offline(void)
313 {
314 _rcu_thread_offline();
315 }
316
317 void rcu_thread_online(void)
318 {
319 _rcu_thread_online();
320 }
321
322 void rcu_register_thread(void)
323 {
324 rcu_reader.tid = pthread_self();
325 assert(rcu_reader.ctr == 0);
326
327 mutex_lock(&rcu_gp_lock);
328 cds_list_add(&rcu_reader.node, &registry);
329 mutex_unlock(&rcu_gp_lock);
330 _rcu_thread_online();
331 }
332
333 void rcu_unregister_thread(void)
334 {
335 /*
336 * We have to make the thread offline otherwise we end up dealocking
337 * with a waiting writer.
338 */
339 _rcu_thread_offline();
340 mutex_lock(&rcu_gp_lock);
341 cds_list_del(&rcu_reader.node);
342 mutex_unlock(&rcu_gp_lock);
343 }
344
345 void rcu_exit(void)
346 {
347 assert(cds_list_empty(&registry));
348 }
349
350 #include "urcu-call-rcu-impl.h"
351 #include "urcu-defer-impl.h"
This page took 0.036061 seconds and 5 git commands to generate.