Don't mark reader thread as being waited for if non-contended
[userspace-rcu.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 static pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42 /*
43 * Global grace period counter.
44 */
45 unsigned long urcu_gp_ctr = RCU_GP_ONLINE;
46
47 /*
48 * Written to only by each individual reader. Read by both the reader and the
49 * writers.
50 */
51 struct urcu_reader_status __thread urcu_reader_status;
52
53 /* Thread IDs of registered readers */
54 #define INIT_NUM_THREADS 4
55
56 struct reader_registry {
57 pthread_t tid;
58 struct urcu_reader_status *urcu_reader_status;
59 };
60
61 #ifdef DEBUG_YIELD
62 unsigned int yield_active;
63 unsigned int __thread rand_yield;
64 #endif
65
66 static struct reader_registry *registry;
67 static int num_readers, alloc_readers;
68
69 static void internal_urcu_lock(void)
70 {
71 int ret;
72
73 #ifndef DISTRUST_SIGNALS_EXTREME
74 ret = pthread_mutex_lock(&urcu_mutex);
75 if (ret) {
76 perror("Error in pthread mutex lock");
77 exit(-1);
78 }
79 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
80 while ((ret = pthread_mutex_trylock(&urcu_mutex)) != 0) {
81 if (ret != EBUSY && ret != EINTR) {
82 printf("ret = %d, errno = %d\n", ret, errno);
83 perror("Error in pthread mutex lock");
84 exit(-1);
85 }
86 poll(NULL,0,10);
87 }
88 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
89 }
90
91 static void internal_urcu_unlock(void)
92 {
93 int ret;
94
95 ret = pthread_mutex_unlock(&urcu_mutex);
96 if (ret) {
97 perror("Error in pthread mutex unlock");
98 exit(-1);
99 }
100 }
101
102 /*
103 * synchronize_rcu() waiting. Single thread.
104 */
105 static void wait_for_quiescent_state(void)
106 {
107 struct reader_registry *index;
108
109 if (!registry)
110 return;
111 /*
112 * Wait for each thread rcu_reader qs_gp count to become 0.
113 */
114 for (index = registry; index < registry + num_readers; index++) {
115 int wait_loops = 0;
116
117 index->urcu_reader_status->gp_waiting = 1;
118 while (rcu_gp_ongoing(&index->urcu_reader_status->qs_gp)) {
119 if (wait_loops++ == RCU_QS_ACTIVE_ATTEMPTS) {
120 sched_yield(); /* ideally sched_yield_to() */
121 wait_loops = 0;
122 } else {
123 #ifndef HAS_INCOHERENT_CACHES
124 cpu_relax();
125 #else /* #ifndef HAS_INCOHERENT_CACHES */
126 smp_mb();
127 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
128 }
129 }
130 index->urcu_reader_status->gp_waiting = 0;
131 }
132 }
133
134 /*
135 * Using a two-subphases algorithm for architectures with smaller than 64-bit
136 * long-size to ensure we do not encounter an overflow bug.
137 */
138
139 #if (BITS_PER_LONG < 64)
140 /*
141 * called with urcu_mutex held.
142 */
143 static void switch_next_urcu_qparity(void)
144 {
145 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR);
146 }
147
148 void synchronize_rcu(void)
149 {
150 unsigned long was_online;
151
152 was_online = urcu_reader_status.qs_gp;
153
154 /* All threads should read qparity before accessing data structure
155 * where new ptr points to.
156 */
157 /* Write new ptr before changing the qparity */
158 smp_mb();
159
160 /*
161 * Mark the writer thread offline to make sure we don't wait for
162 * our own quiescent state. This allows using synchronize_rcu() in
163 * threads registered as readers.
164 */
165 if (was_online)
166 STORE_SHARED(urcu_reader_status.qs_gp, 0);
167
168 internal_urcu_lock();
169
170 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
171
172 switch_next_urcu_qparity(); /* 0 -> 1 */
173
174 /*
175 * Must commit qparity update to memory before waiting for parity
176 * 0 quiescent state. Failure to do so could result in the writer
177 * waiting forever while new readers are always accessing data (no
178 * progress).
179 * Ensured by STORE_SHARED and LOAD_SHARED.
180 */
181
182 /*
183 * Wait for previous parity to be empty of readers.
184 */
185 wait_for_quiescent_state(); /* Wait readers in parity 0 */
186
187 /*
188 * Must finish waiting for quiescent state for parity 0 before
189 * committing qparity update to memory. Failure to do so could result in
190 * the writer waiting forever while new readers are always accessing
191 * data (no progress).
192 * Ensured by STORE_SHARED and LOAD_SHARED.
193 */
194
195 switch_next_urcu_qparity(); /* 1 -> 0 */
196
197 /*
198 * Must commit qparity update to memory before waiting for parity
199 * 1 quiescent state. Failure to do so could result in the writer
200 * waiting forever while new readers are always accessing data (no
201 * progress).
202 * Ensured by STORE_SHARED and LOAD_SHARED.
203 */
204
205 /*
206 * Wait for previous parity to be empty of readers.
207 */
208 wait_for_quiescent_state(); /* Wait readers in parity 1 */
209
210 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
211
212 internal_urcu_unlock();
213
214 /*
215 * Finish waiting for reader threads before letting the old ptr being
216 * freed.
217 */
218 if (was_online)
219 _STORE_SHARED(urcu_reader_status.qs_gp,
220 LOAD_SHARED(urcu_gp_ctr));
221 smp_mb();
222 }
223 #else /* !(BITS_PER_LONG < 64) */
224 void synchronize_rcu(void)
225 {
226 unsigned long was_online;
227
228 was_online = urcu_reader_status.qs_gp;
229
230 /*
231 * Mark the writer thread offline to make sure we don't wait for
232 * our own quiescent state. This allows using synchronize_rcu() in
233 * threads registered as readers.
234 */
235 smp_mb();
236 if (was_online)
237 STORE_SHARED(urcu_reader_status.qs_gp, 0);
238
239 internal_urcu_lock();
240 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
241 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr + RCU_GP_CTR);
242 wait_for_quiescent_state();
243 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_ONGOING);
244 internal_urcu_unlock();
245
246 if (was_online)
247 _STORE_SHARED(urcu_reader_status.qs_gp,
248 LOAD_SHARED(urcu_gp_ctr));
249 smp_mb();
250 }
251 #endif /* !(BITS_PER_LONG < 64) */
252
253 /*
254 * library wrappers to be used by non-LGPL compatible source code.
255 */
256
257 void rcu_read_lock(void)
258 {
259 _rcu_read_lock();
260 }
261
262 void rcu_read_unlock(void)
263 {
264 _rcu_read_unlock();
265 }
266
267 void *rcu_dereference(void *p)
268 {
269 return _rcu_dereference(p);
270 }
271
272 void *rcu_assign_pointer_sym(void **p, void *v)
273 {
274 wmb();
275 return STORE_SHARED(p, v);
276 }
277
278 void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new)
279 {
280 wmb();
281 return cmpxchg(p, old, _new);
282 }
283
284 void *rcu_xchg_pointer_sym(void **p, void *v)
285 {
286 wmb();
287 return xchg(p, v);
288 }
289
290 void *rcu_publish_content_sym(void **p, void *v)
291 {
292 void *oldptr;
293
294 oldptr = _rcu_xchg_pointer(p, v);
295 synchronize_rcu();
296 return oldptr;
297 }
298
299 void rcu_quiescent_state(void)
300 {
301 _rcu_quiescent_state();
302 }
303
304 void rcu_thread_offline(void)
305 {
306 _rcu_thread_offline();
307 }
308
309 void rcu_thread_online(void)
310 {
311 _rcu_thread_online();
312 }
313
314 static void rcu_add_reader(pthread_t id)
315 {
316 struct reader_registry *oldarray;
317
318 if (!registry) {
319 alloc_readers = INIT_NUM_THREADS;
320 num_readers = 0;
321 registry =
322 malloc(sizeof(struct reader_registry) * alloc_readers);
323 }
324 if (alloc_readers < num_readers + 1) {
325 oldarray = registry;
326 registry = malloc(sizeof(struct reader_registry)
327 * (alloc_readers << 1));
328 memcpy(registry, oldarray,
329 sizeof(struct reader_registry) * alloc_readers);
330 alloc_readers <<= 1;
331 free(oldarray);
332 }
333 registry[num_readers].tid = id;
334 /* reference to the TLS of _this_ reader thread. */
335 registry[num_readers].urcu_reader_status = &urcu_reader_status;
336 num_readers++;
337 }
338
339 /*
340 * Never shrink (implementation limitation).
341 * This is O(nb threads). Eventually use a hash table.
342 */
343 static void rcu_remove_reader(pthread_t id)
344 {
345 struct reader_registry *index;
346
347 assert(registry != NULL);
348 for (index = registry; index < registry + num_readers; index++) {
349 if (pthread_equal(index->tid, id)) {
350 memcpy(index, &registry[num_readers - 1],
351 sizeof(struct reader_registry));
352 registry[num_readers - 1].tid = 0;
353 registry[num_readers - 1].urcu_reader_status = NULL;
354 num_readers--;
355 return;
356 }
357 }
358 /* Hrm not found, forgot to register ? */
359 assert(0);
360 }
361
362 void rcu_register_thread(void)
363 {
364 internal_urcu_lock();
365 rcu_add_reader(pthread_self());
366 internal_urcu_unlock();
367 _rcu_thread_online();
368 }
369
370 void rcu_unregister_thread(void)
371 {
372 /*
373 * We have to make the thread offline otherwise we end up dealocking
374 * with a waiting writer.
375 */
376 _rcu_thread_offline();
377 internal_urcu_lock();
378 rcu_remove_reader(pthread_self());
379 internal_urcu_unlock();
380 }
This page took 0.036356 seconds and 4 git commands to generate.