summaryrefslogtreecommitdiff
path: root/ghc/rts/Weak.c
blob: 8fe2575b08090534081eedff0b1ca39647198ac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* -----------------------------------------------------------------------------
 * $Id: Weak.c,v 1.6 1999/02/02 14:17:45 simonm Exp $
 *
 * Weak pointers / finalisers
 *
 * ---------------------------------------------------------------------------*/

#include "Rts.h"
#include "RtsAPI.h"
#include "RtsFlags.h"
#include "Weak.h"
#include "Storage.h"

StgWeak *weak_ptr_list;

/*
 * finaliseWeakPointersNow() is called just before the system is shut
 * down.  It runs the finaliser for each weak pointer still in the
 * system.
 */

void
finaliseWeakPointersNow(void)
{
  StgWeak *w;

  for (w = weak_ptr_list; w; w = w->link) {
    IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
    w->header.info = &DEAD_WEAK_info;
    if (w->finaliser != &NO_FINALISER_closure) {
      rts_evalIO(w->finaliser,NULL);
    }
  }
} 

/*
 * scheduleFinalisers() is called on the list of weak pointers found
 * to be dead after a garbage collection.  It overwrites each object
 * with DEAD_WEAK, and creates a new thread for the finaliser.
 */

void
scheduleFinalisers(StgWeak *list)
{
  StgWeak *w;
  
  for (w = list; w; w = w->link) {
    IF_DEBUG(weak,fprintf(stderr,"Finalising weak pointer at %p -> %p\n", w, w->key));
    if (w->finaliser != &NO_FINALISER_closure) {
#ifdef INTERPRETER
      createGenThread(RtsFlags.GcFlags.initialStkSize, w->finaliser);
#else
      createIOThread(RtsFlags.GcFlags.initialStkSize, w->finaliser);
#endif
    }
    w->header.info = &DEAD_WEAK_info;
  }
}

void
markWeakList(void)
{
  StgWeak *w, **last_w;

  last_w = &weak_ptr_list;
  for (w = weak_ptr_list; w; w = w->link) {
    w = (StgWeak *)MarkRoot((StgClosure *)w);
    *last_w = w;
    last_w = &(w->link);
  }
}