[project @ 2000-06-20 15:18:40 by simonmar]
[ghc-hetmet.git] / ghc / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Weak.c,v 1.15 2000/06/20 15:18:40 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Weak pointers / finalizers
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #include "Rts.h"
11 #include "RtsAPI.h"
12 #include "SchedAPI.h"
13 #include "RtsFlags.h"
14 #include "Weak.h"
15 #include "Storage.h"
16 #include "Prelude.h"
17
18 StgWeak *weak_ptr_list;
19
20 /*
21  * finalizeWeakPointersNow() is called just before the system is shut
22  * down.  It runs the finalizer for each weak pointer still in the
23  * system.
24  *
25  * Careful here - rts_evalIO might cause a garbage collection, which
26  * might change weak_ptr_list.  Must re-load weak_ptr_list each time
27  * around the loop.
28  */
29
30 void
31 finalizeWeakPointersNow(void)
32 {
33   StgWeak *w;
34   
35   while ((w = weak_ptr_list)) {
36     weak_ptr_list = w->link;
37     if (w->header.info != &DEAD_WEAK_info) {
38         w->header.info = &DEAD_WEAK_info;
39         IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
40         if (w->finalizer != &NO_FINALIZER_closure) {
41             rts_evalIO(w->finalizer,NULL);
42         }
43     }
44   }
45
46
47 /*
48  * scheduleFinalizers() is called on the list of weak pointers found
49  * to be dead after a garbage collection.  It overwrites each object
50  * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
51  *
52  * This function is called just after GC.  The weak pointers on the
53  * argument list are those whose keys were found to be not reachable,
54  * however the value and finalizer fields have by now been marked live.
55  * The weak pointer object itself may not be alive - i.e. we may be
56  * looking at either an object in from-space or one in to-space.  It
57  * doesn't really matter either way.
58  */
59
60 void
61 scheduleFinalizers(StgWeak *list)
62 {
63     StgWeak *w;
64     StgTSO *t;
65     StgMutArrPtrs *arr;
66     nat n;
67
68     /* count number of finalizers first... */
69     for (n = 0, w = list; w; w = w->link) { 
70         if (w->finalizer != &NO_FINALIZER_closure)
71             n++;
72     }
73         
74     if (n == 0) return;
75
76     IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
77
78     arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
79     SET_HDR(arr, &MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
80     arr->ptrs = n;
81
82     for (n = 0, w = list; w; w = w->link) {
83         if (w->finalizer != &NO_FINALIZER_closure) {
84             arr->payload[n] = w->finalizer;
85             n++;
86         }
87         w->header.info = &DEAD_WEAK_info;
88     }
89
90     t = createIOThread(RtsFlags.GcFlags.initialStkSize, 
91                        rts_apply(
92                            rts_apply(
93                                (StgClosure *)runFinalizerBatch_closure,
94                                rts_mkInt(n)), 
95                            (StgClosure *)arr)
96         );
97     scheduleThread(t);
98 }