1 /* -----------------------------------------------------------------------------
2 * $Id: Weak.c,v 1.20 2001/11/26 16:54:22 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) {
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);
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.
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.
62 scheduleFinalizers(StgWeak *list)
69 /* count number of finalizers first... */
70 for (n = 0, w = list; w; w = w->link) {
71 if (w->finalizer != &stg_NO_FINALIZER_closure)
77 IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
79 arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
80 SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
83 for (n = 0, w = list; w; w = w->link) {
84 if (w->finalizer != &stg_NO_FINALIZER_closure) {
85 arr->payload[n] = w->finalizer;
90 // A weak pointer is inherently used, so we do not need to call
93 // Furthermore, when PROFILING is turned on, dead weak
94 // pointers are exactly as large as weak pointers, so there is
95 // no need to fill the slop, either. See stg_DEAD_WEAK_info
96 // in StgMiscClosures.hc.
98 SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
101 t = createIOThread(RtsFlags.GcFlags.initialStkSize,
104 (StgClosure *)runFinalizerBatch_closure,