1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-1999
5 * Weak pointers / finalizers
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
19 // ForeignPtrs with C finalizers rely on weak pointers inside weak_ptr_list
20 // to always be in the same order.
22 StgWeak *weak_ptr_list;
24 // So that we can detect when a finalizer illegally calls back into Haskell
25 rtsBool running_finalizers = rtsFalse;
28 runCFinalizer(void *fn, void *ptr, void *env, StgWord flag)
31 ((void (*)(void *, void *))fn)(env, ptr);
33 ((void (*)(void *))fn)(ptr);
37 runAllCFinalizers(StgWeak *list)
41 running_finalizers = rtsTrue;
43 for (w = list; w; w = w->link) {
46 farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer);
48 if ((StgClosure *)farr != &stg_NO_FINALIZER_closure)
49 runCFinalizer((void *)farr->payload[0],
50 (void *)farr->payload[1],
51 (void *)farr->payload[2],
55 running_finalizers = rtsFalse;
59 * scheduleFinalizers() is called on the list of weak pointers found
60 * to be dead after a garbage collection. It overwrites each object
61 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
63 * This function is called just after GC. The weak pointers on the
64 * argument list are those whose keys were found to be not reachable,
65 * however the value and finalizer fields have by now been marked live.
66 * The weak pointer object itself may not be alive - i.e. we may be
67 * looking at either an object in from-space or one in to-space. It
68 * doesn't really matter either way.
70 * Pre-condition: sched_mutex _not_ held.
74 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((void *)farr->payload[0],
101 (void *)farr->payload[1],
102 (void *)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 size = n + mutArrPtrsCardTableSize(n);
125 arr = (StgMutArrPtrs *)allocate(cap, sizeofW(StgMutArrPtrs) + size);
126 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
127 SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
132 for (w = list; w; w = w->link) {
133 if (w->finalizer != &stg_NO_FINALIZER_closure) {
134 arr->payload[n] = w->finalizer;
138 // set all the cards to 1
139 for (i = n; i < size; i++) {
140 arr->payload[i] = (StgClosure *)(W_)(-1);
143 t = createIOThread(cap,
144 RtsFlags.GcFlags.initialStkSize,
147 (StgClosure *)runFinalizerBatch_closure,
151 scheduleThread(cap,t);