* guardians.c (scm_guardian_zombify): mark all zombies in a
[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 * Author: Michael N. Livshin
54 * Modified by: Mikael Djurfeldt
55 */
56
57 #include <stdio.h>
58 #include <assert.h>
59
60 #include "_scm.h"
61 #include "ports.h"
62 #include "print.h"
63 #include "smob.h"
64 #include "vectors.h"
65
66 #include "validate.h"
67 #include "guardians.h"
68
69 static long scm_tc16_guardian;
70
71 /* The live and zombies FIFOs are implemented as tconcs as described
72 in Dybvig's paper. This decouples addition and removal of elements
73 so that no synchronization between these needs to take place.
74 */
75 #define TCONC_IN(tc, obj, pair) \
76 do { \
77 SCM_SETCAR ((tc).tail, obj); \
78 SCM_SETCAR (pair, SCM_BOOL_F); \
79 SCM_SETCDR (pair, SCM_EOL); \
80 SCM_SETCDR ((tc).tail, pair); \
81 (tc).tail = pair; \
82 } while (0)
83
84 #define TCONC_OUT(tc, res) \
85 do { \
86 (res) = SCM_CAR ((tc).head); \
87 (tc).head = SCM_CDR ((tc).head); \
88 } while (0)
89
90 #define TCONC_EMPTYP(tc) (SCM_EQ_P ((tc).head, (tc).tail))
91
92 typedef struct tconc_t
93 {
94 SCM head;
95 SCM tail;
96 } tconc_t;
97
98 typedef struct guardian_t
99 {
100 tconc_t live;
101 tconc_t zombies;
102 struct guardian_t *next;
103 } guardian_t;
104
105 #define GUARDIAN(x) ((guardian_t *) SCM_CELL_WORD_1 (x))
106 #define GUARDIAN_LIVE(x) (GUARDIAN (x)->live)
107 #define GUARDIAN_ZOMBIES(x) (GUARDIAN (x)->zombies)
108 #define GUARDIAN_NEXT(x) (GUARDIAN (x)->next)
109
110 #define CCLO_G(cclo) (SCM_VELTS (cclo)[1])
111
112 /* subr constructed from guard below. */
113 static SCM guard1;
114
115 /* this is wrapped in a compiled closure and is the Scheme entry point
116 for each guardian: if arg is an object, it's added to the
117 guardian's live list. if arg is unbound, the next available
118 zombified object (or #f if none) is returned. */
119 static SCM
120 guard (SCM cclo, SCM arg)
121 {
122 if (!SCM_UNBNDP (arg))
123 {
124 scm_guard (cclo, arg);
125 return SCM_UNSPECIFIED;
126 }
127 else
128 return scm_get_one_zombie (cclo);
129 }
130
131 void
132 scm_guard (SCM guardian, SCM obj)
133 {
134 SCM g = CCLO_G (guardian);
135
136 if (SCM_NIMP (obj))
137 {
138 SCM z;
139
140 SCM_NEWCELL (z);
141
142 /* This critical section barrier will be replaced by a mutex. */
143 SCM_DEFER_INTS;
144 TCONC_IN (GUARDIAN_LIVE (g), obj, z);
145 SCM_ALLOW_INTS;
146 }
147 }
148
149 SCM
150 scm_get_one_zombie (SCM guardian)
151 {
152 SCM g = CCLO_G (guardian);
153 SCM res = SCM_BOOL_F;
154
155 /* This critical section barrier will be replaced by a mutex. */
156 SCM_DEFER_INTS;
157 if (!TCONC_EMPTYP (GUARDIAN_ZOMBIES (g)))
158 TCONC_OUT (GUARDIAN_ZOMBIES (g), res);
159 SCM_ALLOW_INTS;
160 return res;
161 }
162
163 SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
164 (),
165 "Create a new guardian.\n"
166 "A guardian protects a set of objects from garbage collection,\n"
167 "allowing a program to apply cleanup or other actions.\n\n"
168
169 "make-guardian returns a procedure representing the guardian.\n"
170 "Calling the guardian procedure with an argument adds the\n"
171 "argument to the guardian's set of protected objects.\n"
172 "Calling the guardian procedure without an argument returns\n"
173 "one of the protected objects which are ready for garbage\n"
174 "collection or @code{#f} if no such object is available.\n"
175 "Objects which are returned in this way are removed from\n"
176 "the guardian.\n\n".
177
178 "See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993)\n"
179 "\"Guardians in a Generation-Based Garbage Collector\".\n"
180 "ACM SIGPLAN Conference on Programming Language Design\n"
181 "and Implementation, June 1993.")
182 #define FUNC_NAME s_scm_make_guardian
183 {
184 SCM cclo = scm_makcclo (guard1, 2L);
185 guardian_t *g = SCM_MUST_MALLOC_TYPE(guardian_t);
186 SCM z1 = scm_cons (SCM_BOOL_F, SCM_EOL);
187 SCM z2 = scm_cons (SCM_BOOL_F, SCM_EOL);
188 SCM z;
189
190 /* A tconc starts out with one tail pair. */
191 g->live.head = g->live.tail = z1;
192 g->zombies.head = g->zombies.tail = z2;
193
194 SCM_NEWSMOB (z, scm_tc16_guardian, g);
195
196 CCLO_G (cclo) = z;
197
198 return cclo;
199 }
200 #undef FUNC_NAME
201
202 /* during the gc mark phase, live guardians are linked into a list
203 here. */
204 static guardian_t *first_live_guardian = NULL;
205 static guardian_t **current_link_field = NULL;
206
207 /* called before gc mark phase begins to initialise the live guardian
208 list. */
209 void
210 scm_guardian_gc_init()
211 {
212 current_link_field = &first_live_guardian;
213 first_live_guardian = NULL;
214 }
215
216 /* mark a guardian by adding it to the live guardian list. */
217 static SCM
218 g_mark (SCM ptr)
219 {
220 *current_link_field = GUARDIAN (ptr);
221 current_link_field = &GUARDIAN_NEXT (ptr);
222 GUARDIAN_NEXT (ptr) = NULL;
223
224 /* the objects protected by the guardian are not marked here: that
225 would prevent them from ever getting collected. instead marking
226 is done at the end of the mark phase by scm_guardian_zombify. */
227 return SCM_BOOL_F;
228 }
229
230 /* this is called by the garbage collector between the mark and sweep
231 phases. for each marked guardian, it moves any unmarked object in
232 its live list (tconc) to its zombie list (tconc). */
233 void scm_guardian_zombify (void)
234 {
235 guardian_t *first_guardian;
236 guardian_t **link_field = &first_live_guardian;
237
238 /* Note that new guardians may be stuck on the end of the live
239 guardian list as we run this loop. As we move unmarked objects
240 to the zombie list and mark them, we may find some guarded
241 guardians. The guardian mark function will stick them on the end
242 of this list, so they'll be processed properly. */
243
244 do {
245 guardian_t *g;
246
247 first_guardian = *link_field;
248 link_field = current_link_field;
249
250 /* first, scan all the guardians that are currently known to be live
251 and move their unmarked objects to zombie lists. */
252
253 for (g = first_guardian; g; g = g->next)
254 {
255 SCM tconc_tail = g->live.tail;
256 SCM *prev_ptr = &g->live.head;
257 SCM pair = g->live.head;
258
259 while (! SCM_EQ_P (pair, tconc_tail))
260 {
261 SCM next_pair = SCM_CDR (pair);
262
263 if (SCM_NMARKEDP (SCM_CAR (pair)))
264 {
265 /* got you, zombie! */
266
267 /* out of the live list! */
268 *prev_ptr = next_pair;
269
270 /* into the zombie list! */
271 TCONC_IN (g->zombies, SCM_CAR (pair), pair);
272 }
273 else
274 prev_ptr = SCM_CDRLOC (pair);
275
276 pair = next_pair;
277 }
278
279 /* Mark the cells of the live list (yes, the cells in the list,
280 even though we don't care about objects pointed to by the list
281 cars, since we know they are already marked). */
282 for (pair = g->live.head; SCM_NIMP (pair); pair = SCM_GCCDR (pair))
283 SCM_SETGCMARK (pair);
284 }
285
286 /* ghouston: Doesn't it seem a bit disturbing that if a zombie
287 is returned to full life after getting returned from the
288 guardian procedure, it may reference objects which are in a
289 guardian's zombie list? Is it not necessary to move such
290 zombies back to the live list, to avoid allowing the
291 guardian procedure to return an object which is referenced,
292 so not collectable? The paper doesn't give this
293 impression.
294
295 cmm: the paper does explicitly say that an object that is
296 guarded more than once should be returned more than once.
297 I believe this covers the above scenario. */
298
299 /* Preserve the zombies in their undead state, by marking to
300 prevent collection. Note that this may uncover zombified
301 guardians -- if so, they'll be processed in the next loop. */
302
303 for (g = first_guardian; g && (!*link_field || g != *link_field); g = g->next)
304 scm_gc_mark (g->zombies.head);
305
306 } while (current_link_field != link_field);
307 }
308
309 /* not generally used, since guardian smob is wrapped in a closure.
310 maybe useful for debugging. */
311 static int
312 g_print (SCM exp, SCM port, scm_print_state *pstate)
313 {
314 char buf[256];
315 sprintf (buf, "#<guardian live objs: %lu zombies: %lu>",
316 scm_ilength (SCM_CDR (GUARDIAN_LIVE (exp).head)),
317 scm_ilength (SCM_CDR (GUARDIAN_ZOMBIES (exp).head)));
318 scm_puts (buf, port);
319
320 return 1;
321 }
322
323 void
324 scm_init_guardian()
325 {
326 scm_tc16_guardian = scm_make_smob_type_mfpe ("guardian", sizeof (guardian_t),
327 g_mark, NULL, g_print, NULL);
328 guard1 = scm_make_subr_opt ("guardian", scm_tc7_subr_2o, guard, 0);
329
330 #include "guardians.x"
331 }
332
333 /*
334 Local Variables:
335 c-file-style: "gnu"
336 End:
337 */