Commit | Line | Data |
---|---|---|
9f1621ca MD |
1 | /* |
2 | * urcu.c | |
3 | * | |
4 | * Userspace RCU 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 | #include "urcu-static.h" | |
36 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ | |
37 | #include "urcu.h" | |
38 | ||
9f1621ca MD |
39 | pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
40 | ||
41 | /* | |
42 | * Global grace period counter. | |
43 | */ | |
44 | long urcu_gp_ctr = 0; | |
45 | ||
46 | /* | |
47 | * Written to only by each individual reader. Read by both the reader and the | |
48 | * writers. | |
49 | */ | |
50 | long __thread urcu_active_readers; | |
51 | ||
52 | /* Thread IDs of registered readers */ | |
53 | #define INIT_NUM_THREADS 4 | |
54 | ||
55 | struct reader_registry { | |
56 | pthread_t tid; | |
57 | long *urcu_active_readers; | |
58 | char *need_mb; | |
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 char __thread need_mb; | |
68 | static int num_readers, alloc_readers; | |
69 | ||
70 | void internal_urcu_lock(void) | |
71 | { | |
72 | int ret; | |
73 | ||
74 | #ifndef DISTRUST_SIGNALS_EXTREME | |
75 | ret = pthread_mutex_lock(&urcu_mutex); | |
76 | if (ret) { | |
77 | perror("Error in pthread mutex lock"); | |
78 | exit(-1); | |
79 | } | |
80 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ | |
81 | while ((ret = pthread_mutex_trylock(&urcu_mutex)) != 0) { | |
82 | if (ret != EBUSY && ret != EINTR) { | |
83 | printf("ret = %d, errno = %d\n", ret, errno); | |
84 | perror("Error in pthread mutex lock"); | |
85 | exit(-1); | |
86 | } | |
87 | if (need_mb) { | |
88 | smp_mb(); | |
89 | need_mb = 0; | |
90 | smp_mb(); | |
91 | } | |
92 | poll(NULL,0,10); | |
93 | } | |
94 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
95 | } | |
96 | ||
97 | void internal_urcu_unlock(void) | |
98 | { | |
99 | int ret; | |
100 | ||
101 | ret = pthread_mutex_unlock(&urcu_mutex); | |
102 | if (ret) { | |
103 | perror("Error in pthread mutex unlock"); | |
104 | exit(-1); | |
105 | } | |
106 | } | |
107 | ||
9f1621ca MD |
108 | #ifdef HAS_INCOHERENT_CACHES |
109 | static void force_mb_single_thread(struct reader_registry *index) | |
110 | { | |
111 | smp_mb(); | |
112 | } | |
113 | #endif /* #ifdef HAS_INCOHERENT_CACHES */ | |
114 | ||
115 | static void force_mb_all_threads(void) | |
116 | { | |
117 | smp_mb(); | |
118 | } | |
9f1621ca MD |
119 | |
120 | void wait_for_quiescent_state(void) | |
121 | { | |
122 | struct reader_registry *index; | |
123 | ||
124 | if (!registry) | |
125 | return; | |
126 | /* | |
127 | * Wait for each thread urcu_active_readers count to become 0. | |
128 | */ | |
129 | for (index = registry; index < registry + num_readers; index++) { | |
130 | #ifndef HAS_INCOHERENT_CACHES | |
131 | while (rcu_old_gp_ongoing(index->urcu_active_readers)) | |
132 | cpu_relax(); | |
133 | #else /* #ifndef HAS_INCOHERENT_CACHES */ | |
134 | int wait_loops = 0; | |
135 | /* | |
136 | * BUSY-LOOP. Force the reader thread to commit its | |
137 | * urcu_active_readers update to memory if we wait for too long. | |
138 | */ | |
139 | while (rcu_old_gp_ongoing(index->urcu_active_readers)) { | |
140 | if (wait_loops++ == KICK_READER_LOOPS) { | |
141 | force_mb_single_thread(index); | |
142 | wait_loops = 0; | |
143 | } else { | |
144 | cpu_relax(); | |
145 | } | |
146 | } | |
147 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ | |
148 | } | |
149 | } | |
150 | ||
151 | void synchronize_rcu(void) | |
152 | { | |
153 | internal_urcu_lock(); | |
154 | force_mb_all_threads(); | |
155 | urcu_gp_ctr += 2; | |
156 | wait_for_quiescent_state(); | |
157 | force_mb_all_threads(); | |
158 | internal_urcu_unlock(); | |
159 | } | |
160 | ||
161 | /* | |
162 | * library wrappers to be used by non-LGPL compatible source code. | |
163 | */ | |
164 | ||
165 | void rcu_read_lock(void) | |
166 | { | |
167 | _rcu_read_lock(); | |
168 | } | |
169 | ||
170 | void rcu_read_unlock(void) | |
171 | { | |
172 | _rcu_read_unlock(); | |
173 | } | |
174 | ||
175 | void *rcu_dereference(void *p) | |
176 | { | |
177 | return _rcu_dereference(p); | |
178 | } | |
179 | ||
180 | void *rcu_assign_pointer_sym(void **p, void *v) | |
181 | { | |
182 | wmb(); | |
183 | return STORE_SHARED(p, v); | |
184 | } | |
185 | ||
186 | void *rcu_xchg_pointer_sym(void **p, void *v) | |
187 | { | |
188 | wmb(); | |
189 | return xchg(p, v); | |
190 | } | |
191 | ||
192 | void *rcu_publish_content_sym(void **p, void *v) | |
193 | { | |
194 | void *oldptr; | |
195 | ||
196 | oldptr = _rcu_xchg_pointer(p, v); | |
197 | synchronize_rcu(); | |
198 | return oldptr; | |
199 | } | |
200 | ||
201 | static void rcu_add_reader(pthread_t id) | |
202 | { | |
203 | struct reader_registry *oldarray; | |
204 | ||
205 | if (!registry) { | |
206 | alloc_readers = INIT_NUM_THREADS; | |
207 | num_readers = 0; | |
208 | registry = | |
209 | malloc(sizeof(struct reader_registry) * alloc_readers); | |
210 | } | |
211 | if (alloc_readers < num_readers + 1) { | |
212 | oldarray = registry; | |
213 | registry = malloc(sizeof(struct reader_registry) | |
214 | * (alloc_readers << 1)); | |
215 | memcpy(registry, oldarray, | |
216 | sizeof(struct reader_registry) * alloc_readers); | |
217 | alloc_readers <<= 1; | |
218 | free(oldarray); | |
219 | } | |
220 | registry[num_readers].tid = id; | |
221 | /* reference to the TLS of _this_ reader thread. */ | |
222 | registry[num_readers].urcu_active_readers = &urcu_active_readers; | |
223 | registry[num_readers].need_mb = &need_mb; | |
224 | num_readers++; | |
225 | } | |
226 | ||
227 | /* | |
228 | * Never shrink (implementation limitation). | |
229 | * This is O(nb threads). Eventually use a hash table. | |
230 | */ | |
231 | static void rcu_remove_reader(pthread_t id) | |
232 | { | |
233 | struct reader_registry *index; | |
234 | ||
235 | assert(registry != NULL); | |
236 | for (index = registry; index < registry + num_readers; index++) { | |
237 | if (pthread_equal(index->tid, id)) { | |
238 | memcpy(index, ®istry[num_readers - 1], | |
239 | sizeof(struct reader_registry)); | |
240 | registry[num_readers - 1].tid = 0; | |
241 | registry[num_readers - 1].urcu_active_readers = NULL; | |
242 | num_readers--; | |
243 | return; | |
244 | } | |
245 | } | |
246 | /* Hrm not found, forgot to register ? */ | |
247 | assert(0); | |
248 | } | |
249 | ||
250 | void rcu_register_thread(void) | |
251 | { | |
252 | internal_urcu_lock(); | |
9f1621ca MD |
253 | rcu_add_reader(pthread_self()); |
254 | internal_urcu_unlock(); | |
255 | } | |
256 | ||
257 | void rcu_unregister_thread(void) | |
258 | { | |
259 | internal_urcu_lock(); | |
260 | rcu_remove_reader(pthread_self()); | |
261 | internal_urcu_unlock(); | |
262 | } |