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