temporarily disable elisp exception tests
[bpt/guile.git] / libguile / guardians.c
1 /* Copyright (C) 1998,1999,2000,2001, 2006, 2008, 2009, 2011,
2 * 2012, 2013 Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20 \f
21 /* This is an implementation of guardians as described in
22 * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
23 * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
24 * Programming Language Design and Implementation, June 1993
25 * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
26 *
27 * Original design: Mikael Djurfeldt
28 * Original implementation: Michael Livshin
29 * Hacked on since by: everybody
30 *
31 * By this point, the semantics are actually quite different from
32 * those described in the abovementioned paper. The semantic changes
33 * are there to improve safety and intuitiveness. The interface is
34 * still (mostly) the one described by the paper, however.
35 *
36 * Boiled down again: Marius Vollmer
37 *
38 * Now they should again behave like those described in the paper.
39 * Scheme guardians should be simple and friendly, not like the greedy
40 * monsters we had...
41 *
42 * Rewritten for the Boehm-Demers-Weiser GC by Ludovic Courtès.
43 */
44
45 /* Uncomment the following line to debug guardian finalization. */
46 /* #define DEBUG_GUARDIANS 1 */
47
48 #ifdef HAVE_CONFIG_H
49 # include <config.h>
50 #endif
51
52 #include "libguile/_scm.h"
53 #include "libguile/ports.h"
54 #include "libguile/print.h"
55 #include "libguile/smob.h"
56 #include "libguile/validate.h"
57 #include "libguile/root.h"
58 #include "libguile/hashtab.h"
59 #include "libguile/deprecation.h"
60 #include "libguile/eval.h"
61
62 #include "libguile/guardians.h"
63 #include "libguile/bdw-gc.h"
64
65
66
67
68 static scm_t_bits tc16_guardian;
69
70 typedef struct t_guardian
71 {
72 scm_i_pthread_mutex_t mutex;
73 unsigned long live;
74 SCM zombies;
75 struct t_guardian *next;
76 } t_guardian;
77
78 #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
79 #define GUARDIAN_DATA(x) ((t_guardian *) SCM_SMOB_DATA_1 (x))
80
81
82
83
84 static int
85 guardian_print (SCM guardian, SCM port, scm_print_state *pstate SCM_UNUSED)
86 {
87 t_guardian *g = GUARDIAN_DATA (guardian);
88
89 scm_puts_unlocked ("#<guardian ", port);
90 scm_uintprint ((scm_t_bits) g, 16, port);
91
92 scm_puts_unlocked (" (reachable: ", port);
93 scm_display (scm_from_uint (g->live), port);
94 scm_puts_unlocked (" unreachable: ", port);
95 scm_display (scm_length (g->zombies), port);
96 scm_puts_unlocked (")", port);
97
98 scm_puts_unlocked (">", port);
99
100 return 1;
101 }
102
103 /* Handle finalization of OBJ which is guarded by the guardians listed in
104 GUARDIAN_LIST. */
105 static void
106 finalize_guarded (void *ptr, void *finalizer_data)
107 {
108 SCM cell_pool;
109 SCM obj, guardian_list, proxied_finalizer;
110
111 obj = SCM_PACK_POINTER (ptr);
112 guardian_list = SCM_CDR (SCM_PACK_POINTER (finalizer_data));
113 proxied_finalizer = SCM_CAR (SCM_PACK_POINTER (finalizer_data));
114
115 #ifdef DEBUG_GUARDIANS
116 printf ("finalizing guarded %p (%u guardians)\n",
117 ptr, scm_to_uint (scm_length (guardian_list)));
118 #endif
119
120 /* Preallocate a bunch of cells so that we can make sure that no garbage
121 collection (and, thus, nested calls to `finalize_guarded ()') occurs
122 while executing the following loop. This is quite inefficient (call to
123 `scm_length ()') but that shouldn't be a problem in most cases. */
124 cell_pool = scm_make_list (scm_length (guardian_list), SCM_UNSPECIFIED);
125
126 /* Tell each guardian interested in OBJ that OBJ is no longer
127 reachable. */
128 for (;
129 !scm_is_null (guardian_list);
130 guardian_list = SCM_CDR (guardian_list))
131 {
132 SCM zombies;
133 SCM guardian;
134 t_guardian *g;
135
136 guardian = scm_c_weak_vector_ref (scm_car (guardian_list), 0);
137
138 if (scm_is_false (guardian))
139 {
140 /* The guardian itself vanished in the meantime. */
141 #ifdef DEBUG_GUARDIANS
142 printf (" guardian for %p vanished\n", ptr);
143 #endif
144 continue;
145 }
146
147 g = GUARDIAN_DATA (guardian);
148
149 scm_i_pthread_mutex_lock (&g->mutex);
150
151 if (g->live == 0)
152 abort ();
153
154 /* Get a fresh cell from CELL_POOL. */
155 zombies = cell_pool;
156 cell_pool = SCM_CDR (cell_pool);
157
158 /* Compute and update G's zombie list. */
159 SCM_SETCAR (zombies, obj);
160 SCM_SETCDR (zombies, g->zombies);
161 g->zombies = zombies;
162
163 g->live--;
164
165 scm_i_pthread_mutex_unlock (&g->mutex);
166 }
167
168 if (scm_is_true (proxied_finalizer))
169 {
170 /* Re-register the finalizer that was in place before we installed this
171 one. */
172 GC_finalization_proc finalizer, prev_finalizer;
173 void *finalizer_data, *prev_finalizer_data;
174
175 finalizer = (GC_finalization_proc) SCM_UNPACK_POINTER (SCM_CAR (proxied_finalizer));
176 finalizer_data = SCM_UNPACK_POINTER (SCM_CDR (proxied_finalizer));
177
178 if (finalizer == NULL)
179 abort ();
180
181 GC_REGISTER_FINALIZER_NO_ORDER (ptr, finalizer, finalizer_data,
182 &prev_finalizer, &prev_finalizer_data);
183
184 #ifdef DEBUG_GUARDIANS
185 printf (" reinstalled proxied finalizer %p for %p\n", finalizer, ptr);
186 #endif
187 }
188
189 #ifdef DEBUG_GUARDIANS
190 printf ("end of finalize (%p)\n", ptr);
191 #endif
192 }
193
194 /* Add OBJ as a guarded object of GUARDIAN. */
195 static void
196 scm_i_guard (SCM guardian, SCM obj)
197 {
198 t_guardian *g = GUARDIAN_DATA (guardian);
199
200 if (SCM_HEAP_OBJECT_P (obj))
201 {
202 /* Register a finalizer and pass a pair as the ``client data''
203 argument. The pair contains in its car `#f' or a pair describing a
204 ``proxied'' finalizer (see below); its cdr contains a list of
205 guardians interested in OBJ.
206
207 A ``proxied'' finalizer is a finalizer that was registered for OBJ
208 before OBJ became guarded (e.g., a SMOB `free' function). We are
209 assuming here that finalizers are only used internally, either at
210 the very beginning of an object's lifetime (e.g., see `SCM_NEWSMOB')
211 or by this function. */
212 GC_finalization_proc prev_finalizer;
213 void *prev_data;
214 SCM guardians_for_obj, finalizer_data;
215
216 scm_i_pthread_mutex_lock (&g->mutex);
217
218 g->live++;
219
220 /* Note: GUARDIANS_FOR_OBJ holds weak references to guardians so
221 that a guardian can be collected before the objects it guards
222 (see `guardians.test'). */
223 guardians_for_obj = scm_cons (scm_make_weak_vector (SCM_INUM1, guardian),
224 SCM_EOL);
225 finalizer_data = scm_cons (SCM_BOOL_F, guardians_for_obj);
226
227 GC_REGISTER_FINALIZER_NO_ORDER (SCM_UNPACK_POINTER (obj), finalize_guarded,
228 SCM_UNPACK_POINTER (finalizer_data),
229 &prev_finalizer, &prev_data);
230
231 if (prev_finalizer == finalize_guarded)
232 {
233 /* OBJ is already guarded by another guardian: add GUARDIAN to its
234 list of guardians. */
235 SCM prev_guardian_list, prev_finalizer_data;
236
237 if (prev_data == NULL)
238 abort ();
239
240 prev_finalizer_data = SCM_PACK_POINTER (prev_data);
241 if (!scm_is_pair (prev_finalizer_data))
242 abort ();
243
244 prev_guardian_list = SCM_CDR (prev_finalizer_data);
245 SCM_SETCDR (guardians_for_obj, prev_guardian_list);
246
247 /* Also copy information about proxied finalizers. */
248 SCM_SETCAR (finalizer_data, SCM_CAR (prev_finalizer_data));
249 }
250 else if (prev_finalizer != NULL)
251 {
252 /* There was already a finalizer registered for OBJ so we will
253 ``proxy'' it, i.e., record it so that we can re-register it once
254 `finalize_guarded ()' has finished. */
255 SCM proxied_finalizer;
256
257 proxied_finalizer = scm_cons (SCM_PACK_POINTER (prev_finalizer),
258 SCM_PACK_POINTER (prev_data));
259 SCM_SETCAR (finalizer_data, proxied_finalizer);
260 }
261
262 scm_i_pthread_mutex_unlock (&g->mutex);
263 }
264 }
265
266 static SCM
267 scm_i_get_one_zombie (SCM guardian)
268 {
269 t_guardian *g = GUARDIAN_DATA (guardian);
270 SCM res = SCM_BOOL_F;
271
272 scm_i_pthread_mutex_lock (&g->mutex);
273
274 if (!scm_is_null (g->zombies))
275 {
276 /* Note: We return zombies in reverse order. */
277 res = SCM_CAR (g->zombies);
278 g->zombies = SCM_CDR (g->zombies);
279 }
280
281 scm_i_pthread_mutex_unlock (&g->mutex);
282
283 return res;
284 }
285
286 /* This is the Scheme entry point for each guardian: If OBJ is an
287 * object, it's added to the guardian's live list. If OBJ is unbound,
288 * the next available unreachable object (or #f if none) is returned.
289 *
290 * If the second optional argument THROW_P is true (the default), then
291 * an error is raised if GUARDIAN is greedy and OBJ is already greedily
292 * guarded. If THROW_P is false, #f is returned instead of raising the
293 * error, and #t is returned if everything is fine.
294 */
295 static SCM
296 guardian_apply (SCM guardian, SCM obj, SCM throw_p)
297 {
298 if (!SCM_UNBNDP (obj))
299 {
300 scm_i_guard (guardian, obj);
301 return SCM_UNSPECIFIED;
302 }
303 else
304 return scm_i_get_one_zombie (guardian);
305 }
306
307 SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
308 (),
309 "Create a new guardian. A guardian protects a set of objects from\n"
310 "garbage collection, allowing a program to apply cleanup or other\n"
311 "actions.\n"
312 "\n"
313 "@code{make-guardian} returns a procedure representing the guardian.\n"
314 "Calling the guardian procedure with an argument adds the argument to\n"
315 "the guardian's set of protected objects. Calling the guardian\n"
316 "procedure without an argument returns one of the protected objects\n"
317 "which are ready for garbage collection, or @code{#f} if no such object\n"
318 "is available. Objects which are returned in this way are removed from\n"
319 "the guardian.\n"
320 "\n"
321 "You can put a single object into a guardian more than once and you can\n"
322 "put a single object into more than one guardian. The object will then\n"
323 "be returned multiple times by the guardian procedures.\n"
324 "\n"
325 "An object is eligible to be returned from a guardian when it is no\n"
326 "longer referenced from outside any guardian.\n"
327 "\n"
328 "There is no guarantee about the order in which objects are returned\n"
329 "from a guardian. If you want to impose an order on finalization\n"
330 "actions, for example, you can do that by keeping objects alive in some\n"
331 "global data structure until they are no longer needed for finalizing\n"
332 "other objects.\n"
333 "\n"
334 "Being an element in a weak vector, a key in a hash table with weak\n"
335 "keys, or a value in a hash table with weak value does not prevent an\n"
336 "object from being returned by a guardian. But as long as an object\n"
337 "can be returned from a guardian it will not be removed from such a\n"
338 "weak vector or hash table. In other words, a weak link does not\n"
339 "prevent an object from being considered collectable, but being inside\n"
340 "a guardian prevents a weak link from being broken.\n"
341 "\n"
342 "A key in a weak key hash table can be though of as having a strong\n"
343 "reference to its associated value as long as the key is accessible.\n"
344 "Consequently, when the key only accessible from within a guardian, the\n"
345 "reference from the key to the value is also considered to be coming\n"
346 "from within a guardian. Thus, if there is no other reference to the\n"
347 "value, it is eligible to be returned from a guardian.\n")
348 #define FUNC_NAME s_scm_make_guardian
349 {
350 t_guardian *g = scm_gc_malloc (sizeof (t_guardian), "guardian");
351 SCM z;
352
353 scm_i_pthread_mutex_init (&g->mutex, NULL);
354
355 /* A tconc starts out with one tail pair. */
356 g->live = 0;
357 g->zombies = SCM_EOL;
358
359 g->next = NULL;
360
361 SCM_NEWSMOB (z, tc16_guardian, g);
362
363 return z;
364 }
365 #undef FUNC_NAME
366
367 void
368 scm_init_guardians ()
369 {
370 /* We use unordered finalization `a la Java. */
371 GC_set_java_finalization (1);
372
373 tc16_guardian = scm_make_smob_type ("guardian", 0);
374
375 scm_set_smob_print (tc16_guardian, guardian_print);
376 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0);
377
378 #include "libguile/guardians.x"
379 }
380
381 /*
382 Local Variables:
383 c-file-style: "gnu"
384 End:
385 */