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