[project @ 1999-02-26 12:43:58 by simonm]
[ghc-hetmet.git] / ghc / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Weak.c,v 1.9 1999/02/26 12:43:58 simonm 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 "RtsFlags.h"
13 #include "Weak.h"
14 #include "Storage.h"
15
16 StgWeak *weak_ptr_list;
17
18 /*
19  * finalizeWeakPointersNow() is called just before the system is shut
20  * down.  It runs the finalizer for each weak pointer still in the
21  * system.
22  *
23  * Careful here - rts_evalIO might cause a garbage collection, which
24  * might change weak_ptr_list.  Must re-load weak_ptr_list each time
25  * around the loop.
26  */
27
28 void
29 finalizeWeakPointersNow(void)
30 {
31   StgWeak *w;
32   
33   while ((w = weak_ptr_list)) {
34     weak_ptr_list = w->link;
35     IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
36     w->header.info = &DEAD_WEAK_info;
37     if (w->finalizer != &NO_FINALIZER_closure) {
38       rts_evalIO(w->finalizer,NULL);
39     }
40   }
41
42
43 /*
44  * scheduleFinalizers() is called on the list of weak pointers found
45  * to be dead after a garbage collection.  It overwrites each object
46  * with DEAD_WEAK, and creates a new thread for the finalizer.
47  */
48
49 void
50 scheduleFinalizers(StgWeak *list)
51 {
52   StgWeak *w;
53   
54   for (w = list; w; w = w->link) {
55     IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
56     if (w->finalizer != &NO_FINALIZER_closure) {
57 #ifdef INTERPRETER
58       createGenThread(RtsFlags.GcFlags.initialStkSize, w->finalizer);
59 #else
60       createIOThread(RtsFlags.GcFlags.initialStkSize, w->finalizer);
61 #endif
62     }
63     w->header.info = &DEAD_WEAK_info;
64   }
65 }
66
67 void
68 markWeakList(void)
69 {
70   StgWeak *w, **last_w;
71
72   last_w = &weak_ptr_list;
73   for (w = weak_ptr_list; w; w = w->link) {
74     w = (StgWeak *)MarkRoot((StgClosure *)w);
75     *last_w = w;
76     last_w = &(w->link);
77   }
78 }
79