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