Weak.c 3.47 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2 3
 *
 * (c) The GHC Team, 1998-1999
4
 *
5
 * Weak pointers / finalizers
6 7 8
 *
 * ---------------------------------------------------------------------------*/

9
#include "PosixSource.h"
sof's avatar
sof committed
10
#define COMPILING_RTS_MAIN
11
#include "Rts.h"
12
#include "RtsUtils.h"
sof's avatar
sof committed
13
#include "SchedAPI.h"
14 15 16
#include "RtsFlags.h"
#include "Weak.h"
#include "Storage.h"
sof's avatar
sof committed
17
#include "Schedule.h"
18
#include "Prelude.h"
sof's avatar
sof committed
19
#include "RtsAPI.h"
20 21 22 23

StgWeak *weak_ptr_list;

/*
24 25
 * finalizeWeakPointersNow() is called just before the system is shut
 * down.  It runs the finalizer for each weak pointer still in the
26
 * system.
27 28 29 30
 *
 * Careful here - rts_evalIO might cause a garbage collection, which
 * might change weak_ptr_list.  Must re-load weak_ptr_list each time
 * around the loop.
31 32 33
 */

void
34
finalizeWeakPointersNow(void)
35 36
{
  StgWeak *w;
37
  
38
  rts_lock();
39 40
  while ((w = weak_ptr_list)) {
    weak_ptr_list = w->link;
41
    if (w->header.info != &stg_DEAD_WEAK_info) {
42
	SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
43
	IF_DEBUG(weak,debugBelch("Finalising weak pointer at %p -> %p\n", w, w->key));
44
	if (w->finalizer != &stg_NO_FINALIZER_closure) {
45
	    rts_evalLazyIO(w->finalizer,NULL);
46 47
	    rts_unlock();
	    rts_lock();
48
	}
49
    }
50
  }
51
  rts_unlock();
52 53 54
} 

/*
55
 * scheduleFinalizers() is called on the list of weak pointers found
56
 * to be dead after a garbage collection.  It overwrites each object
57
 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
58 59 60 61 62 63 64
 *
 * This function is called just after GC.  The weak pointers on the
 * argument list are those whose keys were found to be not reachable,
 * however the value and finalizer fields have by now been marked live.
 * The weak pointer object itself may not be alive - i.e. we may be
 * looking at either an object in from-space or one in to-space.  It
 * doesn't really matter either way.
sof's avatar
sof committed
65 66
 *
 * Pre-condition: sched_mutex _not_ held.
67 68 69
 */

void
70
scheduleFinalizers(StgWeak *list)
71
{
72 73 74 75 76
    StgWeak *w;
    StgTSO *t;
    StgMutArrPtrs *arr;
    nat n;

77
    // count number of finalizers, and kill all the weak pointers first...
78 79 80 81 82 83 84 85
    n = 0;
    for (w = list; w; w = w->link) { 

	// Better not be a DEAD_WEAK at this stage; the garbage
	// collector removes DEAD_WEAKs from the weak pointer list.
	ASSERT(w->header.info != &stg_DEAD_WEAK_info);

	if (w->finalizer != &stg_NO_FINALIZER_closure) {
86
	    n++;
87 88 89 90 91 92 93 94 95 96 97 98
	}

#ifdef PROFILING
        // A weak pointer is inherently used, so we do not need to call
        // LDV_recordDead().
	//
        // Furthermore, when PROFILING is turned on, dead weak
        // pointers are exactly as large as weak pointers, so there is
        // no need to fill the slop, either.  See stg_DEAD_WEAK_info
        // in StgMiscClosures.hc.
#endif
	SET_HDR(w, &stg_DEAD_WEAK_info, w->header.prof.ccs);
99
    }
100
	
101
    // No finalizers to run?
102 103
    if (n == 0) return;

104
    IF_DEBUG(weak,debugBelch("weak: batching %d finalizers\n", n));
105 106

    arr = (StgMutArrPtrs *)allocate(sizeofW(StgMutArrPtrs) + n);
107
    TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
108
    SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
109
    arr->mut_link = NULL;
110 111
    arr->ptrs = n;

112 113 114
    n = 0;
    for (w = list; w; w = w->link) {
	if (w->finalizer != &stg_NO_FINALIZER_closure) {
115 116 117 118 119 120 121 122 123 124 125 126 127
	    arr->payload[n] = w->finalizer;
	    n++;
	}
    }

    t = createIOThread(RtsFlags.GcFlags.initialStkSize, 
		       rts_apply(
			   rts_apply(
			       (StgClosure *)runFinalizerBatch_closure,
			       rts_mkInt(n)), 
			   (StgClosure *)arr)
	);
    scheduleThread(t);
128
}