94bead37527ef6a3c76e1c17221f4b175e8ecaf2
[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     StgWord size;
80     nat n, i;
81
82     running_finalizers = rtsTrue;
83
84     // count number of finalizers, and kill all the weak pointers first...
85     n = 0;
86     for (w = list; w; w = w->link) { 
87         StgArrWords *farr;
88
89         // Better not be a DEAD_WEAK at this stage; the garbage
90         // collector removes DEAD_WEAKs from the weak pointer list.
91         ASSERT(w->header.info != &stg_DEAD_WEAK_info);
92
93         if (w->finalizer != &stg_NO_FINALIZER_closure) {
94             n++;
95         }
96
97         farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer);
98
99         if ((StgClosure *)farr != &stg_NO_FINALIZER_closure)
100             runCFinalizer((void *)farr->payload[0],
101                           (void *)farr->payload[1],
102                           (void *)farr->payload[2],
103                           farr->payload[3]);
104
105 #ifdef PROFILING
106         // A weak pointer is inherently used, so we do not need to call
107         // LDV_recordDead().
108         //
109         // Furthermore, when PROFILING is turned on, dead weak
110         // pointers are exactly as large as weak pointers, so there is
111         // no need to fill the slop, either.  See stg_DEAD_WEAK_info
112         // in StgMiscClosures.hc.
113 #endif
114         SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
115     }
116         
117     running_finalizers = rtsFalse;
118
119     // No finalizers to run?
120     if (n == 0) return;
121
122     debugTrace(DEBUG_weak, "weak: batching %d finalizers", n);
123
124     size = n + mutArrPtrsCardTableSize(n);
125     arr = (StgMutArrPtrs *)allocate(cap, sizeofW(StgMutArrPtrs) + size);
126     TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
127     SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
128     arr->ptrs = n;
129     arr->size = size;
130
131     n = 0;
132     for (w = list; w; w = w->link) {
133         if (w->finalizer != &stg_NO_FINALIZER_closure) {
134             arr->payload[n] = w->finalizer;
135             n++;
136         }
137     }
138     // set all the cards to 1
139     for (i = n; i < size; i++) {
140         arr->payload[i] = (StgClosure *)(W_)(-1);
141     }
142
143     t = createIOThread(cap, 
144                        RtsFlags.GcFlags.initialStkSize, 
145                        rts_apply(cap,
146                            rts_apply(cap,
147                                (StgClosure *)runFinalizerBatch_closure,
148                                rts_mkInt(cap,n)), 
149                            (StgClosure *)arr)
150         );
151     scheduleThread(cap,t);
152 }