s390: fix arch_s390.h include of urcu/compiler.h
[urcu.git] / urcu-bp.c
1 /*
2 * urcu-bp.c
3 *
4 * Userspace RCU library, "bulletproof" version.
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 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <poll.h>
35 #include <unistd.h>
36 #include <sys/mman.h>
37
38 #include "urcu-bp-static.h"
39 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
40 #include "urcu-bp.h"
41
42 /* Sleep delay in us */
43 #define RCU_SLEEP_DELAY 1000
44 #define ARENA_INIT_ALLOC 16
45
46 void __attribute__((destructor)) rcu_bp_exit(void);
47
48 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
49
50 #ifdef DEBUG_YIELD
51 unsigned int yield_active;
52 unsigned int __thread rand_yield;
53 #endif
54
55 /*
56 * Global grace period counter.
57 * Contains the current RCU_GP_CTR_PHASE.
58 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
59 * Written to only by writer with mutex taken. Read by both writer and readers.
60 */
61 long rcu_gp_ctr = RCU_GP_COUNT;
62
63 /*
64 * Pointer to registry elements. Written to only by each individual reader. Read
65 * by both the reader and the writers.
66 */
67 struct rcu_reader __thread *rcu_reader;
68
69 static LIST_HEAD(registry);
70
71 struct registry_arena {
72 void *p;
73 size_t len;
74 size_t used;
75 };
76
77 static struct registry_arena registry_arena;
78
79 static void rcu_gc_registry(void);
80
81 static void mutex_lock(pthread_mutex_t *mutex)
82 {
83 int ret;
84
85 #ifndef DISTRUST_SIGNALS_EXTREME
86 ret = pthread_mutex_lock(mutex);
87 if (ret) {
88 perror("Error in pthread mutex lock");
89 exit(-1);
90 }
91 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
92 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
93 if (ret != EBUSY && ret != EINTR) {
94 printf("ret = %d, errno = %d\n", ret, errno);
95 perror("Error in pthread mutex lock");
96 exit(-1);
97 }
98 if (rcu_reader.need_mb) {
99 smp_mb();
100 rcu_reader.need_mb = 0;
101 smp_mb();
102 }
103 poll(NULL,0,10);
104 }
105 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
106 }
107
108 static void mutex_unlock(pthread_mutex_t *mutex)
109 {
110 int ret;
111
112 ret = pthread_mutex_unlock(mutex);
113 if (ret) {
114 perror("Error in pthread mutex unlock");
115 exit(-1);
116 }
117 }
118
119 void update_counter_and_wait(void)
120 {
121 LIST_HEAD(qsreaders);
122 int wait_loops = 0;
123 struct rcu_reader *index, *tmp;
124
125 /* Switch parity: 0 -> 1, 1 -> 0 */
126 STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
127
128 /*
129 * Must commit qparity update to memory before waiting for other parity
130 * quiescent state. Failure to do so could result in the writer waiting
131 * forever while new readers are always accessing data (no progress).
132 * Ensured by STORE_SHARED and LOAD_SHARED.
133 */
134
135 /*
136 * Adding a smp_mb() which is _not_ formally required, but makes the
137 * model easier to understand. It does not have a big performance impact
138 * anyway, given this is the write-side.
139 */
140 smp_mb();
141
142 /*
143 * Wait for each thread rcu_reader.ctr count to become 0.
144 */
145 for (;;) {
146 wait_loops++;
147 list_for_each_entry_safe(index, tmp, &registry, head) {
148 if (!rcu_old_gp_ongoing(&index->ctr))
149 list_move(&index->head, &qsreaders);
150 }
151
152 if (list_empty(&registry)) {
153 break;
154 } else {
155 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
156 usleep(RCU_SLEEP_DELAY);
157 else
158 cpu_relax();
159 }
160 }
161 /* put back the reader list in the registry */
162 list_splice(&qsreaders, &registry);
163 }
164
165 void synchronize_rcu(void)
166 {
167 sigset_t newmask, oldmask;
168 int ret;
169
170 ret = sigemptyset(&newmask);
171 assert(!ret);
172 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
173 assert(!ret);
174
175 mutex_lock(&rcu_gp_lock);
176
177 if (list_empty(&registry))
178 goto out;
179
180 /* All threads should read qparity before accessing data structure
181 * where new ptr points to. */
182 /* Write new ptr before changing the qparity */
183 smp_mb();
184
185 /* Remove old registry elements */
186 rcu_gc_registry();
187
188 /*
189 * Wait for previous parity to be empty of readers.
190 */
191 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
192
193 /*
194 * Adding a smp_mb() which is _not_ formally required, but makes the
195 * model easier to understand. It does not have a big performance impact
196 * anyway, given this is the write-side.
197 */
198 smp_mb();
199
200 /*
201 * Wait for previous parity to be empty of readers.
202 */
203 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
204
205 /*
206 * Finish waiting for reader threads before letting the old ptr being
207 * freed.
208 */
209 smp_mb();
210 out:
211 mutex_unlock(&rcu_gp_lock);
212 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
213 assert(!ret);
214 }
215
216 /*
217 * library wrappers to be used by non-LGPL compatible source code.
218 */
219
220 void rcu_read_lock(void)
221 {
222 _rcu_read_lock();
223 }
224
225 void rcu_read_unlock(void)
226 {
227 _rcu_read_unlock();
228 }
229
230 /*
231 * only grow for now.
232 */
233 static void resize_arena(struct registry_arena *arena, size_t len)
234 {
235 void *new_arena;
236
237 if (!arena->p)
238 new_arena = mmap(arena->p, len,
239 PROT_READ | PROT_WRITE,
240 MAP_ANONYMOUS | MAP_PRIVATE,
241 -1, 0);
242 else
243 new_arena = mremap(arena->p, arena->len,
244 len, MREMAP_MAYMOVE);
245 assert(new_arena != MAP_FAILED);
246
247 /*
248 * re-used the same region ?
249 */
250 if (new_arena == arena->p)
251 return;
252
253 memcpy(new_arena, arena->p, arena->len);
254 bzero(new_arena + arena->len, len - arena->len);
255 arena->p = new_arena;
256 }
257
258 /* Called with signals off and mutex locked */
259 static void add_thread(void)
260 {
261 struct rcu_reader *rcu_reader_reg;
262
263 if (registry_arena.len
264 < registry_arena.used + sizeof(struct rcu_reader))
265 resize_arena(&registry_arena,
266 max(registry_arena.len << 1, ARENA_INIT_ALLOC));
267 /*
268 * Find a free spot.
269 */
270 for (rcu_reader_reg = registry_arena.p;
271 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
272 rcu_reader_reg++) {
273 if (!rcu_reader_reg->alloc)
274 break;
275 }
276 rcu_reader_reg->alloc = 1;
277 registry_arena.used += sizeof(struct rcu_reader);
278
279 /* Add to registry */
280 rcu_reader_reg->tid = pthread_self();
281 assert(rcu_reader_reg->ctr == 0);
282 list_add(&rcu_reader_reg->head, &registry);
283 rcu_reader = rcu_reader_reg;
284 }
285
286 /* Called with signals off and mutex locked */
287 static void rcu_gc_registry(void)
288 {
289 struct rcu_reader *rcu_reader_reg;
290 pthread_t tid;
291 int ret;
292
293 for (rcu_reader_reg = registry_arena.p;
294 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
295 rcu_reader_reg++) {
296 if (!rcu_reader_reg->alloc)
297 continue;
298 tid = rcu_reader_reg->tid;
299 ret = pthread_kill(tid, 0);
300 assert(ret != EINVAL);
301 if (ret == ESRCH) {
302 list_del(&rcu_reader_reg->head);
303 rcu_reader_reg->alloc = 0;
304 registry_arena.used -= sizeof(struct rcu_reader);
305 }
306 }
307 }
308
309 /* Disable signals, take mutex, add to registry */
310 void rcu_bp_register(void)
311 {
312 sigset_t newmask, oldmask;
313 int ret;
314
315 ret = sigemptyset(&newmask);
316 assert(!ret);
317 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
318 assert(!ret);
319
320 /*
321 * Check if a signal concurrently registered our thread since
322 * the check in rcu_read_lock(). */
323 if (rcu_reader)
324 goto end;
325
326 mutex_lock(&rcu_gp_lock);
327 add_thread();
328 mutex_unlock(&rcu_gp_lock);
329 end:
330 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
331 assert(!ret);
332 }
333
334 void rcu_bp_exit()
335 {
336 munmap(registry_arena.p, registry_arena.len);
337 }
This page took 0.035059 seconds and 4 git commands to generate.