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

9
#include "PosixSource.h"
10
#include "Rts.h"
Simon Marlow's avatar
Simon Marlow committed
11 12
#include "RtsAPI.h"

13
#include "RtsUtils.h"
14
#include "Weak.h"
sof's avatar
sof committed
15
#include "Schedule.h"
16
#include "Prelude.h"
Simon Marlow's avatar
Simon Marlow committed
17
#include "Trace.h"
18 19 20

StgWeak *weak_ptr_list;

21
void
22
runCFinalizers(StgCFinalizerList *list)
23
{
24 25 26 27 28 29 30 31 32 33
    StgCFinalizerList *head;
    for (head = list;
        (StgClosure *)head != &stg_NO_FINALIZER_closure;
        head = (StgCFinalizerList *)head->link)
    {
        if (head->flag)
            ((void (*)(void *, void *))head->fptr)(head->eptr, head->ptr);
        else
            ((void (*)(void *))head->fptr)(head->ptr);
    }
34 35 36 37 38 39
}

void
runAllCFinalizers(StgWeak *list)
{
    StgWeak *w;
40
    Task *task;
41

42 43 44 45
    task = myTask();
    if (task != NULL) {
        task->running_finalizers = rtsTrue;
    }
46

47
    for (w = list; w; w = w->link) {
48
	runCFinalizers((StgCFinalizerList *)w->cfinalizers);
49
    }
50

51 52 53
    if (task != NULL) {
        task->running_finalizers = rtsFalse;
    }
54 55
}

56
/*
57
 * scheduleFinalizers() is called on the list of weak pointers found
58
 * to be dead after a garbage collection.  It overwrites each object
59
 * with DEAD_WEAK, and creates a new thread to run the pending finalizers.
60 61 62 63 64 65 66
 *
 * 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
67 68
 *
 * Pre-condition: sched_mutex _not_ held.
69 70 71
 */

void
72
scheduleFinalizers(Capability *cap, StgWeak *list)
73
{
74 75 76
    StgWeak *w;
    StgTSO *t;
    StgMutArrPtrs *arr;
77 78
    StgWord size;
    nat n, i;
79
    Task *task;
80

81 82 83 84
    task = myTask();
    if (task != NULL) {
        task->running_finalizers = rtsTrue;
    }
85

86
    // count number of finalizers, and kill all the weak pointers first...
87 88 89 90 91 92 93
    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) {
94
	    n++;
95 96
	}

97
	runCFinalizers((StgCFinalizerList *)w->cfinalizers);
98

99 100 101 102 103 104 105 106 107 108
#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);
109
    }
110
	
111 112 113
    if (task != NULL) {
        task->running_finalizers = rtsFalse;
    }
114

115
    // No finalizers to run?
116 117
    if (n == 0) return;

Simon Marlow's avatar
Simon Marlow committed
118
    debugTrace(DEBUG_weak, "weak: batching %d finalizers", n);
119

120 121
    size = n + mutArrPtrsCardTableSize(n);
    arr = (StgMutArrPtrs *)allocate(cap, sizeofW(StgMutArrPtrs) + size);
122
    TICK_ALLOC_PRIM(sizeofW(StgMutArrPtrs), n, 0);
123
    SET_HDR(arr, &stg_MUT_ARR_PTRS_FROZEN_info, CCS_SYSTEM);
124
    arr->ptrs = n;
125
    arr->size = size;
126

127 128 129
    n = 0;
    for (w = list; w; w = w->link) {
	if (w->finalizer != &stg_NO_FINALIZER_closure) {
130 131 132 133
	    arr->payload[n] = w->finalizer;
	    n++;
	}
    }
134 135 136 137
    // set all the cards to 1
    for (i = n; i < size; i++) {
        arr->payload[i] = (StgClosure *)(W_)(-1);
    }
138

139 140 141 142
    t = createIOThread(cap, 
		       RtsFlags.GcFlags.initialStkSize, 
		       rts_apply(cap,
			   rts_apply(cap,
143
			       (StgClosure *)runFinalizerBatch_closure,
144
			       rts_mkInt(cap,n)), 
145 146
			   (StgClosure *)arr)
	);
147
    scheduleThread(cap,t);
148
}