1 /* -----------------------------------------------------------------------------
2 * $Id: Weak.c,v 1.14 2000/05/22 13:09:29 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_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
38 w->header.info = &DEAD_WEAK_info;
39 if (w->finalizer != &NO_FINALIZER_closure) {
40 rts_evalIO(w->finalizer,NULL);
46 * scheduleFinalizers() is called on the list of weak pointers found
47 * to be dead after a garbage collection. It overwrites each object
48 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
50 * This function is called just after GC. The weak pointers on the
51 * argument list are those whose keys were found to be not reachable,
52 * however the value and finalizer fields have by now been marked live.
53 * The weak pointer object itself may not be alive - i.e. we may be
54 * looking at either an object in from-space or one in to-space. It
55 * doesn't really matter either way.
59 scheduleFinalizers(StgWeak *list)
66 /* count number of finalizers first... */
67 for (n = 0, w = list; w; w = w->link) {
68 if (w->finalizer != &NO_FINALIZER_closure)
74 IF_DEBUG(weak,fprintf(stderr,"weak: batching %d finalizers\n", n));
76 arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
77 SET_HDR(arr, &MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
80 for (n = 0, w = list; w; w = w->link) {
81 if (w->finalizer != &NO_FINALIZER_closure) {
82 arr->payload[n] = w->finalizer;
85 w->header.info = &DEAD_WEAK_info;
88 t = createIOThread(RtsFlags.GcFlags.initialStkSize,
91 (StgClosure *)runFinalizerBatch_closure,