Commit | Line | Data |
---|---|---|
786ee85b MD |
1 | /* |
2 | * urcu-defer.c | |
3 | * | |
4 | * Userspace RCU library - batch memory reclamation | |
5 | * | |
6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <pthread.h> | |
25 | #include <signal.h> | |
26 | #include <assert.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <errno.h> | |
30 | #include <poll.h> | |
31 | ||
32 | #include "urcu-defer-static.h" | |
33 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ | |
34 | #include "urcu-defer.h" | |
35 | ||
36 | void __attribute__((destructor)) urcu_defer_exit(void); | |
37 | ||
38 | extern void synchronize_rcu(void); | |
39 | ||
40 | /* | |
41 | * urcu_defer_mutex nests inside defer_thread_mutex. | |
42 | */ | |
43 | static pthread_mutex_t urcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER; | |
44 | static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER; | |
45 | ||
46 | /* | |
47 | * Written to only by each individual deferer. Read by both the deferer and | |
48 | * the reclamation tread. | |
49 | */ | |
50 | struct defer_queue __thread defer_queue; | |
51 | ||
52 | /* Thread IDs of registered deferers */ | |
53 | #define INIT_NUM_THREADS 4 | |
54 | ||
55 | struct deferer_registry { | |
56 | pthread_t tid; | |
57 | struct defer_queue *defer_queue; | |
58 | unsigned long last_head; | |
59 | }; | |
60 | ||
61 | static struct deferer_registry *registry; | |
62 | static int num_deferers, alloc_deferers; | |
63 | ||
64 | static pthread_t tid_defer; | |
65 | static int exit_defer; | |
66 | ||
67 | static void internal_urcu_lock(pthread_mutex_t *mutex) | |
68 | { | |
69 | int ret; | |
70 | ||
71 | #ifndef DISTRUST_SIGNALS_EXTREME | |
72 | ret = pthread_mutex_lock(mutex); | |
73 | if (ret) { | |
74 | perror("Error in pthread mutex lock"); | |
75 | exit(-1); | |
76 | } | |
77 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ | |
78 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { | |
79 | if (ret != EBUSY && ret != EINTR) { | |
80 | printf("ret = %d, errno = %d\n", ret, errno); | |
81 | perror("Error in pthread mutex lock"); | |
82 | exit(-1); | |
83 | } | |
84 | poll(NULL,0,10); | |
85 | } | |
86 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
87 | } | |
88 | ||
89 | static void internal_urcu_unlock(pthread_mutex_t *mutex) | |
90 | { | |
91 | int ret; | |
92 | ||
93 | ret = pthread_mutex_unlock(mutex); | |
94 | if (ret) { | |
95 | perror("Error in pthread mutex unlock"); | |
96 | exit(-1); | |
97 | } | |
98 | } | |
99 | ||
100 | /* | |
101 | * Must be called after Q.S. is reached. | |
102 | */ | |
103 | static void rcu_defer_barrier_queue(struct defer_queue *queue, | |
804b4375 | 104 | unsigned long head) |
786ee85b MD |
105 | { |
106 | unsigned long i; | |
804b4375 MD |
107 | void (*fct)(void *p); |
108 | void *p; | |
786ee85b MD |
109 | |
110 | /* | |
111 | * Tail is only modified when lock is held. | |
112 | * Head is only modified by owner thread. | |
113 | */ | |
114 | ||
804b4375 | 115 | for (i = queue->tail; i != head;) { |
786ee85b | 116 | smp_rmb(); /* read head before q[]. */ |
804b4375 MD |
117 | p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
118 | if (unlikely(DQ_IS_FCT_BIT(p))) { | |
804b4375 MD |
119 | DQ_CLEAR_FCT_BIT(p); |
120 | queue->last_fct_out = p; | |
121 | p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); | |
122 | } else if (unlikely(p == DQ_FCT_MARK)) { | |
804b4375 MD |
123 | p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
124 | queue->last_fct_out = p; | |
125 | p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); | |
29cdb8d8 | 126 | } |
804b4375 | 127 | fct = queue->last_fct_out; |
804b4375 | 128 | fct(p); |
786ee85b MD |
129 | } |
130 | smp_mb(); /* push tail after having used q[] */ | |
131 | STORE_SHARED(queue->tail, i); | |
132 | } | |
133 | ||
134 | static void _rcu_defer_barrier_thread(void) | |
135 | { | |
136 | unsigned long head; | |
137 | ||
138 | head = defer_queue.head; | |
139 | synchronize_rcu(); | |
140 | rcu_defer_barrier_queue(&defer_queue, head); | |
141 | } | |
142 | ||
143 | ||
144 | void rcu_defer_barrier_thread(void) | |
145 | { | |
146 | internal_urcu_lock(&urcu_defer_mutex); | |
147 | _rcu_defer_barrier_thread(); | |
148 | internal_urcu_unlock(&urcu_defer_mutex); | |
149 | } | |
150 | ||
151 | void rcu_defer_barrier(void) | |
152 | { | |
153 | struct deferer_registry *index; | |
154 | ||
155 | if (!registry) | |
156 | return; | |
157 | ||
158 | internal_urcu_lock(&urcu_defer_mutex); | |
159 | for (index = registry; index < registry + num_deferers; index++) | |
160 | index->last_head = LOAD_SHARED(index->defer_queue->head); | |
161 | synchronize_rcu(); | |
162 | for (index = registry; index < registry + num_deferers; index++) | |
163 | rcu_defer_barrier_queue(index->defer_queue, | |
164 | index->last_head); | |
165 | internal_urcu_unlock(&urcu_defer_mutex); | |
166 | } | |
167 | ||
168 | void *thr_defer(void *args) | |
169 | { | |
170 | for (;;) { | |
171 | if (LOAD_SHARED(exit_defer)) | |
172 | break; | |
173 | poll(NULL,0,100); /* wait for 100ms */ | |
174 | rcu_defer_barrier(); | |
175 | } | |
176 | ||
177 | return NULL; | |
178 | } | |
179 | ||
180 | /* | |
181 | * library wrappers to be used by non-LGPL compatible source code. | |
182 | */ | |
183 | ||
804b4375 | 184 | void rcu_defer_queue(void (*fct)(void *p), void *p) |
786ee85b | 185 | { |
804b4375 | 186 | _rcu_defer_queue(fct, p); |
786ee85b MD |
187 | } |
188 | ||
189 | static void rcu_add_deferer(pthread_t id) | |
190 | { | |
191 | struct deferer_registry *oldarray; | |
192 | ||
193 | if (!registry) { | |
194 | alloc_deferers = INIT_NUM_THREADS; | |
195 | num_deferers = 0; | |
196 | registry = | |
197 | malloc(sizeof(struct deferer_registry) * alloc_deferers); | |
198 | } | |
199 | if (alloc_deferers < num_deferers + 1) { | |
200 | oldarray = registry; | |
201 | registry = malloc(sizeof(struct deferer_registry) | |
202 | * (alloc_deferers << 1)); | |
203 | memcpy(registry, oldarray, | |
204 | sizeof(struct deferer_registry) * alloc_deferers); | |
205 | alloc_deferers <<= 1; | |
206 | free(oldarray); | |
207 | } | |
208 | registry[num_deferers].tid = id; | |
209 | /* reference to the TLS of _this_ deferer thread. */ | |
210 | registry[num_deferers].defer_queue = &defer_queue; | |
804b4375 | 211 | registry[num_deferers].last_head = 0; |
786ee85b MD |
212 | num_deferers++; |
213 | } | |
214 | ||
215 | /* | |
216 | * Never shrink (implementation limitation). | |
217 | * This is O(nb threads). Eventually use a hash table. | |
218 | */ | |
219 | static void rcu_remove_deferer(pthread_t id) | |
220 | { | |
221 | struct deferer_registry *index; | |
222 | ||
223 | assert(registry != NULL); | |
224 | for (index = registry; index < registry + num_deferers; index++) { | |
225 | if (pthread_equal(index->tid, id)) { | |
226 | memcpy(index, ®istry[num_deferers - 1], | |
227 | sizeof(struct deferer_registry)); | |
228 | registry[num_deferers - 1].tid = 0; | |
229 | registry[num_deferers - 1].defer_queue = NULL; | |
804b4375 | 230 | registry[num_deferers - 1].last_head = 0; |
786ee85b MD |
231 | num_deferers--; |
232 | return; | |
233 | } | |
234 | } | |
235 | /* Hrm not found, forgot to register ? */ | |
236 | assert(0); | |
237 | } | |
238 | ||
239 | static void start_defer_thread(void) | |
240 | { | |
241 | int ret; | |
242 | ||
243 | ret = pthread_create(&tid_defer, NULL, thr_defer, | |
244 | NULL); | |
245 | assert(!ret); | |
246 | } | |
247 | ||
248 | static void stop_defer_thread(void) | |
249 | { | |
250 | int ret; | |
251 | void *tret; | |
252 | ||
253 | STORE_SHARED(exit_defer, 1); | |
254 | ret = pthread_join(tid_defer, &tret); | |
255 | assert(!ret); | |
256 | } | |
257 | ||
258 | void rcu_defer_register_thread(void) | |
259 | { | |
260 | int deferers; | |
261 | ||
262 | internal_urcu_lock(&defer_thread_mutex); | |
263 | internal_urcu_lock(&urcu_defer_mutex); | |
264 | defer_queue.q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE); | |
265 | rcu_add_deferer(pthread_self()); | |
266 | deferers = num_deferers; | |
267 | internal_urcu_unlock(&urcu_defer_mutex); | |
268 | ||
269 | if (deferers == 1) | |
270 | start_defer_thread(); | |
271 | internal_urcu_unlock(&defer_thread_mutex); | |
272 | } | |
273 | ||
274 | void rcu_defer_unregister_thread(void) | |
275 | { | |
276 | int deferers; | |
277 | ||
278 | internal_urcu_lock(&defer_thread_mutex); | |
279 | internal_urcu_lock(&urcu_defer_mutex); | |
280 | rcu_remove_deferer(pthread_self()); | |
281 | _rcu_defer_barrier_thread(); | |
282 | free(defer_queue.q); | |
283 | defer_queue.q = NULL; | |
284 | deferers = num_deferers; | |
285 | internal_urcu_unlock(&urcu_defer_mutex); | |
286 | ||
287 | if (deferers == 0) | |
288 | stop_defer_thread(); | |
289 | internal_urcu_unlock(&defer_thread_mutex); | |
290 | } | |
291 | ||
292 | void urcu_defer_exit(void) | |
293 | { | |
294 | free(registry); | |
295 | } |