[project @ 1999-02-11 14:22:53 by simonm]
[ghc-hetmet.git] / ghc / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Weak.c,v 1.8 1999/02/11 14:22:55 simonm Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Weak pointers / finalizers
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #include "Rts.h"
11 #include "RtsAPI.h"
12 #include "RtsFlags.h"
13 #include "Weak.h"
14 #include "Storage.h"
15
16 StgWeak *weak_ptr_list;
17
18 /*
19  * finalizeWeakPointersNow() is called just before the system is shut
20  * down.  It runs the finalizer for each weak pointer still in the
21  * system.
22  */
23
24 void
25 finalizeWeakPointersNow(void)
26 {
27   StgWeak *w;
28
29   for (w = weak_ptr_list; w; w = w->link) {
30     IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
31     w->header.info = &DEAD_WEAK_info;
32     if (w->finalizer != &NO_FINALIZER_closure) {
33       rts_evalIO(w->finalizer,NULL);
34     }
35   }
36
37
38 /*
39  * scheduleFinalizers() is called on the list of weak pointers found
40  * to be dead after a garbage collection.  It overwrites each object
41  * with DEAD_WEAK, and creates a new thread for the finalizer.
42  */
43
44 void
45 scheduleFinalizers(StgWeak *list)
46 {
47   StgWeak *w;
48   
49   for (w = list; w; w = w->link) {
50     IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
51     if (w->finalizer != &NO_FINALIZER_closure) {
52 #ifdef INTERPRETER
53       createGenThread(RtsFlags.GcFlags.initialStkSize, w->finalizer);
54 #else
55       createIOThread(RtsFlags.GcFlags.initialStkSize, w->finalizer);
56 #endif
57     }
58     w->header.info = &DEAD_WEAK_info;
59   }
60 }
61
62 void
63 markWeakList(void)
64 {
65   StgWeak *w, **last_w;
66
67   last_w = &weak_ptr_list;
68   for (w = weak_ptr_list; w; w = w->link) {
69     w = (StgWeak *)MarkRoot((StgClosure *)w);
70     *last_w = w;
71     last_w = &(w->link);
72   }
73 }
74