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