[project @ 2000-06-19 12:09:22 by simonmar]
[ghc-hetmet.git] / ghc / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Weak.c,v 1.14 2000/05/22 13:09:29 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_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
38     w->header.info = &DEAD_WEAK_info;
39     if (w->finalizer != &NO_FINALIZER_closure) {
40       rts_evalIO(w->finalizer,NULL);
41     }
42   }
43
44
45 /*
46  * scheduleFinalizers() is called on the list of weak pointers found
47  * to be dead after a garbage collection.  It overwrites each object
48  * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
49  *
50  * This function is called just after GC.  The weak pointers on the
51  * argument list are those whose keys were found to be not reachable,
52  * however the value and finalizer fields have by now been marked live.
53  * The weak pointer object itself may not be alive - i.e. we may be
54  * looking at either an object in from-space or one in to-space.  It
55  * doesn't really matter either way.
56  */
57
58 void
59 scheduleFinalizers(StgWeak *list)
60 {
61     StgWeak *w;
62     StgTSO *t;
63     StgMutArrPtrs *arr;
64     nat n;
65
66     /* count number of finalizers first... */
67     for (n = 0, w = list; w; w = w->link) { 
68         if (w->finalizer != &NO_FINALIZER_closure)
69             n++;
70     }
71         
72     if (n == 0) return;
73
74     IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
75
76     arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
77     SET_HDR(arr, &MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
78     arr->ptrs = n;
79
80     for (n = 0, w = list; w; w = w->link) {
81         if (w->finalizer != &NO_FINALIZER_closure) {
82             arr->payload[n] = w->finalizer;
83             n++;
84         }
85         w->header.info = &DEAD_WEAK_info;
86     }
87
88     t = createIOThread(RtsFlags.GcFlags.initialStkSize, 
89                        rts_apply(
90                            rts_apply(
91                                (StgClosure *)runFinalizerBatch_closure,
92                                rts_mkInt(n)), 
93                            (StgClosure *)arr)
94         );
95     scheduleThread(t);
96 }