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