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 * ---------------------------------------------------------------------------*/
23 /* -----------------------------------------------------------------------------
26 traverse_weak_ptr_list is called possibly many times during garbage
27 collection. It returns a flag indicating whether it did any work
28 (i.e. called evacuate on any live pointers).
30 Invariant: traverse_weak_ptr_list is called when the heap is in an
31 idempotent state. That means that there are no pending
32 evacuate/scavenge operations. This invariant helps the weak
33 pointer code decide which weak pointers are dead - if there are no
34 new live weak pointers, then all the currently unreachable ones are
37 For generational GC: we just don't try to finalize weak pointers in
38 older generations than the one we're collecting. This could
39 probably be optimised by keeping per-generation lists of weak
40 pointers, but for a few weak pointers this scheme will work.
42 There are three distinct stages to processing weak pointers:
44 - weak_stage == WeakPtrs
46 We process all the weak pointers whos keys are alive (evacuate
47 their values and finalizers), and repeat until we can find no new
48 live keys. If no live keys are found in this pass, then we
49 evacuate the finalizers of all the dead weak pointers in order to
52 - weak_stage == WeakThreads
54 Now, we discover which *threads* are still alive. Pointers to
55 threads from the all_threads and main thread lists are the
56 weakest of all: a pointers from the finalizer of a dead weak
57 pointer can keep a thread alive. Any threads found to be unreachable
58 are evacuated and placed on the resurrected_threads list so we
59 can send them a signal later.
61 - weak_stage == WeakDone
63 No more evacuation is done.
65 -------------------------------------------------------------------------- */
67 /* Which stage of processing various kinds of weak pointer are we at?
68 * (see traverse_weak_ptr_list() below for discussion).
70 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
71 static WeakStage weak_stage;
75 StgWeak *old_weak_ptr_list; // also pending finaliser list
77 /* List of all threads during GC
79 StgTSO *resurrected_threads;
84 old_weak_ptr_list = weak_ptr_list;
86 weak_stage = WeakPtrs;
87 resurrected_threads = END_TSO_QUEUE;
91 traverseWeakPtrList(void)
93 StgWeak *w, **last_w, *next_w;
95 rtsBool flag = rtsFalse;
103 /* doesn't matter where we evacuate values/finalizers to, since
104 * these pointers are treated as roots (iff the keys are alive).
108 last_w = &old_weak_ptr_list;
109 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
111 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
112 * called on a live weak pointer object. Just remove it.
114 if (w->header.info == &stg_DEAD_WEAK_info) {
115 next_w = ((StgDeadWeak *)w)->link;
120 switch (get_itbl(w)->type) {
123 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
128 /* Now, check whether the key is reachable.
130 new = isAlive(w->key);
133 // evacuate the value and finalizer
135 evacuate(&w->finalizer);
136 // remove this weak ptr from the old_weak_ptr list
138 // and put it on the new weak ptr list
140 w->link = weak_ptr_list;
144 debugTrace(DEBUG_weak,
145 "weak pointer still alive at %p -> %p",
156 barf("traverseWeakPtrList: not WEAK");
160 /* If we didn't make any changes, then we can go round and kill all
161 * the dead weak pointers. The old_weak_ptr list is used as a list
162 * of pending finalizers later on.
164 if (flag == rtsFalse) {
165 for (w = old_weak_ptr_list; w; w = w->link) {
166 evacuate(&w->finalizer);
169 // Next, move to the WeakThreads stage after fully
170 // scavenging the finalizers we've just evacuated.
171 weak_stage = WeakThreads;
177 /* Now deal with the all_threads list, which behaves somewhat like
178 * the weak ptr list. If we discover any threads that are about to
179 * become garbage, we wake them up and administer an exception.
182 StgTSO *t, *tmp, *next, **prev;
186 // Traverse thread lists for generations we collected...
187 for (g = 0; g <= N; g++) {
188 for (s = 0; s < generations[g].n_steps; s++) {
189 stp = &generations[g].steps[s];
191 prev = &stp->old_threads;
193 for (t = stp->old_threads; t != END_TSO_QUEUE; t = next) {
195 tmp = (StgTSO *)isAlive((StgClosure *)t);
201 ASSERT(get_itbl(t)->type == TSO);
202 switch (t->what_next) {
203 case ThreadRelocated:
209 // finshed or died. The thread might still
210 // be alive, but we don't keep it on the
211 // all_threads list. Don't forget to
212 // stub out its global_link field.
213 next = t->global_link;
214 t->global_link = END_TSO_QUEUE;
222 // not alive (yet): leave this thread on the
223 // old_all_threads list.
224 prev = &(t->global_link);
225 next = t->global_link;
229 // alive: move this thread onto the correct
231 next = t->global_link;
232 new_step = Bdescr((P_)t)->step;
233 t->global_link = new_step->threads;
234 new_step->threads = t;
242 /* If we evacuated any threads, we need to go back to the scavenger.
244 if (flag) return rtsTrue;
246 /* And resurrect any threads which were about to become garbage.
251 StgTSO *t, *tmp, *next;
253 for (g = 0; g <= N; g++) {
254 for (s = 0; s < generations[g].n_steps; s++) {
255 stp = &generations[g].steps[s];
257 for (t = stp->old_threads; t != END_TSO_QUEUE; t = next) {
258 next = t->global_link;
260 evacuate((StgClosure **)&tmp);
261 tmp->global_link = resurrected_threads;
262 resurrected_threads = tmp;
268 /* Finally, we can update the blackhole_queue. This queue
269 * simply strings together TSOs blocked on black holes, it is
270 * not intended to keep anything alive. Hence, we do not follow
271 * pointers on the blackhole_queue until now, when we have
272 * determined which TSOs are otherwise reachable. We know at
273 * this point that all TSOs have been evacuated, however.
277 for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->_link)) {
278 *pt = (StgTSO *)isAlive((StgClosure *)*pt);
283 weak_stage = WeakDone; // *now* we're done,
284 return rtsTrue; // but one more round of scavenging, please
287 barf("traverse_weak_ptr_list");
293 /* -----------------------------------------------------------------------------
296 Threads on this list behave like weak pointers during the normal
297 phase of garbage collection: if the blackhole is reachable, then
298 the thread is reachable too.
299 -------------------------------------------------------------------------- */
301 traverseBlackholeQueue (void)
303 StgTSO *prev, *t, *tmp;
310 for (t = blackhole_queue; t != END_TSO_QUEUE; prev=t, t = t->_link) {
311 // if the thread is not yet alive...
312 if (! (tmp = (StgTSO *)isAlive((StgClosure*)t))) {
313 // if the closure it is blocked on is either (a) a
314 // reachable BLAKCHOLE or (b) not a BLACKHOLE, then we
315 // make the thread alive.
316 if (!isAlive(t->block_info.closure)) {
317 type = get_itbl(t->block_info.closure)->type;
318 if (type == BLACKHOLE || type == CAF_BLACKHOLE) {
322 evacuate((StgClosure **)&t);
323 if (prev) prev->_link = t;
324 // no write barrier when on the blackhole queue,
325 // because we traverse the whole queue on every GC.
332 /* -----------------------------------------------------------------------------
333 After GC, the live weak pointer list may have forwarding pointers
334 on it, because a weak pointer object was evacuated after being
335 moved to the live weak pointer list. We remove those forwarding
338 Also, we don't consider weak pointer objects to be reachable, but
339 we must nevertheless consider them to be "live" and retain them.
340 Therefore any weak pointer objects which haven't as yet been
341 evacuated need to be evacuated now.
342 -------------------------------------------------------------------------- */
345 markWeakPtrList ( void )
347 StgWeak *w, **last_w, *tmp;
349 last_w = &weak_ptr_list;
350 for (w = weak_ptr_list; w; w = w->link) {
351 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
352 ASSERT(w->header.info == &stg_DEAD_WEAK_info
353 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
355 evacuate((StgClosure **)&tmp);