60325199cc49dba30d7d7c3d830906ed4dda538f
[ghc-hetmet.git] / ghc / rts / Weak.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Weak.c,v 1.10 1999/02/26 13:36:14 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  * This function is called just after GC.  The weak pointers on the
49  * argument list are those whose keys were found to be not reachable,
50  * however the value and finalizer fields have by now been marked live.
51  * The weak pointer object itself may not be alive - i.e. we may be
52  * looking at either an object in from-space or one in to-space.  It
53  * doesn't really matter either way.
54  */
55
56 void
57 scheduleFinalizers(StgWeak *list)
58 {
59   StgWeak *w;
60   
61   for (w = list; w; w = w->link) {
62     IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
63     if (w->finalizer != &NO_FINALIZER_closure) {
64 #ifdef INTERPRETER
65       createGenThread(RtsFlags.GcFlags.initialStkSize, w->finalizer);
66 #else
67       createIOThread(RtsFlags.GcFlags.initialStkSize, w->finalizer);
68 #endif
69     }
70     w->header.info = &DEAD_WEAK_info;
71   }
72 }