1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2006
5 * Weak pointers and weak-like things in the GC
7 * ---------------------------------------------------------------------------*/
17 /* -----------------------------------------------------------------------------
20 traverse_weak_ptr_list is called possibly many times during garbage
21 collection. It returns a flag indicating whether it did any work
22 (i.e. called evacuate on any live pointers).
24 Invariant: traverse_weak_ptr_list is called when the heap is in an
25 idempotent state. That means that there are no pending
26 evacuate/scavenge operations. This invariant helps the weak
27 pointer code decide which weak pointers are dead - if there are no
28 new live weak pointers, then all the currently unreachable ones are
31 For generational GC: we just don't try to finalize weak pointers in
32 older generations than the one we're collecting. This could
33 probably be optimised by keeping per-generation lists of weak
34 pointers, but for a few weak pointers this scheme will work.
36 There are three distinct stages to processing weak pointers:
38 - weak_stage == WeakPtrs
40 We process all the weak pointers whos keys are alive (evacuate
41 their values and finalizers), and repeat until we can find no new
42 live keys. If no live keys are found in this pass, then we
43 evacuate the finalizers of all the dead weak pointers in order to
46 - weak_stage == WeakThreads
48 Now, we discover which *threads* are still alive. Pointers to
49 threads from the all_threads and main thread lists are the
50 weakest of all: a pointers from the finalizer of a dead weak
51 pointer can keep a thread alive. Any threads found to be unreachable
52 are evacuated and placed on the resurrected_threads list so we
53 can send them a signal later.
55 - weak_stage == WeakDone
57 No more evacuation is done.
59 -------------------------------------------------------------------------- */
61 /* Which stage of processing various kinds of weak pointer are we at?
62 * (see traverse_weak_ptr_list() below for discussion).
64 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
65 static WeakStage weak_stage;
69 StgWeak *old_weak_ptr_list; // also pending finaliser list
71 /* List of all threads during GC
73 StgTSO *resurrected_threads;
74 static StgTSO *old_all_threads;
79 old_weak_ptr_list = weak_ptr_list;
81 weak_stage = WeakPtrs;
83 /* The all_threads list is like the weak_ptr_list.
84 * See traverseWeakPtrList() for the details.
86 old_all_threads = all_threads;
87 all_threads = END_TSO_QUEUE;
88 resurrected_threads = END_TSO_QUEUE;
92 traverseWeakPtrList(void)
94 StgWeak *w, **last_w, *next_w;
96 rtsBool flag = rtsFalse;
104 /* doesn't matter where we evacuate values/finalizers to, since
105 * these pointers are treated as roots (iff the keys are alive).
109 last_w = &old_weak_ptr_list;
110 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
112 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
113 * called on a live weak pointer object. Just remove it.
115 if (w->header.info == &stg_DEAD_WEAK_info) {
116 next_w = ((StgDeadWeak *)w)->link;
121 switch (get_itbl(w)->type) {
124 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
129 /* Now, check whether the key is reachable.
131 new = isAlive(w->key);
134 // evacuate the value and finalizer
135 w->value = evacuate(w->value);
136 w->finalizer = evacuate(w->finalizer);
137 // remove this weak ptr from the old_weak_ptr list
139 // and put it on the new weak ptr list
141 w->link = weak_ptr_list;
145 debugTrace(DEBUG_weak,
146 "weak pointer still alive at %p -> %p",
157 barf("traverseWeakPtrList: not WEAK");
161 /* If we didn't make any changes, then we can go round and kill all
162 * the dead weak pointers. The old_weak_ptr list is used as a list
163 * of pending finalizers later on.
165 if (flag == rtsFalse) {
166 for (w = old_weak_ptr_list; w; w = w->link) {
167 w->finalizer = evacuate(w->finalizer);
170 // Next, move to the WeakThreads stage after fully
171 // scavenging the finalizers we've just evacuated.
172 weak_stage = WeakThreads;
178 /* Now deal with the all_threads list, which behaves somewhat like
179 * the weak ptr list. If we discover any threads that are about to
180 * become garbage, we wake them up and administer an exception.
183 StgTSO *t, *tmp, *next, **prev;
185 prev = &old_all_threads;
186 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
188 tmp = (StgTSO *)isAlive((StgClosure *)t);
194 ASSERT(get_itbl(t)->type == TSO);
195 switch (t->what_next) {
196 case ThreadRelocated:
202 // finshed or died. The thread might still be alive, but we
203 // don't keep it on the all_threads list. Don't forget to
204 // stub out its global_link field.
205 next = t->global_link;
206 t->global_link = END_TSO_QUEUE;
214 // not alive (yet): leave this thread on the
215 // old_all_threads list.
216 prev = &(t->global_link);
217 next = t->global_link;
220 // alive: move this thread onto the all_threads list.
221 next = t->global_link;
222 t->global_link = all_threads;
229 /* If we evacuated any threads, we need to go back to the scavenger.
231 if (flag) return rtsTrue;
233 /* And resurrect any threads which were about to become garbage.
236 StgTSO *t, *tmp, *next;
237 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
238 next = t->global_link;
239 tmp = (StgTSO *)evacuate((StgClosure *)t);
240 tmp->global_link = resurrected_threads;
241 resurrected_threads = tmp;
245 /* Finally, we can update the blackhole_queue. This queue
246 * simply strings together TSOs blocked on black holes, it is
247 * not intended to keep anything alive. Hence, we do not follow
248 * pointers on the blackhole_queue until now, when we have
249 * determined which TSOs are otherwise reachable. We know at
250 * this point that all TSOs have been evacuated, however.
254 for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->link)) {
255 *pt = (StgTSO *)isAlive((StgClosure *)*pt);
260 weak_stage = WeakDone; // *now* we're done,
261 return rtsTrue; // but one more round of scavenging, please
264 barf("traverse_weak_ptr_list");
270 /* -----------------------------------------------------------------------------
273 Threads on this list behave like weak pointers during the normal
274 phase of garbage collection: if the blackhole is reachable, then
275 the thread is reachable too.
276 -------------------------------------------------------------------------- */
278 traverseBlackholeQueue (void)
280 StgTSO *prev, *t, *tmp;
286 for (t = blackhole_queue; t != END_TSO_QUEUE; prev=t, t = t->link) {
287 if (! (tmp = (StgTSO *)isAlive((StgClosure*)t))) {
288 if (isAlive(t->block_info.closure)) {
289 t = (StgTSO *)evacuate((StgClosure *)t);
290 if (prev) prev->link = t;
298 /* -----------------------------------------------------------------------------
299 After GC, the live weak pointer list may have forwarding pointers
300 on it, because a weak pointer object was evacuated after being
301 moved to the live weak pointer list. We remove those forwarding
304 Also, we don't consider weak pointer objects to be reachable, but
305 we must nevertheless consider them to be "live" and retain them.
306 Therefore any weak pointer objects which haven't as yet been
307 evacuated need to be evacuated now.
308 -------------------------------------------------------------------------- */
311 markWeakPtrList ( void )
313 StgWeak *w, **last_w;
315 last_w = &weak_ptr_list;
316 for (w = weak_ptr_list; w; w = w->link) {
317 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
318 ASSERT(w->header.info == &stg_DEAD_WEAK_info
319 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
320 w = (StgWeak *)evacuate((StgClosure *)w);