1 /* -----------------------------------------------------------------------------
2 * $Id: Weak.c,v 1.15 2000/06/20 15:18:40 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * Weak pointers / finalizers
8 * ---------------------------------------------------------------------------*/
18 StgWeak *weak_ptr_list;
21 * finalizeWeakPointersNow() is called just before the system is shut
22 * down. It runs the finalizer for each weak pointer still in the
25 * Careful here - rts_evalIO might cause a garbage collection, which
26 * might change weak_ptr_list. Must re-load weak_ptr_list each time
31 finalizeWeakPointersNow(void)
35 while ((w = weak_ptr_list)) {
36 weak_ptr_list = w->link;
37 if (w->header.info != &DEAD_WEAK_info) {
38 w->header.info = &DEAD_WEAK_info;
39 IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
40 if (w->finalizer != &NO_FINALIZER_closure) {
41 rts_evalIO(w->finalizer,NULL);
48 * scheduleFinalizers() is called on the list of weak pointers found
49 * to be dead after a garbage collection. It overwrites each object
50 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
52 * This function is called just after GC. The weak pointers on the
53 * argument list are those whose keys were found to be not reachable,
54 * however the value and finalizer fields have by now been marked live.
55 * The weak pointer object itself may not be alive - i.e. we may be
56 * looking at either an object in from-space or one in to-space. It
57 * doesn't really matter either way.
61 scheduleFinalizers(StgWeak *list)
68 /* count number of finalizers first... */
69 for (n = 0, w = list; w; w = w->link) {
70 if (w->finalizer != &NO_FINALIZER_closure)
76 IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
78 arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
79 SET_HDR(arr, &MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
82 for (n = 0, w = list; w; w = w->link) {
83 if (w->finalizer != &NO_FINALIZER_closure) {
84 arr->payload[n] = w->finalizer;
87 w->header.info = &DEAD_WEAK_info;
90 t = createIOThread(RtsFlags.GcFlags.initialStkSize,
93 (StgClosure *)runFinalizerBatch_closure,