1 /* -----------------------------------------------------------------------------
2 * $Id: Weak.c,v 1.29 2003/03/26 17:43:05 sof Exp $
4 * (c) The GHC Team, 1998-1999
6 * Weak pointers / finalizers
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
11 #define COMPILING_RTS_MAIN
21 StgWeak *weak_ptr_list;
24 * finalizeWeakPointersNow() is called just before the system is shut
25 * down. It runs the finalizer for each weak pointer still in the
28 * Careful here - rts_evalIO might cause a garbage collection, which
29 * might change weak_ptr_list. Must re-load weak_ptr_list each time
34 finalizeWeakPointersNow(void)
38 #if defined(RTS_SUPPORTS_THREADS)
41 while ((w = weak_ptr_list)) {
42 weak_ptr_list = w->link;
43 if (w->header.info != &stg_DEAD_WEAK_info) {
44 w->header.info = &stg_DEAD_WEAK_info;
45 IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
46 if (w->finalizer != &stg_NO_FINALIZER_closure) {
47 #if defined(RTS_SUPPORTS_THREADS)
48 rts_evalIO(w->finalizer,NULL);
52 rts_mainLazyIO(w->finalizer,NULL);
57 #if defined(RTS_SUPPORTS_THREADS)
63 * scheduleFinalizers() is called on the list of weak pointers found
64 * to be dead after a garbage collection. It overwrites each object
65 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
67 * This function is called just after GC. The weak pointers on the
68 * argument list are those whose keys were found to be not reachable,
69 * however the value and finalizer fields have by now been marked live.
70 * The weak pointer object itself may not be alive - i.e. we may be
71 * looking at either an object in from-space or one in to-space. It
72 * doesn't really matter either way.
74 * Pre-condition: sched_mutex _not_ held.
78 scheduleFinalizers(StgWeak *list)
85 // count number of finalizers, and kill all the weak pointers first...
87 for (w = list; w; w = w->link) {
89 // Better not be a DEAD_WEAK at this stage; the garbage
90 // collector removes DEAD_WEAKs from the weak pointer list.
91 ASSERT(w->header.info != &stg_DEAD_WEAK_info);
93 if (w->finalizer != &stg_NO_FINALIZER_closure) {
98 // A weak pointer is inherently used, so we do not need to call
101 // Furthermore, when PROFILING is turned on, dead weak
102 // pointers are exactly as large as weak pointers, so there is
103 // no need to fill the slop, either. See stg_DEAD_WEAK_info
104 // in StgMiscClosures.hc.
106 SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
109 // No finalizers to run?
112 IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
114 arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
115 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
116 SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
120 for (w = list; w; w = w->link) {
121 if (w->finalizer != &stg_NO_FINALIZER_closure) {
122 arr->payload[n] = w->finalizer;
127 t = createIOThread(RtsFlags.GcFlags.initialStkSize,
130 (StgClosure *)runFinalizerBatch_closure,