urcu (mb/signal): list move
[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(struct urcu_reader *index)
101 {
102 uatomic_dec(&gp_futex);
103 smp_mb(); /* Write futex before read reader_gp */
104 if (!rcu_gp_ongoing(&index->ctr)) {
105 /* Read reader_gp before write futex */
106 smp_mb();
107 /* Callbacks are queued, don't wait. */
108 uatomic_set(&gp_futex, 0);
109 } else {
110 /* Read reader_gp before read futex */
111 smp_rmb();
112 if (uatomic_read(&gp_futex) == -1)
113 futex(&gp_futex, FUTEX_WAIT, -1,
114 NULL, NULL, 0);
115 }
116 }
117
118 static void wait_for_quiescent_state(void)
119 {
120 struct urcu_reader *index;
121
122 if (list_empty(&registry))
123 return;
124 /*
125 * Wait for each thread rcu_reader_qs_gp count to become 0.
126 */
127 list_for_each_entry(index, &registry, head) {
128 int wait_loops = 0;
129
130 while (rcu_gp_ongoing(&index->ctr)) {
131 if (wait_loops++ == RCU_QS_ACTIVE_ATTEMPTS) {
132 wait_gp(index);
133 } else {
134 #ifndef HAS_INCOHERENT_CACHES
135 cpu_relax();
136 #else /* #ifndef HAS_INCOHERENT_CACHES */
137 smp_mb();
138 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
139 }
140 }
141 }
142 }
143
144 /*
145 * Using a two-subphases algorithm for architectures with smaller than 64-bit
146 * long-size to ensure we do not encounter an overflow bug.
147 */
148
149 #if (BITS_PER_LONG < 64)
150 /*
151 * called with urcu_mutex held.
152 */
153 static void switch_next_urcu_qparity(void)
154 {
155 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR);
156 }
157
158 void synchronize_rcu(void)
159 {
160 unsigned long was_online;
161
162 was_online = urcu_reader.ctr;
163
164 /* All threads should read qparity before accessing data structure
165 * where new ptr points to.
166 */
167 /* Write new ptr before changing the qparity */
168 smp_mb();
169
170 /*
171 * Mark the writer thread offline to make sure we don't wait for
172 * our own quiescent state. This allows using synchronize_rcu() in
173 * threads registered as readers.
174 */
175 if (was_online)
176 STORE_SHARED(urcu_reader.ctr, 0);
177
178 internal_urcu_lock();
179
180 switch_next_urcu_qparity(); /* 0 -> 1 */
181
182 /*
183 * Must commit qparity update to memory before waiting for parity
184 * 0 quiescent state. Failure to do so could result in the writer
185 * waiting forever while new readers are always accessing data (no
186 * progress).
187 * Ensured by STORE_SHARED and LOAD_SHARED.
188 */
189
190 /*
191 * Wait for previous parity to be empty of readers.
192 */
193 wait_for_quiescent_state(); /* Wait readers in parity 0 */
194
195 /*
196 * Must finish waiting for quiescent state for parity 0 before
197 * committing qparity update to memory. Failure to do so could result in
198 * the writer waiting forever while new readers are always accessing
199 * data (no progress).
200 * Ensured by STORE_SHARED and LOAD_SHARED.
201 */
202
203 switch_next_urcu_qparity(); /* 1 -> 0 */
204
205 /*
206 * Must commit qparity update to memory before waiting for parity
207 * 1 quiescent state. Failure to do so could result in the writer
208 * waiting forever while new readers are always accessing data (no
209 * progress).
210 * Ensured by STORE_SHARED and LOAD_SHARED.
211 */
212
213 /*
214 * Wait for previous parity to be empty of readers.
215 */
216 wait_for_quiescent_state(); /* Wait readers in parity 1 */
217
218 internal_urcu_unlock();
219
220 /*
221 * Finish waiting for reader threads before letting the old ptr being
222 * freed.
223 */
224 if (was_online)
225 _STORE_SHARED(urcu_reader.ctr, LOAD_SHARED(urcu_gp_ctr));
226 smp_mb();
227 }
228 #else /* !(BITS_PER_LONG < 64) */
229 void synchronize_rcu(void)
230 {
231 unsigned long was_online;
232
233 was_online = urcu_reader.ctr;
234
235 /*
236 * Mark the writer thread offline to make sure we don't wait for
237 * our own quiescent state. This allows using synchronize_rcu() in
238 * threads registered as readers.
239 */
240 smp_mb();
241 if (was_online)
242 STORE_SHARED(urcu_reader.ctr, 0);
243
244 internal_urcu_lock();
245 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr + RCU_GP_CTR);
246 wait_for_quiescent_state();
247 internal_urcu_unlock();
248
249 if (was_online)
250 _STORE_SHARED(urcu_reader.ctr, LOAD_SHARED(urcu_gp_ctr));
251 smp_mb();
252 }
253 #endif /* !(BITS_PER_LONG < 64) */
254
255 /*
256 * library wrappers to be used by non-LGPL compatible source code.
257 */
258
259 void rcu_read_lock(void)
260 {
261 _rcu_read_lock();
262 }
263
264 void rcu_read_unlock(void)
265 {
266 _rcu_read_unlock();
267 }
268
269 void *rcu_dereference(void *p)
270 {
271 return _rcu_dereference(p);
272 }
273
274 void *rcu_assign_pointer_sym(void **p, void *v)
275 {
276 wmb();
277 return STORE_SHARED(p, v);
278 }
279
280 void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new)
281 {
282 wmb();
283 return uatomic_cmpxchg(p, old, _new);
284 }
285
286 void *rcu_xchg_pointer_sym(void **p, void *v)
287 {
288 wmb();
289 return uatomic_xchg(p, v);
290 }
291
292 void *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
301 void rcu_quiescent_state(void)
302 {
303 _rcu_quiescent_state();
304 }
305
306 void rcu_thread_offline(void)
307 {
308 _rcu_thread_offline();
309 }
310
311 void rcu_thread_online(void)
312 {
313 _rcu_thread_online();
314 }
315
316 void rcu_register_thread(void)
317 {
318 urcu_reader.tid = pthread_self();
319 assert(urcu_reader.ctr == 0);
320
321 internal_urcu_lock();
322 list_add(&urcu_reader.head, &registry);
323 internal_urcu_unlock();
324 _rcu_thread_online();
325 }
326
327 void rcu_unregister_thread(void)
328 {
329 /*
330 * We have to make the thread offline otherwise we end up dealocking
331 * with a waiting writer.
332 */
333 _rcu_thread_offline();
334 internal_urcu_lock();
335 list_del(&urcu_reader.head);
336 internal_urcu_unlock();
337 }
338
339 void rcu_exit(void)
340 {
341 assert(list_empty(&registry));
342 }
This page took 0.035262 seconds and 4 git commands to generate.