1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-1999
5 * Weak pointers / finalizers
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
20 // ForeignPtrs with C finalizers rely on weak pointers inside weak_ptr_list
21 // to always be in the same order.
23 StgWeak *weak_ptr_list;
25 // So that we can detect when a finalizer illegally calls back into Haskell
26 rtsBool running_finalizers = rtsFalse;
29 runCFinalizer(StgVoid *fn, StgVoid *ptr, StgVoid *env, StgWord flag)
32 ((void (*)(void *, void *))fn)(env, ptr);
34 ((void (*)(void *))fn)(ptr);
38 runAllCFinalizers(StgWeak *list)
42 running_finalizers = rtsTrue;
44 for (w = list; w; w = w->link) {
47 farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer);
49 if ((StgClosure *)farr != &stg_NO_FINALIZER_closure)
50 runCFinalizer((StgVoid *)farr->payload[0],
51 (StgVoid *)farr->payload[1],
52 (StgVoid *)farr->payload[2],
56 running_finalizers = rtsFalse;
60 * scheduleFinalizers() is called on the list of weak pointers found
61 * to be dead after a garbage collection. It overwrites each object
62 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
64 * This function is called just after GC. The weak pointers on the
65 * argument list are those whose keys were found to be not reachable,
66 * however the value and finalizer fields have by now been marked live.
67 * The weak pointer object itself may not be alive - i.e. we may be
68 * looking at either an object in from-space or one in to-space. It
69 * doesn't really matter either way.
71 * Pre-condition: sched_mutex _not_ held.
75 scheduleFinalizers(Capability *cap, StgWeak *list)
82 running_finalizers = rtsTrue;
84 // count number of finalizers, and kill all the weak pointers first...
86 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) {
97 farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer);
99 if ((StgClosure *)farr != &stg_NO_FINALIZER_closure)
100 runCFinalizer((StgVoid *)farr->payload[0],
101 (StgVoid *)farr->payload[1],
102 (StgVoid *)farr->payload[2],
106 // A weak pointer is inherently used, so we do not need to call
109 // Furthermore, when PROFILING is turned on, dead weak
110 // pointers are exactly as large as weak pointers, so there is
111 // no need to fill the slop, either. See stg_DEAD_WEAK_info
112 // in StgMiscClosures.hc.
114 SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
117 running_finalizers = rtsFalse;
119 // No finalizers to run?
122 debugTrace(DEBUG_weak, "weak: batching %d finalizers", n);
124 arr = (StgMutArrPtrs *)allocateLocal(cap, sizeofW(StgMutArrPtrs) + n);
125 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
126 SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
130 for (w = list; w; w = w->link) {
131 if (w->finalizer != &stg_NO_FINALIZER_closure) {
132 arr->payload[n] = w->finalizer;
137 t = createIOThread(cap,
138 RtsFlags.GcFlags.initialStkSize,
141 (StgClosure *)runFinalizerBatch_closure,
145 scheduleThread(cap,t);