1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-1999
5 * Weak pointers / finalizers
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
10 #define COMPILING_RTS_MAIN
22 StgWeak *weak_ptr_list;
25 * scheduleFinalizers() is called on the list of weak pointers found
26 * to be dead after a garbage collection. It overwrites each object
27 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
29 * This function is called just after GC. The weak pointers on the
30 * argument list are those whose keys were found to be not reachable,
31 * however the value and finalizer fields have by now been marked live.
32 * The weak pointer object itself may not be alive - i.e. we may be
33 * looking at either an object in from-space or one in to-space. It
34 * doesn't really matter either way.
36 * Pre-condition: sched_mutex _not_ held.
40 scheduleFinalizers(Capability *cap, StgWeak *list)
47 // count number of finalizers, and kill all the weak pointers first...
49 for (w = list; w; w = w->link) {
51 // Better not be a DEAD_WEAK at this stage; the garbage
52 // collector removes DEAD_WEAKs from the weak pointer list.
53 ASSERT(w->header.info != &stg_DEAD_WEAK_info);
55 if (w->finalizer != &stg_NO_FINALIZER_closure) {
60 // A weak pointer is inherently used, so we do not need to call
63 // Furthermore, when PROFILING is turned on, dead weak
64 // pointers are exactly as large as weak pointers, so there is
65 // no need to fill the slop, either. See stg_DEAD_WEAK_info
66 // in StgMiscClosures.hc.
68 SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
71 // No finalizers to run?
74 debugTrace(DEBUG_weak, "weak: batching %d finalizers", n);
76 arr = (StgMutArrPtrs *)allocateLocal(cap, sizeofW(StgMutArrPtrs) + n);
77 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
78 SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
82 for (w = list; w; w = w->link) {
83 if (w->finalizer != &stg_NO_FINALIZER_closure) {
84 arr->payload[n] = w->finalizer;
89 t = createIOThread(cap,
90 RtsFlags.GcFlags.initialStkSize,
93 (StgClosure *)runFinalizerBatch_closure,
97 scheduleThread(cap,t);