1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2008
5 * Weak pointers and weak-like things in the GC
7 * Documentation on the architecture of the Garbage Collector can be
8 * found in the online commentary:
10 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
12 * ---------------------------------------------------------------------------*/
14 #include "PosixSource.h"
27 /* -----------------------------------------------------------------------------
30 traverse_weak_ptr_list is called possibly many times during garbage
31 collection. It returns a flag indicating whether it did any work
32 (i.e. called evacuate on any live pointers).
34 Invariant: traverse_weak_ptr_list is called when the heap is in an
35 idempotent state. That means that there are no pending
36 evacuate/scavenge operations. This invariant helps the weak
37 pointer code decide which weak pointers are dead - if there are no
38 new live weak pointers, then all the currently unreachable ones are
41 For generational GC: we just don't try to finalize weak pointers in
42 older generations than the one we're collecting. This could
43 probably be optimised by keeping per-generation lists of weak
44 pointers, but for a few weak pointers this scheme will work.
46 There are three distinct stages to processing weak pointers:
48 - weak_stage == WeakPtrs
50 We process all the weak pointers whos keys are alive (evacuate
51 their values and finalizers), and repeat until we can find no new
52 live keys. If no live keys are found in this pass, then we
53 evacuate the finalizers of all the dead weak pointers in order to
56 - weak_stage == WeakThreads
58 Now, we discover which *threads* are still alive. Pointers to
59 threads from the all_threads and main thread lists are the
60 weakest of all: a pointers from the finalizer of a dead weak
61 pointer can keep a thread alive. Any threads found to be unreachable
62 are evacuated and placed on the resurrected_threads list so we
63 can send them a signal later.
65 - weak_stage == WeakDone
67 No more evacuation is done.
69 -------------------------------------------------------------------------- */
71 /* Which stage of processing various kinds of weak pointer are we at?
72 * (see traverse_weak_ptr_list() below for discussion).
74 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
75 static WeakStage weak_stage;
79 StgWeak *old_weak_ptr_list; // also pending finaliser list
81 // List of threads found to be unreachable
82 StgTSO *resurrected_threads;
84 static void resurrectUnreachableThreads (generation *gen);
85 static rtsBool tidyThreadList (generation *gen);
90 old_weak_ptr_list = weak_ptr_list;
92 weak_stage = WeakPtrs;
93 resurrected_threads = END_TSO_QUEUE;
97 traverseWeakPtrList(void)
99 StgWeak *w, **last_w, *next_w;
101 rtsBool flag = rtsFalse;
102 const StgInfoTable *info;
104 switch (weak_stage) {
110 /* doesn't matter where we evacuate values/finalizers to, since
111 * these pointers are treated as roots (iff the keys are alive).
115 last_w = &old_weak_ptr_list;
116 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
118 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
119 * called on a live weak pointer object. Just remove it.
121 if (w->header.info == &stg_DEAD_WEAK_info) {
122 next_w = ((StgDeadWeak *)w)->link;
128 switch (info->type) {
131 /* Now, check whether the key is reachable.
133 new = isAlive(w->key);
136 // evacuate the value and finalizer
138 evacuate(&w->finalizer);
139 // remove this weak ptr from the old_weak_ptr list
141 // and put it on the new weak ptr list
143 w->link = weak_ptr_list;
147 debugTrace(DEBUG_weak,
148 "weak pointer still alive at %p -> %p",
159 barf("traverseWeakPtrList: not WEAK");
163 /* If we didn't make any changes, then we can go round and kill all
164 * the dead weak pointers. The old_weak_ptr list is used as a list
165 * of pending finalizers later on.
167 if (flag == rtsFalse) {
168 for (w = old_weak_ptr_list; w; w = w->link) {
169 evacuate(&w->finalizer);
172 // Next, move to the WeakThreads stage after fully
173 // scavenging the finalizers we've just evacuated.
174 weak_stage = WeakThreads;
180 /* Now deal with the step->threads lists, which behave somewhat like
181 * the weak ptr list. If we discover any threads that are about to
182 * become garbage, we wake them up and administer an exception.
187 // Traverse thread lists for generations we collected...
188 // ToDo when we have one gen per capability:
189 // for (n = 0; n < n_capabilities; n++) {
190 // if (tidyThreadList(&nurseries[n])) {
194 for (g = 0; g <= N; g++) {
195 if (tidyThreadList(&generations[g])) {
200 /* If we evacuated any threads, we need to go back to the scavenger.
202 if (flag) return rtsTrue;
204 /* And resurrect any threads which were about to become garbage.
208 for (g = 0; g <= N; g++) {
209 resurrectUnreachableThreads(&generations[g]);
213 /* Finally, we can update the blackhole_queue. This queue
214 * simply strings together TSOs blocked on black holes, it is
215 * not intended to keep anything alive. Hence, we do not follow
216 * pointers on the blackhole_queue until now, when we have
217 * determined which TSOs are otherwise reachable. We know at
218 * this point that all TSOs have been evacuated, however.
222 for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->_link)) {
223 *pt = (StgTSO *)isAlive((StgClosure *)*pt);
228 weak_stage = WeakDone; // *now* we're done,
229 return rtsTrue; // but one more round of scavenging, please
233 barf("traverse_weak_ptr_list");
238 static void resurrectUnreachableThreads (generation *gen)
240 StgTSO *t, *tmp, *next;
242 for (t = gen->old_threads; t != END_TSO_QUEUE; t = next) {
243 next = t->global_link;
245 // ThreadFinished and ThreadComplete: we have to keep
246 // these on the all_threads list until they
247 // become garbage, because they might get
248 // pending exceptions.
249 switch (t->what_next) {
255 evacuate((StgClosure **)&tmp);
256 tmp->global_link = resurrected_threads;
257 resurrected_threads = tmp;
262 static rtsBool tidyThreadList (generation *gen)
264 StgTSO *t, *tmp, *next, **prev;
265 rtsBool flag = rtsFalse;
267 prev = &gen->old_threads;
269 for (t = gen->old_threads; t != END_TSO_QUEUE; t = next) {
271 tmp = (StgTSO *)isAlive((StgClosure *)t);
277 ASSERT(get_itbl(t)->type == TSO);
278 if (t->what_next == ThreadRelocated) {
284 next = t->global_link;
286 // if the thread is not masking exceptions but there are
287 // pending exceptions on its queue, then something has gone
289 ASSERT(t->blocked_exceptions == END_BLOCKED_EXCEPTIONS_QUEUE
290 || (t->flags & TSO_BLOCKEX));
293 // not alive (yet): leave this thread on the
294 // old_all_threads list.
295 prev = &(t->global_link);
301 // move this thread onto the correct threads list.
303 new_gen = Bdescr((P_)t)->gen;
304 t->global_link = new_gen->threads;
305 new_gen->threads = t;
312 /* -----------------------------------------------------------------------------
315 Threads on this list behave like weak pointers during the normal
316 phase of garbage collection: if the blackhole is reachable, then
317 the thread is reachable too.
318 -------------------------------------------------------------------------- */
320 traverseBlackholeQueue (void)
322 StgTSO *prev, *t, *tmp;
329 for (t = blackhole_queue; t != END_TSO_QUEUE; prev=t, t = t->_link) {
330 // if the thread is not yet alive...
331 if (! (tmp = (StgTSO *)isAlive((StgClosure*)t))) {
332 // if the closure it is blocked on is either (a) a
333 // reachable BLAKCHOLE or (b) not a BLACKHOLE, then we
334 // make the thread alive.
335 if (!isAlive(t->block_info.closure)) {
336 type = get_itbl(t->block_info.closure)->type;
337 if (type == BLACKHOLE || type == CAF_BLACKHOLE) {
341 evacuate((StgClosure **)&t);
347 // no write barrier when on the blackhole queue,
348 // because we traverse the whole queue on every GC.
355 /* -----------------------------------------------------------------------------
356 Evacuate every weak pointer object on the weak_ptr_list, and update
359 ToDo: with a lot of weak pointers, this will be expensive. We
360 should have a per-GC weak pointer list, just like threads.
361 -------------------------------------------------------------------------- */
364 markWeakPtrList ( void )
366 StgWeak *w, **last_w;
368 last_w = &weak_ptr_list;
369 for (w = weak_ptr_list; w; w = w->link) {
370 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
371 ASSERT(IS_FORWARDING_PTR(w->header.info)
372 || w->header.info == &stg_DEAD_WEAK_info
373 || get_itbl(w)->type == WEAK);
374 evacuate((StgClosure **)last_w);
376 if (w->header.info == &stg_DEAD_WEAK_info) {
377 last_w = &(((StgDeadWeak*)w)->link);