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)
81 running_finalizers = rtsTrue;
83 // count number of finalizers, and kill all the weak pointers first...
85 for (w = list; w; w = w->link) {
88 // Better not be a DEAD_WEAK at this stage; the garbage
89 // collector removes DEAD_WEAKs from the weak pointer list.
90 ASSERT(w->header.info != &stg_DEAD_WEAK_info);
92 if (w->finalizer != &stg_NO_FINALIZER_closure) {
96 farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer);
98 if ((StgClosure *)farr != &stg_NO_FINALIZER_closure)
99 runCFinalizer((void *)farr->payload[0],
100 (void *)farr->payload[1],
101 (void *)farr->payload[2],
105 // A weak pointer is inherently used, so we do not need to call
108 // Furthermore, when PROFILING is turned on, dead weak
109 // pointers are exactly as large as weak pointers, so there is
110 // no need to fill the slop, either. See stg_DEAD_WEAK_info
111 // in StgMiscClosures.hc.
113 SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
116 running_finalizers = rtsFalse;
118 // No finalizers to run?
121 debugTrace(DEBUG_weak, "weak: batching %d finalizers", n);
123 arr = (StgMutArrPtrs *)allocateLocal(cap, sizeofW(StgMutArrPtrs) + n);
124 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
125 SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
129 for (w = list; w; w = w->link) {
130 if (w->finalizer != &stg_NO_FINALIZER_closure) {
131 arr->payload[n] = w->finalizer;
136 t = createIOThread(cap,
137 RtsFlags.GcFlags.initialStkSize,
140 (StgClosure *)runFinalizerBatch_closure,
144 scheduleThread(cap,t);