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"
26 /* -----------------------------------------------------------------------------
29 traverse_weak_ptr_list is called possibly many times during garbage
30 collection. It returns a flag indicating whether it did any work
31 (i.e. called evacuate on any live pointers).
33 Invariant: traverse_weak_ptr_list is called when the heap is in an
34 idempotent state. That means that there are no pending
35 evacuate/scavenge operations. This invariant helps the weak
36 pointer code decide which weak pointers are dead - if there are no
37 new live weak pointers, then all the currently unreachable ones are
40 For generational GC: we just don't try to finalize weak pointers in
41 older generations than the one we're collecting. This could
42 probably be optimised by keeping per-generation lists of weak
43 pointers, but for a few weak pointers this scheme will work.
45 There are three distinct stages to processing weak pointers:
47 - weak_stage == WeakPtrs
49 We process all the weak pointers whos keys are alive (evacuate
50 their values and finalizers), and repeat until we can find no new
51 live keys. If no live keys are found in this pass, then we
52 evacuate the finalizers of all the dead weak pointers in order to
55 - weak_stage == WeakThreads
57 Now, we discover which *threads* are still alive. Pointers to
58 threads from the all_threads and main thread lists are the
59 weakest of all: a pointers from the finalizer of a dead weak
60 pointer can keep a thread alive. Any threads found to be unreachable
61 are evacuated and placed on the resurrected_threads list so we
62 can send them a signal later.
64 - weak_stage == WeakDone
66 No more evacuation is done.
68 -------------------------------------------------------------------------- */
70 /* Which stage of processing various kinds of weak pointer are we at?
71 * (see traverse_weak_ptr_list() below for discussion).
73 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
74 static WeakStage weak_stage;
78 StgWeak *old_weak_ptr_list; // also pending finaliser list
80 // List of threads found to be unreachable
81 StgTSO *resurrected_threads;
83 // List of blocked threads found to have pending throwTos
84 StgTSO *exception_threads;
86 static void resurrectUnreachableThreads (generation *gen);
87 static rtsBool tidyThreadList (generation *gen);
92 old_weak_ptr_list = weak_ptr_list;
94 weak_stage = WeakPtrs;
95 resurrected_threads = END_TSO_QUEUE;
96 exception_threads = END_TSO_QUEUE;
100 traverseWeakPtrList(void)
102 StgWeak *w, **last_w, *next_w;
104 rtsBool flag = rtsFalse;
105 const StgInfoTable *info;
107 switch (weak_stage) {
113 /* doesn't matter where we evacuate values/finalizers to, since
114 * these pointers are treated as roots (iff the keys are alive).
118 last_w = &old_weak_ptr_list;
119 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
121 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
122 * called on a live weak pointer object. Just remove it.
124 if (w->header.info == &stg_DEAD_WEAK_info) {
125 next_w = ((StgDeadWeak *)w)->link;
130 info = w->header.info;
131 if (IS_FORWARDING_PTR(info)) {
132 next_w = (StgWeak *)UN_FORWARDING_PTR(info);
137 switch (INFO_PTR_TO_STRUCT(info)->type) {
140 /* Now, check whether the key is reachable.
142 new = isAlive(w->key);
145 // evacuate the value and finalizer
147 evacuate(&w->finalizer);
148 // remove this weak ptr from the old_weak_ptr list
150 // and put it on the new weak ptr list
152 w->link = weak_ptr_list;
156 debugTrace(DEBUG_weak,
157 "weak pointer still alive at %p -> %p",
168 barf("traverseWeakPtrList: not WEAK");
172 /* If we didn't make any changes, then we can go round and kill all
173 * the dead weak pointers. The old_weak_ptr list is used as a list
174 * of pending finalizers later on.
176 if (flag == rtsFalse) {
177 for (w = old_weak_ptr_list; w; w = w->link) {
178 evacuate(&w->finalizer);
181 // Next, move to the WeakThreads stage after fully
182 // scavenging the finalizers we've just evacuated.
183 weak_stage = WeakThreads;
189 /* Now deal with the step->threads lists, which behave somewhat like
190 * the weak ptr list. If we discover any threads that are about to
191 * become garbage, we wake them up and administer an exception.
196 // Traverse thread lists for generations we collected...
197 // ToDo when we have one gen per capability:
198 // for (n = 0; n < n_capabilities; n++) {
199 // if (tidyThreadList(&nurseries[n])) {
203 for (g = 0; g <= N; g++) {
204 if (tidyThreadList(&generations[g])) {
209 /* If we evacuated any threads, we need to go back to the scavenger.
211 if (flag) return rtsTrue;
213 /* And resurrect any threads which were about to become garbage.
217 for (g = 0; g <= N; g++) {
218 resurrectUnreachableThreads(&generations[g]);
222 /* Finally, we can update the blackhole_queue. This queue
223 * simply strings together TSOs blocked on black holes, it is
224 * not intended to keep anything alive. Hence, we do not follow
225 * pointers on the blackhole_queue until now, when we have
226 * determined which TSOs are otherwise reachable. We know at
227 * this point that all TSOs have been evacuated, however.
231 for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->_link)) {
232 *pt = (StgTSO *)isAlive((StgClosure *)*pt);
237 weak_stage = WeakDone; // *now* we're done,
238 return rtsTrue; // but one more round of scavenging, please
242 barf("traverse_weak_ptr_list");
247 static void resurrectUnreachableThreads (generation *gen)
249 StgTSO *t, *tmp, *next;
251 for (t = gen->old_threads; t != END_TSO_QUEUE; t = next) {
252 next = t->global_link;
254 // ThreadFinished and ThreadComplete: we have to keep
255 // these on the all_threads list until they
256 // become garbage, because they might get
257 // pending exceptions.
258 switch (t->what_next) {
264 evacuate((StgClosure **)&tmp);
265 tmp->global_link = resurrected_threads;
266 resurrected_threads = tmp;
271 static rtsBool tidyThreadList (generation *gen)
273 StgTSO *t, *tmp, *next, **prev;
274 rtsBool flag = rtsFalse;
276 prev = &gen->old_threads;
278 for (t = gen->old_threads; t != END_TSO_QUEUE; t = next) {
280 tmp = (StgTSO *)isAlive((StgClosure *)t);
286 ASSERT(get_itbl(t)->type == TSO);
287 if (t->what_next == ThreadRelocated) {
293 next = t->global_link;
295 // This is a good place to check for blocked
296 // exceptions. It might be the case that a thread is
297 // blocked on delivering an exception to a thread that
298 // is also blocked - we try to ensure that this
299 // doesn't happen in throwTo(), but it's too hard (or
300 // impossible) to close all the race holes, so we
301 // accept that some might get through and deal with
302 // them here. A GC will always happen at some point,
303 // even if the system is otherwise deadlocked.
305 // If an unreachable thread has blocked
306 // exceptions, we really want to perform the
307 // blocked exceptions rather than throwing
308 // BlockedIndefinitely exceptions. This is the
309 // only place we can discover such threads.
310 // The target thread might even be
311 // ThreadFinished or ThreadKilled. Bugs here
312 // will only be seen when running on a
314 if (t->blocked_exceptions != END_TSO_QUEUE) {
316 evacuate((StgClosure **)&t);
319 t->global_link = exception_threads;
320 exception_threads = t;
326 // not alive (yet): leave this thread on the
327 // old_all_threads list.
328 prev = &(t->global_link);
334 // move this thread onto the correct threads list.
336 new_gen = Bdescr((P_)t)->gen;
337 t->global_link = new_gen->threads;
338 new_gen->threads = t;
345 /* -----------------------------------------------------------------------------
348 Threads on this list behave like weak pointers during the normal
349 phase of garbage collection: if the blackhole is reachable, then
350 the thread is reachable too.
351 -------------------------------------------------------------------------- */
353 traverseBlackholeQueue (void)
355 StgTSO *prev, *t, *tmp;
362 for (t = blackhole_queue; t != END_TSO_QUEUE; prev=t, t = t->_link) {
363 // if the thread is not yet alive...
364 if (! (tmp = (StgTSO *)isAlive((StgClosure*)t))) {
365 // if the closure it is blocked on is either (a) a
366 // reachable BLAKCHOLE or (b) not a BLACKHOLE, then we
367 // make the thread alive.
368 if (!isAlive(t->block_info.closure)) {
369 type = get_itbl(t->block_info.closure)->type;
370 if (type == BLACKHOLE || type == CAF_BLACKHOLE) {
374 evacuate((StgClosure **)&t);
380 // no write barrier when on the blackhole queue,
381 // because we traverse the whole queue on every GC.
388 /* -----------------------------------------------------------------------------
389 After GC, the live weak pointer list may have forwarding pointers
390 on it, because a weak pointer object was evacuated after being
391 moved to the live weak pointer list. We remove those forwarding
394 Also, we don't consider weak pointer objects to be reachable, but
395 we must nevertheless consider them to be "live" and retain them.
396 Therefore any weak pointer objects which haven't as yet been
397 evacuated need to be evacuated now.
398 -------------------------------------------------------------------------- */
401 markWeakPtrList ( void )
403 StgWeak *w, **last_w, *tmp;
405 last_w = &weak_ptr_list;
406 for (w = weak_ptr_list; w; w = w->link) {
407 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
408 ASSERT(IS_FORWARDING_PTR(w->header.info)
409 || w->header.info == &stg_DEAD_WEAK_info
410 || get_itbl(w)->type == WEAK);
412 evacuate((StgClosure **)&tmp);