[project @ 1999-01-13 17:25:37 by simonm]
[ghc-hetmet.git] / ghc / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Weak.c,v 1.3 1999/01/13 17:25:49 simonm Exp $
3  *
4  * Weak pointers / finalisers
5  *
6  * ---------------------------------------------------------------------------*/
7
8 #include "Rts.h"
9 #include "RtsAPI.h"
10 #include "RtsFlags.h"
11 #include "Weak.h"
12 #include "Storage.h"
13
14 StgWeak *weak_ptr_list;
15
16 /*
17  * finaliseWeakPointersNow() is called just before the system is shut
18  * down.  It runs the finaliser for each weak pointer still in the
19  * system.
20  */
21
22 void
23 finaliseWeakPointersNow(void)
24 {
25   StgWeak *w;
26
27   for (w = weak_ptr_list; w; w = w->link) {
28     IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p\n", w));
29     w->header.info = &DEAD_WEAK_info;
30     rts_evalIO(w->finaliser,NULL);
31   }
32
33
34 /*
35  * scheduleFinalisers() is called on the list of weak pointers found
36  * to be dead after a garbage collection.  It overwrites each object
37  * with DEAD_WEAK, and creates a new thread for the finaliser.
38  */
39
40 void
41 scheduleFinalisers(StgWeak *list)
42 {
43   StgWeak *w;
44   
45   for (w = list; w; w = w->link) {
46     IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p\n", w));
47 #ifdef INTERPRETER
48     createGenThread(RtsFlags.GcFlags.initialStkSize, w->finaliser);
49 #else
50     createIOThread(RtsFlags.GcFlags.initialStkSize, w->finaliser);
51 #endif
52     w->header.info = &DEAD_WEAK_info;
53
54     /* need to fill the slop with zeros if we're sanity checking */
55     IF_DEBUG(sanity, {
56       nat dw_size = sizeW_fromITBL(get_itbl(w));
57       memset((P_)w + dw_size, 0, (sizeofW(StgWeak) - dw_size) * sizeof(W_));
58     });
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