1 /* -----------------------------------------------------------------------------
2 * $Id: Weak.c,v 1.19 2001/11/22 14:25:13 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * Weak pointers / finalizers
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
19 StgWeak *weak_ptr_list;
22 * finalizeWeakPointersNow() is called just before the system is shut
23 * down. It runs the finalizer for each weak pointer still in the
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
32 finalizeWeakPointersNow(void)
36 while ((w = weak_ptr_list)) {
37 weak_ptr_list = w->link;
38 if (w->header.info != &stg_DEAD_WEAK_info) {
40 // Even thought the info type of w changes, we DO NOT perform any
41 // LDV profiling because at this moment, LDV profiling must already
42 // have been terminated. See the comments in shutdownHaskell().
43 // At any rate, there is no need to call LDV_recordDead() because
44 // weak pointers are inherently used.
46 ASSERT(ldvTime == 0); // LDV profiling is turned off.
48 w->header.info = &stg_DEAD_WEAK_info;
49 IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
50 if (w->finalizer != &stg_NO_FINALIZER_closure) {
51 rts_evalIO(w->finalizer,NULL);
58 * scheduleFinalizers() is called on the list of weak pointers found
59 * to be dead after a garbage collection. It overwrites each object
60 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
62 * This function is called just after GC. The weak pointers on the
63 * argument list are those whose keys were found to be not reachable,
64 * however the value and finalizer fields have by now been marked live.
65 * The weak pointer object itself may not be alive - i.e. we may be
66 * looking at either an object in from-space or one in to-space. It
67 * doesn't really matter either way.
71 scheduleFinalizers(StgWeak *list)
78 /* count number of finalizers first... */
79 for (n = 0, w = list; w; w = w->link) {
80 if (w->finalizer != &stg_NO_FINALIZER_closure)
86 IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
88 arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
89 SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
92 for (n = 0, w = list; w; w = w->link) {
93 if (w->finalizer != &stg_NO_FINALIZER_closure) {
94 arr->payload[n] = w->finalizer;
99 // A weak pointer is inherently used, so we do not need to call
102 // Furthermore, when PROFILING is turned on, dead weak
103 // pointers are exactly as large as weak pointers, so there is
104 // no need to fill the slop, either. See stg_DEAD_WEAK_info
105 // in StgMiscClosures.hc.
107 SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
110 t = createIOThread(RtsFlags.GcFlags.initialStkSize,
113 (StgClosure *)runFinalizerBatch_closure,