Expose nop QSBR read lock to non-GPL/LGPL programs
[urcu.git] / urcu-qsbr.c
CommitLineData
9f1621ca 1/*
7ac06cef 2 * urcu-qsbr.c
9f1621ca 3 *
7ac06cef 4 * Userspace RCU QSBR library
9f1621ca
MD
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
727f819d 35#define BUILD_QSBR_LIB
7ac06cef 36#include "urcu-qsbr-static.h"
9f1621ca 37/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
7ac06cef 38#include "urcu-qsbr.h"
9f1621ca 39
9f1621ca
MD
40pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42/*
43 * Global grace period counter.
44 */
45long urcu_gp_ctr = 0;
46
47/*
48 * Written to only by each individual reader. Read by both the reader and the
49 * writers.
50 */
3395d46c 51long __thread rcu_reader_qs_gp;
9f1621ca
MD
52
53/* Thread IDs of registered readers */
54#define INIT_NUM_THREADS 4
55
56struct reader_registry {
57 pthread_t tid;
3395d46c 58 long *rcu_reader_qs_gp;
9f1621ca
MD
59};
60
61#ifdef DEBUG_YIELD
62unsigned int yield_active;
63unsigned int __thread rand_yield;
64#endif
65
66static struct reader_registry *registry;
9f1621ca
MD
67static int num_readers, alloc_readers;
68
90c1618a 69static void internal_urcu_lock(void)
9f1621ca
MD
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 }
9f1621ca
MD
86 poll(NULL,0,10);
87 }
88#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
89}
90
90c1618a 91static void internal_urcu_unlock(void)
9f1621ca
MD
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
9f1621ca
MD
102#ifdef HAS_INCOHERENT_CACHES
103static void force_mb_single_thread(struct reader_registry *index)
104{
105 smp_mb();
106}
107#endif /* #ifdef HAS_INCOHERENT_CACHES */
108
109static void force_mb_all_threads(void)
110{
111 smp_mb();
112}
9f1621ca 113
90c1618a 114static void wait_for_quiescent_state(void)
9f1621ca
MD
115{
116 struct reader_registry *index;
117
118 if (!registry)
119 return;
120 /*
3395d46c 121 * Wait for each thread rcu_reader_qs_gp count to become 0.
9f1621ca
MD
122 */
123 for (index = registry; index < registry + num_readers; index++) {
124#ifndef HAS_INCOHERENT_CACHES
3395d46c
MD
125 while (rcu_gp_ongoing(index->rcu_reader_qs_gp) &&
126 (*index->rcu_reader_qs_gp - urcu_gp_ctr < 0))
9f1621ca
MD
127 cpu_relax();
128#else /* #ifndef HAS_INCOHERENT_CACHES */
129 int wait_loops = 0;
130 /*
131 * BUSY-LOOP. Force the reader thread to commit its
3395d46c 132 * rcu_reader_qs_gp update to memory if we wait for too long.
9f1621ca 133 */
3395d46c
MD
134 while (rcu_gp_ongoing(index->rcu_reader_qs_gp) &&
135 (*index->rcu_reader_qs_gp - urcu_gp_ctr < 0)) {
9f1621ca
MD
136 if (wait_loops++ == KICK_READER_LOOPS) {
137 force_mb_single_thread(index);
138 wait_loops = 0;
139 } else {
140 cpu_relax();
141 }
142 }
143#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
144 }
145}
146
147void synchronize_rcu(void)
148{
ff2f67a0
MD
149 int was_online;
150
151 was_online = rcu_reader_qs_gp & 1;
152
153 /*
154 * Mark the writer thread offline to make sure we don't wait for
155 * our own quiescent state. This allows using synchronize_rcu() in
156 * threads registered as readers.
157 */
158 if (was_online)
159 _rcu_thread_offline();
160
9f1621ca
MD
161 internal_urcu_lock();
162 force_mb_all_threads();
163 urcu_gp_ctr += 2;
164 wait_for_quiescent_state();
165 force_mb_all_threads();
166 internal_urcu_unlock();
ff2f67a0
MD
167
168 if (was_online)
169 _rcu_thread_online();
9f1621ca
MD
170}
171
172/*
173 * library wrappers to be used by non-LGPL compatible source code.
174 */
175
176void rcu_read_lock(void)
177{
178 _rcu_read_lock();
179}
180
181void rcu_read_unlock(void)
182{
183 _rcu_read_unlock();
184}
185
186void *rcu_dereference(void *p)
187{
188 return _rcu_dereference(p);
189}
190
191void *rcu_assign_pointer_sym(void **p, void *v)
192{
193 wmb();
194 return STORE_SHARED(p, v);
195}
196
197void *rcu_xchg_pointer_sym(void **p, void *v)
198{
199 wmb();
200 return xchg(p, v);
201}
202
203void *rcu_publish_content_sym(void **p, void *v)
204{
205 void *oldptr;
206
207 oldptr = _rcu_xchg_pointer(p, v);
208 synchronize_rcu();
209 return oldptr;
210}
211
7ac06cef
MD
212void rcu_quiescent_state(void)
213{
214 _rcu_quiescent_state();
215}
216
217void rcu_thread_offline(void)
218{
219 _rcu_thread_offline();
220}
221
222void rcu_thread_online(void)
223{
224 _rcu_thread_online();
225}
226
9f1621ca
MD
227static void rcu_add_reader(pthread_t id)
228{
229 struct reader_registry *oldarray;
230
231 if (!registry) {
232 alloc_readers = INIT_NUM_THREADS;
233 num_readers = 0;
234 registry =
235 malloc(sizeof(struct reader_registry) * alloc_readers);
236 }
237 if (alloc_readers < num_readers + 1) {
238 oldarray = registry;
239 registry = malloc(sizeof(struct reader_registry)
240 * (alloc_readers << 1));
241 memcpy(registry, oldarray,
242 sizeof(struct reader_registry) * alloc_readers);
243 alloc_readers <<= 1;
244 free(oldarray);
245 }
246 registry[num_readers].tid = id;
247 /* reference to the TLS of _this_ reader thread. */
3395d46c 248 registry[num_readers].rcu_reader_qs_gp = &rcu_reader_qs_gp;
9f1621ca
MD
249 num_readers++;
250}
251
252/*
253 * Never shrink (implementation limitation).
254 * This is O(nb threads). Eventually use a hash table.
255 */
256static void rcu_remove_reader(pthread_t id)
257{
258 struct reader_registry *index;
259
260 assert(registry != NULL);
261 for (index = registry; index < registry + num_readers; index++) {
262 if (pthread_equal(index->tid, id)) {
263 memcpy(index, &registry[num_readers - 1],
264 sizeof(struct reader_registry));
265 registry[num_readers - 1].tid = 0;
3395d46c 266 registry[num_readers - 1].rcu_reader_qs_gp = NULL;
9f1621ca
MD
267 num_readers--;
268 return;
269 }
270 }
271 /* Hrm not found, forgot to register ? */
272 assert(0);
273}
274
275void rcu_register_thread(void)
276{
277 internal_urcu_lock();
9f1621ca
MD
278 rcu_add_reader(pthread_self());
279 internal_urcu_unlock();
5f373c84 280 _rcu_thread_online();
9f1621ca
MD
281}
282
283void rcu_unregister_thread(void)
284{
76f3022f
MD
285 /*
286 * We have to make the thread offline otherwise we end up dealocking
287 * with a waiting writer.
288 */
289 _rcu_thread_offline();
9f1621ca
MD
290 internal_urcu_lock();
291 rcu_remove_reader(pthread_self());
292 internal_urcu_unlock();
293}
This page took 0.033891 seconds and 4 git commands to generate.