1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2006
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 * ---------------------------------------------------------------------------*/
22 /* -----------------------------------------------------------------------------
25 traverse_weak_ptr_list is called possibly many times during garbage
26 collection. It returns a flag indicating whether it did any work
27 (i.e. called evacuate on any live pointers).
29 Invariant: traverse_weak_ptr_list is called when the heap is in an
30 idempotent state. That means that there are no pending
31 evacuate/scavenge operations. This invariant helps the weak
32 pointer code decide which weak pointers are dead - if there are no
33 new live weak pointers, then all the currently unreachable ones are
36 For generational GC: we just don't try to finalize weak pointers in
37 older generations than the one we're collecting. This could
38 probably be optimised by keeping per-generation lists of weak
39 pointers, but for a few weak pointers this scheme will work.
41 There are three distinct stages to processing weak pointers:
43 - weak_stage == WeakPtrs
45 We process all the weak pointers whos keys are alive (evacuate
46 their values and finalizers), and repeat until we can find no new
47 live keys. If no live keys are found in this pass, then we
48 evacuate the finalizers of all the dead weak pointers in order to
51 - weak_stage == WeakThreads
53 Now, we discover which *threads* are still alive. Pointers to
54 threads from the all_threads and main thread lists are the
55 weakest of all: a pointers from the finalizer of a dead weak
56 pointer can keep a thread alive. Any threads found to be unreachable
57 are evacuated and placed on the resurrected_threads list so we
58 can send them a signal later.
60 - weak_stage == WeakDone
62 No more evacuation is done.
64 -------------------------------------------------------------------------- */
66 /* Which stage of processing various kinds of weak pointer are we at?
67 * (see traverse_weak_ptr_list() below for discussion).
69 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
70 static WeakStage weak_stage;
74 StgWeak *old_weak_ptr_list; // also pending finaliser list
76 /* List of all threads during GC
78 StgTSO *resurrected_threads;
79 static StgTSO *old_all_threads;
84 old_weak_ptr_list = weak_ptr_list;
86 weak_stage = WeakPtrs;
88 /* The all_threads list is like the weak_ptr_list.
89 * See traverseWeakPtrList() for the details.
91 old_all_threads = all_threads;
92 all_threads = END_TSO_QUEUE;
93 resurrected_threads = END_TSO_QUEUE;
97 traverseWeakPtrList(void)
99 StgWeak *w, **last_w, *next_w;
101 rtsBool flag = rtsFalse;
103 switch (weak_stage) {
109 /* doesn't matter where we evacuate values/finalizers to, since
110 * these pointers are treated as roots (iff the keys are alive).
114 last_w = &old_weak_ptr_list;
115 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
117 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
118 * called on a live weak pointer object. Just remove it.
120 if (w->header.info == &stg_DEAD_WEAK_info) {
121 next_w = ((StgDeadWeak *)w)->link;
126 switch (get_itbl(w)->type) {
129 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
134 /* Now, check whether the key is reachable.
136 new = isAlive(w->key);
139 // evacuate the value and finalizer
140 w->value = evacuate(w->value);
141 w->finalizer = evacuate(w->finalizer);
142 // remove this weak ptr from the old_weak_ptr list
144 // and put it on the new weak ptr list
146 w->link = weak_ptr_list;
150 debugTrace(DEBUG_weak,
151 "weak pointer still alive at %p -> %p",
162 barf("traverseWeakPtrList: not WEAK");
166 /* If we didn't make any changes, then we can go round and kill all
167 * the dead weak pointers. The old_weak_ptr list is used as a list
168 * of pending finalizers later on.
170 if (flag == rtsFalse) {
171 for (w = old_weak_ptr_list; w; w = w->link) {
172 w->finalizer = evacuate(w->finalizer);
175 // Next, move to the WeakThreads stage after fully
176 // scavenging the finalizers we've just evacuated.
177 weak_stage = WeakThreads;
183 /* Now deal with the all_threads list, which behaves somewhat like
184 * the weak ptr list. If we discover any threads that are about to
185 * become garbage, we wake them up and administer an exception.
188 StgTSO *t, *tmp, *next, **prev;
190 prev = &old_all_threads;
191 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
193 tmp = (StgTSO *)isAlive((StgClosure *)t);
199 ASSERT(get_itbl(t)->type == TSO);
200 switch (t->what_next) {
201 case ThreadRelocated:
207 // finshed or died. The thread might still be alive, but we
208 // don't keep it on the all_threads list. Don't forget to
209 // stub out its global_link field.
210 next = t->global_link;
211 t->global_link = END_TSO_QUEUE;
219 // not alive (yet): leave this thread on the
220 // old_all_threads list.
221 prev = &(t->global_link);
222 next = t->global_link;
225 // alive: move this thread onto the all_threads list.
226 next = t->global_link;
227 t->global_link = all_threads;
234 /* If we evacuated any threads, we need to go back to the scavenger.
236 if (flag) return rtsTrue;
238 /* And resurrect any threads which were about to become garbage.
241 StgTSO *t, *tmp, *next;
242 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
243 next = t->global_link;
244 tmp = (StgTSO *)evacuate((StgClosure *)t);
245 tmp->global_link = resurrected_threads;
246 resurrected_threads = tmp;
250 /* Finally, we can update the blackhole_queue. This queue
251 * simply strings together TSOs blocked on black holes, it is
252 * not intended to keep anything alive. Hence, we do not follow
253 * pointers on the blackhole_queue until now, when we have
254 * determined which TSOs are otherwise reachable. We know at
255 * this point that all TSOs have been evacuated, however.
259 for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->link)) {
260 *pt = (StgTSO *)isAlive((StgClosure *)*pt);
265 weak_stage = WeakDone; // *now* we're done,
266 return rtsTrue; // but one more round of scavenging, please
269 barf("traverse_weak_ptr_list");
275 /* -----------------------------------------------------------------------------
278 Threads on this list behave like weak pointers during the normal
279 phase of garbage collection: if the blackhole is reachable, then
280 the thread is reachable too.
281 -------------------------------------------------------------------------- */
283 traverseBlackholeQueue (void)
285 StgTSO *prev, *t, *tmp;
292 for (t = blackhole_queue; t != END_TSO_QUEUE; prev=t, t = t->link) {
293 // if the thread is not yet alive...
294 if (! (tmp = (StgTSO *)isAlive((StgClosure*)t))) {
295 // if the closure it is blocked on is either (a) a
296 // reachable BLAKCHOLE or (b) not a BLACKHOLE, then we
297 // make the thread alive.
298 if (!isAlive(t->block_info.closure)) {
299 type = get_itbl(t->block_info.closure)->type;
300 if (type == BLACKHOLE || type == CAF_BLACKHOLE) {
304 t = (StgTSO *)evacuate((StgClosure *)t);
305 if (prev) prev->link = t;
312 /* -----------------------------------------------------------------------------
313 After GC, the live weak pointer list may have forwarding pointers
314 on it, because a weak pointer object was evacuated after being
315 moved to the live weak pointer list. We remove those forwarding
318 Also, we don't consider weak pointer objects to be reachable, but
319 we must nevertheless consider them to be "live" and retain them.
320 Therefore any weak pointer objects which haven't as yet been
321 evacuated need to be evacuated now.
322 -------------------------------------------------------------------------- */
325 markWeakPtrList ( void )
327 StgWeak *w, **last_w;
329 last_w = &weak_ptr_list;
330 for (w = weak_ptr_list; w; w = w->link) {
331 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
332 ASSERT(w->header.info == &stg_DEAD_WEAK_info
333 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
334 w = (StgWeak *)evacuate((StgClosure *)w);