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