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, | |
104 | unsigned long head) | |
105 | { | |
106 | unsigned long i; | |
107 | ||
108 | /* | |
109 | * Tail is only modified when lock is held. | |
110 | * Head is only modified by owner thread. | |
111 | */ | |
112 | ||
113 | for (i = queue->tail; i != head; i++) { | |
114 | smp_rmb(); /* read head before q[]. */ | |
115 | free(LOAD_SHARED(queue->q[i & DEFER_QUEUE_MASK])); | |
116 | } | |
117 | smp_mb(); /* push tail after having used q[] */ | |
118 | STORE_SHARED(queue->tail, i); | |
119 | } | |
120 | ||
121 | static void _rcu_defer_barrier_thread(void) | |
122 | { | |
123 | unsigned long head; | |
124 | ||
125 | head = defer_queue.head; | |
126 | synchronize_rcu(); | |
127 | rcu_defer_barrier_queue(&defer_queue, head); | |
128 | } | |
129 | ||
130 | ||
131 | void rcu_defer_barrier_thread(void) | |
132 | { | |
133 | internal_urcu_lock(&urcu_defer_mutex); | |
134 | _rcu_defer_barrier_thread(); | |
135 | internal_urcu_unlock(&urcu_defer_mutex); | |
136 | } | |
137 | ||
138 | void rcu_defer_barrier(void) | |
139 | { | |
140 | struct deferer_registry *index; | |
141 | ||
142 | if (!registry) | |
143 | return; | |
144 | ||
145 | internal_urcu_lock(&urcu_defer_mutex); | |
146 | for (index = registry; index < registry + num_deferers; index++) | |
147 | index->last_head = LOAD_SHARED(index->defer_queue->head); | |
148 | synchronize_rcu(); | |
149 | for (index = registry; index < registry + num_deferers; index++) | |
150 | rcu_defer_barrier_queue(index->defer_queue, | |
151 | index->last_head); | |
152 | internal_urcu_unlock(&urcu_defer_mutex); | |
153 | } | |
154 | ||
155 | void *thr_defer(void *args) | |
156 | { | |
157 | for (;;) { | |
158 | if (LOAD_SHARED(exit_defer)) | |
159 | break; | |
160 | poll(NULL,0,100); /* wait for 100ms */ | |
161 | rcu_defer_barrier(); | |
162 | } | |
163 | ||
164 | return NULL; | |
165 | } | |
166 | ||
167 | /* | |
168 | * library wrappers to be used by non-LGPL compatible source code. | |
169 | */ | |
170 | ||
171 | void rcu_defer_queue(void *p) | |
172 | { | |
173 | _rcu_defer_queue(p); | |
174 | } | |
175 | ||
176 | static void rcu_add_deferer(pthread_t id) | |
177 | { | |
178 | struct deferer_registry *oldarray; | |
179 | ||
180 | if (!registry) { | |
181 | alloc_deferers = INIT_NUM_THREADS; | |
182 | num_deferers = 0; | |
183 | registry = | |
184 | malloc(sizeof(struct deferer_registry) * alloc_deferers); | |
185 | } | |
186 | if (alloc_deferers < num_deferers + 1) { | |
187 | oldarray = registry; | |
188 | registry = malloc(sizeof(struct deferer_registry) | |
189 | * (alloc_deferers << 1)); | |
190 | memcpy(registry, oldarray, | |
191 | sizeof(struct deferer_registry) * alloc_deferers); | |
192 | alloc_deferers <<= 1; | |
193 | free(oldarray); | |
194 | } | |
195 | registry[num_deferers].tid = id; | |
196 | /* reference to the TLS of _this_ deferer thread. */ | |
197 | registry[num_deferers].defer_queue = &defer_queue; | |
198 | num_deferers++; | |
199 | } | |
200 | ||
201 | /* | |
202 | * Never shrink (implementation limitation). | |
203 | * This is O(nb threads). Eventually use a hash table. | |
204 | */ | |
205 | static void rcu_remove_deferer(pthread_t id) | |
206 | { | |
207 | struct deferer_registry *index; | |
208 | ||
209 | assert(registry != NULL); | |
210 | for (index = registry; index < registry + num_deferers; index++) { | |
211 | if (pthread_equal(index->tid, id)) { | |
212 | memcpy(index, ®istry[num_deferers - 1], | |
213 | sizeof(struct deferer_registry)); | |
214 | registry[num_deferers - 1].tid = 0; | |
215 | registry[num_deferers - 1].defer_queue = NULL; | |
216 | num_deferers--; | |
217 | return; | |
218 | } | |
219 | } | |
220 | /* Hrm not found, forgot to register ? */ | |
221 | assert(0); | |
222 | } | |
223 | ||
224 | static void start_defer_thread(void) | |
225 | { | |
226 | int ret; | |
227 | ||
228 | ret = pthread_create(&tid_defer, NULL, thr_defer, | |
229 | NULL); | |
230 | assert(!ret); | |
231 | } | |
232 | ||
233 | static void stop_defer_thread(void) | |
234 | { | |
235 | int ret; | |
236 | void *tret; | |
237 | ||
238 | STORE_SHARED(exit_defer, 1); | |
239 | ret = pthread_join(tid_defer, &tret); | |
240 | assert(!ret); | |
241 | } | |
242 | ||
243 | void rcu_defer_register_thread(void) | |
244 | { | |
245 | int deferers; | |
246 | ||
247 | internal_urcu_lock(&defer_thread_mutex); | |
248 | internal_urcu_lock(&urcu_defer_mutex); | |
249 | defer_queue.q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE); | |
250 | rcu_add_deferer(pthread_self()); | |
251 | deferers = num_deferers; | |
252 | internal_urcu_unlock(&urcu_defer_mutex); | |
253 | ||
254 | if (deferers == 1) | |
255 | start_defer_thread(); | |
256 | internal_urcu_unlock(&defer_thread_mutex); | |
257 | } | |
258 | ||
259 | void rcu_defer_unregister_thread(void) | |
260 | { | |
261 | int deferers; | |
262 | ||
263 | internal_urcu_lock(&defer_thread_mutex); | |
264 | internal_urcu_lock(&urcu_defer_mutex); | |
265 | rcu_remove_deferer(pthread_self()); | |
266 | _rcu_defer_barrier_thread(); | |
267 | free(defer_queue.q); | |
268 | defer_queue.q = NULL; | |
269 | deferers = num_deferers; | |
270 | internal_urcu_unlock(&urcu_defer_mutex); | |
271 | ||
272 | if (deferers == 0) | |
273 | stop_defer_thread(); | |
274 | internal_urcu_unlock(&defer_thread_mutex); | |
275 | } | |
276 | ||
277 | void urcu_defer_exit(void) | |
278 | { | |
279 | free(registry); | |
280 | } |