* guardians.c (guardian_print): for sharing guardians, print that
[bpt/guile.git] / libguile / guardians.c
1 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 /* This is an implementation of guardians as described in
48 * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
49 * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
50 * Programming Language Design and Implementation, June 1993
51 * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
52 *
53 * By this point, the semantics are actually quite different from
54 * those described in the abovementioned paper. The semantic changes
55 * are there to improve safety and intuitiveness. The interface is
56 * still (mostly) the one described by the paper, however.
57 *
58 * Original design: Mikael Djurfeldt
59 * Original implementation: Michael Livshin
60 * Hacked on since by: everybody
61 */
62
63
64 #include "libguile/_scm.h"
65 #include "libguile/ports.h"
66 #include "libguile/print.h"
67 #include "libguile/smob.h"
68 #include "libguile/validate.h"
69 #include "libguile/root.h"
70 #include "libguile/hashtab.h"
71 #include "libguile/weaks.h"
72
73 #include "libguile/guardians.h"
74
75
76 /* The live and zombies FIFOs are implemented as tconcs as described
77 in Dybvig's paper. This decouples addition and removal of elements
78 so that no synchronization between these needs to take place.
79 */
80
81 typedef struct tconc_t
82 {
83 SCM head;
84 SCM tail;
85 } tconc_t;
86
87 #define TCONC_EMPTYP(tc) (SCM_EQ_P ((tc).head, (tc).tail))
88
89 #define TCONC_IN(tc, obj, pair) \
90 do { \
91 SCM_SETCAR ((tc).tail, obj); \
92 SCM_SETCAR (pair, SCM_BOOL_F); \
93 SCM_SETCDR (pair, SCM_EOL); \
94 SCM_SETCDR ((tc).tail, pair); \
95 (tc).tail = pair; \
96 } while (0)
97
98 #define TCONC_OUT(tc, res) \
99 do { \
100 (res) = SCM_CAR ((tc).head); \
101 (tc).head = SCM_CDR ((tc).head); \
102 } while (0)
103
104
105 static scm_bits_t tc16_guardian;
106
107 typedef struct guardian_t
108 {
109 tconc_t live;
110 tconc_t zombies;
111 struct guardian_t *next;
112 int greedy_p;
113 int listed_p;
114 } guardian_t;
115
116 #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
117 #define GUARDIAN(x) ((guardian_t *) SCM_CELL_WORD_1 (x))
118 #define GUARDIAN_LIVE(x) (GUARDIAN (x)->live)
119 #define GUARDIAN_ZOMBIES(x) (GUARDIAN (x)->zombies)
120 #define GUARDIAN_NEXT(x) (GUARDIAN (x)->next)
121 #define GUARDIAN_GREEDY_P(x) (GUARDIAN (x)->greedy_p)
122 #define GUARDIAN_LISTED_P(x) (GUARDIAN (x)->listed_p)
123
124
125 /* during the gc mark phase, live guardians are linked into the lists
126 here. */
127 static guardian_t *greedy_guardians = NULL;
128 static guardian_t *sharing_guardians = NULL;
129
130 static SCM greedily_guarded_whash = SCM_EOL;
131
132 /* this is the list of guarded objects that are parts of cycles. we
133 don't know in which order to return them from guardians, so we just
134 unguard them and whine about it in after-gc-hook */
135 static SCM self_centered_zombies = SCM_EOL;
136
137
138 static void
139 add_to_live_list (SCM g)
140 {
141 if (GUARDIAN_LISTED_P (g))
142 return;
143
144 if (GUARDIAN_GREEDY_P (g))
145 {
146 GUARDIAN_NEXT (g) = greedy_guardians;
147 greedy_guardians = GUARDIAN (g);
148 }
149 else
150 {
151 GUARDIAN_NEXT (g) = sharing_guardians;
152 sharing_guardians = GUARDIAN (g);
153 }
154
155 GUARDIAN_LISTED_P (g) = 1;
156 }
157
158 /* mark a guardian by adding it to the live guardian list. */
159 static SCM
160 guardian_mark (SCM ptr)
161 {
162 add_to_live_list (ptr);
163
164 /* the objects protected by the guardian are not marked here: that
165 would prevent them from ever getting collected. instead marking
166 is done at the end of the mark phase by guardian_zombify. */
167 return SCM_BOOL_F;
168 }
169
170
171 static scm_sizet
172 guardian_free (SCM ptr)
173 {
174 scm_must_free (GUARDIAN (ptr));
175 return sizeof (guardian_t);
176 }
177
178
179 static int
180 guardian_print (SCM g, SCM port, scm_print_state *pstate)
181 {
182 scm_puts ("#<", port);
183 if (GUARDIAN_GREEDY_P (g))
184 scm_puts ("greedy ", port);
185 else
186 scm_puts ("sharing ", port);
187 scm_puts ("guardian (reachable: ", port);
188 scm_display (scm_length (SCM_CDR (GUARDIAN_LIVE (g).head)), port);
189 scm_puts (" unreachable: ", port);
190 scm_display (scm_length (SCM_CDR (GUARDIAN_ZOMBIES (g).head)), port);
191 scm_puts (")>", port);
192
193 return 1;
194 }
195
196
197 /* This is the Scheme entry point for each guardian: If arg is an object, it's
198 * added to the guardian's live list. If arg is unbound, the next available
199 * zombified object (or #f if none) is returned.
200 */
201 static SCM
202 guardian_apply (SCM guardian, SCM arg)
203 {
204 if (!SCM_UNBNDP (arg))
205 {
206 scm_guard (guardian, arg);
207 return SCM_UNSPECIFIED;
208 }
209 else
210 return scm_get_one_zombie (guardian);
211 }
212
213
214 void
215 scm_guard (SCM guardian, SCM obj)
216 {
217 if (!SCM_IMP (obj))
218 {
219 SCM z;
220
221 SCM_NEWCELL (z);
222
223 /* This critical section barrier will be replaced by a mutex. */
224 SCM_DEFER_INTS;
225
226 if (GUARDIAN_GREEDY_P (guardian))
227 {
228 if (SCM_NFALSEP (scm_hashq_get_handle
229 (greedily_guarded_whash, obj)))
230 {
231 SCM_ALLOW_INTS;
232 scm_misc_error ("guard",
233 "object is already greedily guarded", obj);
234 }
235 else
236 scm_hashq_create_handle_x (greedily_guarded_whash,
237 obj, guardian);
238 }
239
240 TCONC_IN (GUARDIAN_LIVE (guardian), obj, z);
241
242 SCM_ALLOW_INTS;
243 }
244
245 }
246
247
248 SCM
249 scm_get_one_zombie (SCM guardian)
250 {
251 SCM res = SCM_BOOL_F;
252
253 /* This critical section barrier will be replaced by a mutex. */
254 SCM_DEFER_INTS;
255
256 if (!TCONC_EMPTYP (GUARDIAN_ZOMBIES (guardian)))
257 TCONC_OUT (GUARDIAN_ZOMBIES (guardian), res);
258
259 if (SCM_NFALSEP (res)
260 && GUARDIAN_GREEDY_P (guardian)
261 && SCM_NFALSEP (scm_hashq_get_handle
262 (greedily_guarded_whash, res)))
263 scm_hashq_remove_x (greedily_guarded_whash, res);
264
265 SCM_ALLOW_INTS;
266
267 return res;
268 }
269
270
271 SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 1, 0,
272 (SCM greedy_p),
273 "Create a new guardian.\n"
274 "A guardian protects a set of objects from garbage collection,\n"
275 "allowing a program to apply cleanup or other actions.\n\n"
276
277 "make-guardian returns a procedure representing the guardian.\n"
278 "Calling the guardian procedure with an argument adds the\n"
279 "argument to the guardian's set of protected objects.\n"
280 "Calling the guardian procedure without an argument returns\n"
281 "one of the protected objects which are ready for garbage\n"
282 "collection or @code{#f} if no such object is available.\n"
283 "Objects which are returned in this way are removed from\n"
284 "the guardian.\n\n"
285
286 "make-guardian takes one optional argument that says whether the\n"
287 "new guardian should be greedy or not. if there is any chance\n"
288 "that any object protected by the guardian may be resurrected,\n"
289 "then make the guardian greedy (this is the default).\n\n"
290
291 "See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993)\n"
292 "\"Guardians in a Generation-Based Garbage Collector\".\n"
293 "ACM SIGPLAN Conference on Programming Language Design\n"
294 "and Implementation, June 1993.\n\n"
295
296 "(the semantics are slightly different at this point, but the\n"
297 "paper still (mostly) accurately describes the interface).")
298 #define FUNC_NAME s_scm_make_guardian
299 {
300 guardian_t *g = SCM_MUST_MALLOC_TYPE (guardian_t);
301 SCM z1 = scm_cons (SCM_BOOL_F, SCM_EOL);
302 SCM z2 = scm_cons (SCM_BOOL_F, SCM_EOL);
303 SCM z;
304
305 /* A tconc starts out with one tail pair. */
306 g->live.head = g->live.tail = z1;
307 g->zombies.head = g->zombies.tail = z2;
308 g->listed_p = 0;
309
310 if (SCM_UNBNDP (greedy_p))
311 g->greedy_p = 1;
312 else
313 g->greedy_p = SCM_NFALSEP (greedy_p);
314
315 SCM_NEWSMOB (z, tc16_guardian, g);
316
317 return z;
318 }
319 #undef FUNC_NAME
320
321
322 /* called before gc mark phase begins to initialise the live guardian list. */
323 static void *
324 guardian_gc_init (void *dummy1, void *dummy2, void *dummy3)
325 {
326 greedy_guardians = sharing_guardians = NULL;
327
328 return 0;
329 }
330
331 static void
332 mark_dependencies_in_tconc (tconc_t *tc)
333 {
334 SCM pair, next_pair;
335 SCM *prev_ptr;
336
337 /* scan the list for unmarked objects, and mark their
338 dependencies */
339 for (pair = tc->head, prev_ptr = &tc->head;
340 ! SCM_EQ_P (pair, tc->tail);
341 pair = next_pair)
342 {
343 SCM obj = SCM_CAR (pair);
344 next_pair = SCM_CDR (pair);
345
346 if (! SCM_MARKEDP (obj))
347 {
348 /* a candidate for finalizing */
349 scm_gc_mark_dependencies (obj);
350
351 if (SCM_MARKEDP (obj))
352 {
353 /* uh oh. a cycle. transfer this object (the
354 spine cell, to be exact) to
355 self_centered_zombies, so we'll be able to
356 complain about it later. */
357 *prev_ptr = next_pair;
358 SCM_SETGCMARK (pair);
359 SCM_SETCDR (pair, SCM_CDR (self_centered_zombies));
360 SCM_SETCDR (self_centered_zombies, pair);
361 }
362 else
363 {
364 /* see if this is a guardian. if yes, list it (but don't
365 mark it yet). */
366 if (GUARDIAN_P (obj))
367 add_to_live_list (obj);
368
369 prev_ptr = SCM_CDRLOC (pair);
370 }
371 }
372 }
373 }
374
375 static void
376 mark_dependencies (guardian_t *g)
377 {
378 mark_dependencies_in_tconc (&g->zombies);
379 mark_dependencies_in_tconc (&g->live);
380 }
381
382 static void
383 mark_and_zombify (guardian_t *g)
384 {
385 SCM tconc_tail = g->live.tail;
386 SCM *prev_ptr = &g->live.head;
387 SCM pair = g->live.head;
388
389 while (! SCM_EQ_P (pair, tconc_tail))
390 {
391 SCM next_pair = SCM_CDR (pair);
392
393 if (SCM_NMARKEDP (SCM_CAR (pair)))
394 {
395 /* got you, zombie! */
396
397 /* out of the live list! */
398 *prev_ptr = next_pair;
399
400 if (g->greedy_p)
401 /* if the guardian is greedy, mark this zombie now. this
402 way it won't be zombified again this time around. */
403 SCM_SETGCMARK (SCM_CAR (pair));
404
405 /* into the zombie list! */
406 TCONC_IN (g->zombies, SCM_CAR (pair), pair);
407 }
408 else
409 prev_ptr = SCM_CDRLOC (pair);
410
411 pair = next_pair;
412 }
413
414 /* Mark the cells of the live list (yes, the cells in the list, we
415 don't care about objects pointed to by the list cars, since we
416 know they are already marked). */
417 for (pair = g->live.head; !SCM_NULLP (pair); pair = SCM_CDR (pair))
418 SCM_SETGCMARK (pair);
419 }
420
421
422 /* this is called by the garbage collector between the mark and sweep
423 phases. for each marked guardian, it moves any unmarked object in
424 its live list (tconc) to its zombie list (tconc). */
425 static void *
426 guardian_zombify (void *dummy1, void *dummy2, void *dummy3)
427 {
428 guardian_t *last_greedy_guardian = NULL;
429 guardian_t *last_sharing_guardian = NULL;
430 guardian_t *first_greedy_guardian = NULL;
431 guardian_t *first_sharing_guardian = NULL;
432 guardian_t *g;
433
434 /* First, find all newly unreachable objects and mark their
435 dependencies.
436
437 Note that new guardians may be stuck on the end of the live
438 guardian lists as we run this loop, since guardians might be
439 guarded too. When we mark a guarded guardian, its mark function
440 sticks in the appropriate live guardian list. The loop
441 terminates when no new guardians are found. */
442
443 do {
444 first_greedy_guardian = greedy_guardians;
445 first_sharing_guardian = sharing_guardians;
446
447 for (g = greedy_guardians; g != last_greedy_guardian;
448 g = g->next)
449 mark_dependencies (g);
450 for (g = sharing_guardians; g != last_sharing_guardian;
451 g = g->next)
452 mark_dependencies (g);
453
454 last_greedy_guardian = first_greedy_guardian;
455 last_sharing_guardian = first_sharing_guardian;
456 } while (first_greedy_guardian != greedy_guardians
457 || first_sharing_guardian != sharing_guardians);
458
459 /* now, scan all the guardians that are currently known to be live
460 and move their unmarked objects to zombie lists. */
461
462 for (g = greedy_guardians; g; g = g->next)
463 {
464 mark_and_zombify (g);
465 g->listed_p = 0;
466 }
467 for (g = sharing_guardians; g; g = g->next)
468 {
469 mark_and_zombify (g);
470 g->listed_p = 0;
471 }
472
473 /* Preserve the zombies in their undead state, by marking to prevent
474 collection. */
475 for (g = greedy_guardians; g; g = g->next)
476 scm_gc_mark (g->zombies.head);
477 for (g = sharing_guardians; g; g = g->next)
478 scm_gc_mark (g->zombies.head);
479
480 return 0;
481 }
482
483 static void *
484 whine_about_self_centered_zombies (void *dummy1, void *dummy2, void *dummy3)
485 {
486 if (! SCM_NULLP (SCM_CDR (self_centered_zombies)))
487 {
488 SCM pair;
489
490 scm_puts ("** WARNING: the following guarded objects were unguarded due to cycles:",
491 scm_cur_errp);
492 scm_newline (scm_cur_errp);
493 for (pair = SCM_CDR (self_centered_zombies);
494 ! SCM_NULLP (pair); pair = SCM_CDR (pair))
495 {
496 scm_display (SCM_CAR (pair), scm_cur_errp);
497 scm_newline (scm_cur_errp);
498 }
499
500 SCM_SETCDR (self_centered_zombies, SCM_EOL);
501 }
502
503 return 0;
504 }
505
506 void
507 scm_init_guardians ()
508 {
509 tc16_guardian = scm_make_smob_type ("guardian", 0);
510 scm_set_smob_mark (tc16_guardian, guardian_mark);
511 scm_set_smob_free (tc16_guardian, guardian_free);
512 scm_set_smob_print (tc16_guardian, guardian_print);
513 scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0);
514
515 scm_c_hook_add (&scm_before_mark_c_hook, guardian_gc_init, 0, 0);
516 scm_c_hook_add (&scm_before_sweep_c_hook, guardian_zombify, 0, 0);
517
518 self_centered_zombies =
519 scm_permanent_object (scm_cons (SCM_UNDEFINED, SCM_EOL));
520 scm_c_hook_add (&scm_after_gc_c_hook,
521 whine_about_self_centered_zombies, 0, 0);
522
523 greedily_guarded_whash =
524 scm_permanent_object (scm_make_doubly_weak_hash_table (SCM_MAKINUM (31)));
525
526 #ifndef SCM_MAGIC_SNARFER
527 #include "libguile/guardians.x"
528 #endif
529 }
530
531 /*
532 Local Variables:
533 c-file-style: "gnu"
534 End:
535 */