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