RTS tidyup sweep, first phase
[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 #include "Rts.h"
11 #include "RtsAPI.h"
12
13 #include "RtsUtils.h"
14 #include "Weak.h"
15 #include "Schedule.h"
16 #include "Prelude.h"
17 #include "Trace.h"
18
19 // ForeignPtrs with C finalizers rely on weak pointers inside weak_ptr_list
20 // to always be in the same order.
21
22 StgWeak *weak_ptr_list;
23
24 // So that we can detect when a finalizer illegally calls back into Haskell
25 rtsBool running_finalizers = rtsFalse;
26
27 void
28 runCFinalizer(void *fn, void *ptr, void *env, StgWord flag)
29 {
30     if (flag)
31         ((void (*)(void *, void *))fn)(env, ptr);
32     else
33         ((void (*)(void *))fn)(ptr);
34 }
35
36 void
37 runAllCFinalizers(StgWeak *list)
38 {
39     StgWeak *w;
40
41     running_finalizers = rtsTrue;
42
43     for (w = list; w; w = w->link) {
44         StgArrWords *farr;
45
46         farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer);
47
48         if ((StgClosure *)farr != &stg_NO_FINALIZER_closure)
49             runCFinalizer((void *)farr->payload[0],
50                           (void *)farr->payload[1],
51                           (void *)farr->payload[2],
52                           farr->payload[3]);
53     }
54
55     running_finalizers = rtsFalse;
56 }
57
58 /*
59  * scheduleFinalizers() is called on the list of weak pointers found
60  * to be dead after a garbage collection.  It overwrites each object
61  * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
62  *
63  * This function is called just after GC.  The weak pointers on the
64  * argument list are those whose keys were found to be not reachable,
65  * however the value and finalizer fields have by now been marked live.
66  * The weak pointer object itself may not be alive - i.e. we may be
67  * looking at either an object in from-space or one in to-space.  It
68  * doesn't really matter either way.
69  *
70  * Pre-condition: sched_mutex _not_ held.
71  */
72
73 void
74 scheduleFinalizers(Capability *cap, StgWeak *list)
75 {
76     StgWeak *w;
77     StgTSO *t;
78     StgMutArrPtrs *arr;
79     nat n;
80
81     running_finalizers = rtsTrue;
82
83     // count number of finalizers, and kill all the weak pointers first...
84     n = 0;
85     for (w = list; w; w = w->link) { 
86         StgArrWords *farr;
87
88         // Better not be a DEAD_WEAK at this stage; the garbage
89         // collector removes DEAD_WEAKs from the weak pointer list.
90         ASSERT(w->header.info != &stg_DEAD_WEAK_info);
91
92         if (w->finalizer != &stg_NO_FINALIZER_closure) {
93             n++;
94         }
95
96         farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer);
97
98         if ((StgClosure *)farr != &stg_NO_FINALIZER_closure)
99             runCFinalizer((void *)farr->payload[0],
100                           (void *)farr->payload[1],
101                           (void *)farr->payload[2],
102                           farr->payload[3]);
103
104 #ifdef PROFILING
105         // A weak pointer is inherently used, so we do not need to call
106         // LDV_recordDead().
107         //
108         // Furthermore, when PROFILING is turned on, dead weak
109         // pointers are exactly as large as weak pointers, so there is
110         // no need to fill the slop, either.  See stg_DEAD_WEAK_info
111         // in StgMiscClosures.hc.
112 #endif
113         SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
114     }
115         
116     running_finalizers = rtsFalse;
117
118     // No finalizers to run?
119     if (n == 0) return;
120
121     debugTrace(DEBUG_weak, "weak: batching %d finalizers", n);
122
123     arr = (StgMutArrPtrs *)allocateLocal(cap, sizeofW(StgMutArrPtrs) + n);
124     TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
125     SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
126     arr->ptrs = n;
127
128     n = 0;
129     for (w = list; w; w = w->link) {
130         if (w->finalizer != &stg_NO_FINALIZER_closure) {
131             arr->payload[n] = w->finalizer;
132             n++;
133         }
134     }
135
136     t = createIOThread(cap, 
137                        RtsFlags.GcFlags.initialStkSize, 
138                        rts_apply(cap,
139                            rts_apply(cap,
140                                (StgClosure *)runFinalizerBatch_closure,
141                                rts_mkInt(cap,n)), 
142                            (StgClosure *)arr)
143         );
144     scheduleThread(cap,t);
145 }