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