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;
25 runCFinalizer(void *fn, void *ptr, void *env, StgWord flag)
28 ((void (*)(void *, void *))fn)(env, ptr);
30 ((void (*)(void *))fn)(ptr);
34 runAllCFinalizers(StgWeak *list)
41 task->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((void *)farr->payload[0],
51 (void *)farr->payload[1],
52 (void *)farr->payload[2],
57 task->running_finalizers = rtsFalse;
62 * scheduleFinalizers() is called on the list of weak pointers found
63 * to be dead after a garbage collection. It overwrites each object
64 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
66 * This function is called just after GC. The weak pointers on the
67 * argument list are those whose keys were found to be not reachable,
68 * however the value and finalizer fields have by now been marked live.
69 * The weak pointer object itself may not be alive - i.e. we may be
70 * looking at either an object in from-space or one in to-space. It
71 * doesn't really matter either way.
73 * Pre-condition: sched_mutex _not_ held.
77 scheduleFinalizers(Capability *cap, StgWeak *list)
88 task->running_finalizers = rtsTrue;
91 // count number of finalizers, and kill all the weak pointers first...
93 for (w = list; w; w = w->link) {
96 // Better not be a DEAD_WEAK at this stage; the garbage
97 // collector removes DEAD_WEAKs from the weak pointer list.
98 ASSERT(w->header.info != &stg_DEAD_WEAK_info);
100 if (w->finalizer != &stg_NO_FINALIZER_closure) {
104 farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer);
106 if ((StgClosure *)farr != &stg_NO_FINALIZER_closure)
107 runCFinalizer((void *)farr->payload[0],
108 (void *)farr->payload[1],
109 (void *)farr->payload[2],
113 // A weak pointer is inherently used, so we do not need to call
116 // Furthermore, when PROFILING is turned on, dead weak
117 // pointers are exactly as large as weak pointers, so there is
118 // no need to fill the slop, either. See stg_DEAD_WEAK_info
119 // in StgMiscClosures.hc.
121 SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
125 task->running_finalizers = rtsFalse;
128 // No finalizers to run?
131 debugTrace(DEBUG_weak, "weak: batching %d finalizers", n);
133 size = n + mutArrPtrsCardTableSize(n);
134 arr = (StgMutArrPtrs *)allocate(cap, sizeofW(StgMutArrPtrs) + size);
135 TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
136 SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
141 for (w = list; w; w = w->link) {
142 if (w->finalizer != &stg_NO_FINALIZER_closure) {
143 arr->payload[n] = w->finalizer;
147 // set all the cards to 1
148 for (i = n; i < size; i++) {
149 arr->payload[i] = (StgClosure *)(W_)(-1);
152 t = createIOThread(cap,
153 RtsFlags.GcFlags.initialStkSize,
156 (StgClosure *)runFinalizerBatch_closure,
160 scheduleThread(cap,t);