[project @ 2001-11-26 16:54:21 by simonmar]
[ghc-hetmet.git] / ghc / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Weak.c,v 1.20 2001/11/26 16:54:22 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Weak pointers / finalizers
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #include "PosixSource.h"
11 #include "Rts.h"
12 #include "RtsAPI.h"
13 #include "SchedAPI.h"
14 #include "RtsFlags.h"
15 #include "Weak.h"
16 #include "Storage.h"
17 #include "Prelude.h"
18
19 StgWeak *weak_ptr_list;
20
21 /*
22  * finalizeWeakPointersNow() is called just before the system is shut
23  * down.  It runs the finalizer for each weak pointer still in the
24  * system.
25  *
26  * Careful here - rts_evalIO might cause a garbage collection, which
27  * might change weak_ptr_list.  Must re-load weak_ptr_list each time
28  * around the loop.
29  */
30
31 void
32 finalizeWeakPointersNow(void)
33 {
34   StgWeak *w;
35   
36   while ((w = weak_ptr_list)) {
37     weak_ptr_list = w->link;
38     if (w->header.info != &stg_DEAD_WEAK_info) {
39         w->header.info = &stg_DEAD_WEAK_info;
40         IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
41         if (w->finalizer != &stg_NO_FINALIZER_closure) {
42             rts_evalIO(w->finalizer,NULL);
43         }
44     }
45   }
46
47
48 /*
49  * scheduleFinalizers() is called on the list of weak pointers found
50  * to be dead after a garbage collection.  It overwrites each object
51  * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
52  *
53  * This function is called just after GC.  The weak pointers on the
54  * argument list are those whose keys were found to be not reachable,
55  * however the value and finalizer fields have by now been marked live.
56  * The weak pointer object itself may not be alive - i.e. we may be
57  * looking at either an object in from-space or one in to-space.  It
58  * doesn't really matter either way.
59  */
60
61 void
62 scheduleFinalizers(StgWeak *list)
63 {
64     StgWeak *w;
65     StgTSO *t;
66     StgMutArrPtrs *arr;
67     nat n;
68
69     /* count number of finalizers first... */
70     for (n = 0, w = list; w; w = w->link) { 
71         if (w->finalizer != &stg_NO_FINALIZER_closure)
72             n++;
73     }
74         
75     if (n == 0) return;
76
77     IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
78
79     arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
80     SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
81     arr->ptrs = n;
82
83     for (n = 0, w = list; w; w = w->link) {
84         if (w->finalizer != &stg_NO_FINALIZER_closure) {
85             arr->payload[n] = w->finalizer;
86             n++;
87         }
88
89 #ifdef PROFILING
90         // A weak pointer is inherently used, so we do not need to call
91         // LDV_recordDead().
92         //
93         // Furthermore, when PROFILING is turned on, dead weak
94         // pointers are exactly as large as weak pointers, so there is
95         // no need to fill the slop, either.  See stg_DEAD_WEAK_info
96         // in StgMiscClosures.hc.
97 #endif
98         SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
99     }
100
101     t = createIOThread(RtsFlags.GcFlags.initialStkSize, 
102                        rts_apply(
103                            rts_apply(
104                                (StgClosure *)runFinalizerBatch_closure,
105                                rts_mkInt(n)), 
106                            (StgClosure *)arr)
107         );
108     scheduleThread(t);
109 }