New tracing interface
[ghc-hetmet.git] / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-1999
4  *
5  * Weak pointers / finalizers
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #define COMPILING_RTS_MAIN
11 #include "Rts.h"
12 #include "RtsUtils.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 #include "Trace.h"
21
22 StgWeak *weak_ptr_list;
23
24 /*
25  * scheduleFinalizers() is called on the list of weak pointers found
26  * to be dead after a garbage collection.  It overwrites each object
27  * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
28  *
29  * This function is called just after GC.  The weak pointers on the
30  * argument list are those whose keys were found to be not reachable,
31  * however the value and finalizer fields have by now been marked live.
32  * The weak pointer object itself may not be alive - i.e. we may be
33  * looking at either an object in from-space or one in to-space.  It
34  * doesn't really matter either way.
35  *
36  * Pre-condition: sched_mutex _not_ held.
37  */
38
39 void
40 scheduleFinalizers(Capability *cap, StgWeak *list)
41 {
42     StgWeak *w;
43     StgTSO *t;
44     StgMutArrPtrs *arr;
45     nat n;
46
47     // count number of finalizers, and kill all the weak pointers first...
48     n = 0;
49     for (w = list; w; w = w->link) { 
50
51         // Better not be a DEAD_WEAK at this stage; the garbage
52         // collector removes DEAD_WEAKs from the weak pointer list.
53         ASSERT(w->header.info != &stg_DEAD_WEAK_info);
54
55         if (w->finalizer != &stg_NO_FINALIZER_closure) {
56             n++;
57         }
58
59 #ifdef PROFILING
60         // A weak pointer is inherently used, so we do not need to call
61         // LDV_recordDead().
62         //
63         // Furthermore, when PROFILING is turned on, dead weak
64         // pointers are exactly as large as weak pointers, so there is
65         // no need to fill the slop, either.  See stg_DEAD_WEAK_info
66         // in StgMiscClosures.hc.
67 #endif
68         SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
69     }
70         
71     // No finalizers to run?
72     if (n == 0) return;
73
74     debugTrace(DEBUG_weak, "weak: batching %d finalizers", n);
75
76     arr = (StgMutArrPtrs *)allocateLocal(cap, sizeofW(StgMutArrPtrs) + n);
77     TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
78     SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
79     arr->ptrs = n;
80
81     n = 0;
82     for (w = list; w; w = w->link) {
83         if (w->finalizer != &stg_NO_FINALIZER_closure) {
84             arr->payload[n] = w->finalizer;
85             n++;
86         }
87     }
88
89     t = createIOThread(cap, 
90                        RtsFlags.GcFlags.initialStkSize, 
91                        rts_apply(cap,
92                            rts_apply(cap,
93                                (StgClosure *)runFinalizerBatch_closure,
94                                rts_mkInt(cap,n)), 
95                            (StgClosure *)arr)
96         );
97     scheduleThread(cap,t);
98 }