[project @ 2003-07-20 21:28:04 by panne]
[ghc-hetmet.git] / ghc / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Weak.c,v 1.29 2003/03/26 17:43:05 sof 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 #if defined(RTS_SUPPORTS_THREADS)
39   rts_lock();
40 #endif
41   while ((w = weak_ptr_list)) {
42     weak_ptr_list = w->link;
43     if (w->header.info != &stg_DEAD_WEAK_info) {
44         w->header.info = &stg_DEAD_WEAK_info;
45         IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
46         if (w->finalizer != &stg_NO_FINALIZER_closure) {
47 #if defined(RTS_SUPPORTS_THREADS)
48             rts_evalIO(w->finalizer,NULL);
49             rts_unlock();
50             rts_lock();
51 #else
52             rts_mainLazyIO(w->finalizer,NULL);
53 #endif
54         }
55     }
56   }
57 #if defined(RTS_SUPPORTS_THREADS)
58   rts_unlock();
59 #endif
60
61
62 /*
63  * scheduleFinalizers() is called on the list of weak pointers found
64  * to be dead after a garbage collection.  It overwrites each object
65  * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
66  *
67  * This function is called just after GC.  The weak pointers on the
68  * argument list are those whose keys were found to be not reachable,
69  * however the value and finalizer fields have by now been marked live.
70  * The weak pointer object itself may not be alive - i.e. we may be
71  * looking at either an object in from-space or one in to-space.  It
72  * doesn't really matter either way.
73  *
74  * Pre-condition: sched_mutex _not_ held.
75  */
76
77 void
78 scheduleFinalizers(StgWeak *list)
79 {
80     StgWeak *w;
81     StgTSO *t;
82     StgMutArrPtrs *arr;
83     nat n;
84
85     // count number of finalizers, and kill all the weak pointers first...
86     n = 0;
87     for (w = list; w; w = w->link) { 
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 #ifdef PROFILING
98         // A weak pointer is inherently used, so we do not need to call
99         // LDV_recordDead().
100         //
101         // Furthermore, when PROFILING is turned on, dead weak
102         // pointers are exactly as large as weak pointers, so there is
103         // no need to fill the slop, either.  See stg_DEAD_WEAK_info
104         // in StgMiscClosures.hc.
105 #endif
106         SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
107     }
108         
109     // No finalizers to run?
110     if (n == 0) return;
111
112     IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
113
114     arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
115     TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
116     SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
117     arr->ptrs = n;
118
119     n = 0;
120     for (w = list; w; w = w->link) {
121         if (w->finalizer != &stg_NO_FINALIZER_closure) {
122             arr->payload[n] = w->finalizer;
123             n++;
124         }
125     }
126
127     t = createIOThread(RtsFlags.GcFlags.initialStkSize, 
128                        rts_apply(
129                            rts_apply(
130                                (StgClosure *)runFinalizerBatch_closure,
131                                rts_mkInt(n)), 
132                            (StgClosure *)arr)
133         );
134     scheduleThread(t);
135 }