update copyrights in rts/sm
[ghc.git] / rts / sm / MarkWeak.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 1998-2008
4 *
5 * Weak pointers and weak-like things in the GC
6 *
7 * Documentation on the architecture of the Garbage Collector can be
8 * found in the online commentary:
9 *
10 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
11 *
12 * ---------------------------------------------------------------------------*/
13
14 #include "Rts.h"
15 #include "Storage.h"
16 #include "MarkWeak.h"
17 #include "GC.h"
18 #include "GCThread.h"
19 #include "Evac.h"
20 #include "Trace.h"
21 #include "Schedule.h"
22
23 /* -----------------------------------------------------------------------------
24 Weak Pointers
25
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).
29
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
35 dead.
36
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.
41
42 There are three distinct stages to processing weak pointers:
43
44 - weak_stage == WeakPtrs
45
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
50 run them.
51
52 - weak_stage == WeakThreads
53
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.
60
61 - weak_stage == WeakDone
62
63 No more evacuation is done.
64
65 -------------------------------------------------------------------------- */
66
67 /* Which stage of processing various kinds of weak pointer are we at?
68 * (see traverse_weak_ptr_list() below for discussion).
69 */
70 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
71 static WeakStage weak_stage;
72
73 /* Weak pointers
74 */
75 StgWeak *old_weak_ptr_list; // also pending finaliser list
76
77 /* List of all threads during GC
78 */
79 StgTSO *resurrected_threads;
80 static StgTSO *old_all_threads;
81
82 void
83 initWeakForGC(void)
84 {
85 old_weak_ptr_list = weak_ptr_list;
86 weak_ptr_list = NULL;
87 weak_stage = WeakPtrs;
88
89 /* The all_threads list is like the weak_ptr_list.
90 * See traverseWeakPtrList() for the details.
91 */
92 old_all_threads = all_threads;
93 all_threads = END_TSO_QUEUE;
94 resurrected_threads = END_TSO_QUEUE;
95 }
96
97 rtsBool
98 traverseWeakPtrList(void)
99 {
100 StgWeak *w, **last_w, *next_w;
101 StgClosure *new;
102 rtsBool flag = rtsFalse;
103
104 switch (weak_stage) {
105
106 case WeakDone:
107 return rtsFalse;
108
109 case WeakPtrs:
110 /* doesn't matter where we evacuate values/finalizers to, since
111 * these pointers are treated as roots (iff the keys are alive).
112 */
113 gct->evac_step = 0;
114
115 last_w = &old_weak_ptr_list;
116 for (w = old_weak_ptr_list; w != NULL; w = next_w) {
117
118 /* There might be a DEAD_WEAK on the list if finalizeWeak# was
119 * called on a live weak pointer object. Just remove it.
120 */
121 if (w->header.info == &stg_DEAD_WEAK_info) {
122 next_w = ((StgDeadWeak *)w)->link;
123 *last_w = next_w;
124 continue;
125 }
126
127 switch (get_itbl(w)->type) {
128
129 case EVACUATED:
130 next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
131 *last_w = next_w;
132 continue;
133
134 case WEAK:
135 /* Now, check whether the key is reachable.
136 */
137 new = isAlive(w->key);
138 if (new != NULL) {
139 w->key = new;
140 // evacuate the value and finalizer
141 evacuate(&w->value);
142 evacuate(&w->finalizer);
143 // remove this weak ptr from the old_weak_ptr list
144 *last_w = w->link;
145 // and put it on the new weak ptr list
146 next_w = w->link;
147 w->link = weak_ptr_list;
148 weak_ptr_list = w;
149 flag = rtsTrue;
150
151 debugTrace(DEBUG_weak,
152 "weak pointer still alive at %p -> %p",
153 w, w->key);
154 continue;
155 }
156 else {
157 last_w = &(w->link);
158 next_w = w->link;
159 continue;
160 }
161
162 default:
163 barf("traverseWeakPtrList: not WEAK");
164 }
165 }
166
167 /* If we didn't make any changes, then we can go round and kill all
168 * the dead weak pointers. The old_weak_ptr list is used as a list
169 * of pending finalizers later on.
170 */
171 if (flag == rtsFalse) {
172 for (w = old_weak_ptr_list; w; w = w->link) {
173 evacuate(&w->finalizer);
174 }
175
176 // Next, move to the WeakThreads stage after fully
177 // scavenging the finalizers we've just evacuated.
178 weak_stage = WeakThreads;
179 }
180
181 return rtsTrue;
182
183 case WeakThreads:
184 /* Now deal with the all_threads list, which behaves somewhat like
185 * the weak ptr list. If we discover any threads that are about to
186 * become garbage, we wake them up and administer an exception.
187 */
188 {
189 StgTSO *t, *tmp, *next, **prev;
190
191 prev = &old_all_threads;
192 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
193
194 tmp = (StgTSO *)isAlive((StgClosure *)t);
195
196 if (tmp != NULL) {
197 t = tmp;
198 }
199
200 ASSERT(get_itbl(t)->type == TSO);
201 switch (t->what_next) {
202 case ThreadRelocated:
203 next = t->link;
204 *prev = next;
205 continue;
206 case ThreadKilled:
207 case ThreadComplete:
208 // finshed or died. The thread might still be alive, but we
209 // don't keep it on the all_threads list. Don't forget to
210 // stub out its global_link field.
211 next = t->global_link;
212 t->global_link = END_TSO_QUEUE;
213 *prev = next;
214 continue;
215 default:
216 ;
217 }
218
219 if (tmp == NULL) {
220 // not alive (yet): leave this thread on the
221 // old_all_threads list.
222 prev = &(t->global_link);
223 next = t->global_link;
224 }
225 else {
226 // alive: move this thread onto the all_threads list.
227 next = t->global_link;
228 t->global_link = all_threads;
229 all_threads = t;
230 *prev = next;
231 }
232 }
233 }
234
235 /* If we evacuated any threads, we need to go back to the scavenger.
236 */
237 if (flag) return rtsTrue;
238
239 /* And resurrect any threads which were about to become garbage.
240 */
241 {
242 StgTSO *t, *tmp, *next;
243 for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
244 next = t->global_link;
245 tmp = t;
246 evacuate((StgClosure **)&tmp);
247 tmp->global_link = resurrected_threads;
248 resurrected_threads = tmp;
249 }
250 }
251
252 /* Finally, we can update the blackhole_queue. This queue
253 * simply strings together TSOs blocked on black holes, it is
254 * not intended to keep anything alive. Hence, we do not follow
255 * pointers on the blackhole_queue until now, when we have
256 * determined which TSOs are otherwise reachable. We know at
257 * this point that all TSOs have been evacuated, however.
258 */
259 {
260 StgTSO **pt;
261 for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->link)) {
262 *pt = (StgTSO *)isAlive((StgClosure *)*pt);
263 ASSERT(*pt != NULL);
264 }
265 }
266
267 weak_stage = WeakDone; // *now* we're done,
268 return rtsTrue; // but one more round of scavenging, please
269
270 default:
271 barf("traverse_weak_ptr_list");
272 return rtsTrue;
273 }
274
275 }
276
277 /* -----------------------------------------------------------------------------
278 The blackhole queue
279
280 Threads on this list behave like weak pointers during the normal
281 phase of garbage collection: if the blackhole is reachable, then
282 the thread is reachable too.
283 -------------------------------------------------------------------------- */
284 rtsBool
285 traverseBlackholeQueue (void)
286 {
287 StgTSO *prev, *t, *tmp;
288 rtsBool flag;
289 nat type;
290
291 flag = rtsFalse;
292 prev = NULL;
293
294 for (t = blackhole_queue; t != END_TSO_QUEUE; prev=t, t = t->link) {
295 // if the thread is not yet alive...
296 if (! (tmp = (StgTSO *)isAlive((StgClosure*)t))) {
297 // if the closure it is blocked on is either (a) a
298 // reachable BLAKCHOLE or (b) not a BLACKHOLE, then we
299 // make the thread alive.
300 if (!isAlive(t->block_info.closure)) {
301 type = get_itbl(t->block_info.closure)->type;
302 if (type == BLACKHOLE || type == CAF_BLACKHOLE) {
303 continue;
304 }
305 }
306 tmp = t;
307 evacuate((StgClosure **)&tmp);
308 if (prev) prev->link = t;
309 flag = rtsTrue;
310 }
311 }
312 return flag;
313 }
314
315 /* -----------------------------------------------------------------------------
316 After GC, the live weak pointer list may have forwarding pointers
317 on it, because a weak pointer object was evacuated after being
318 moved to the live weak pointer list. We remove those forwarding
319 pointers here.
320
321 Also, we don't consider weak pointer objects to be reachable, but
322 we must nevertheless consider them to be "live" and retain them.
323 Therefore any weak pointer objects which haven't as yet been
324 evacuated need to be evacuated now.
325 -------------------------------------------------------------------------- */
326
327 void
328 markWeakPtrList ( void )
329 {
330 StgWeak *w, **last_w, *tmp;
331
332 last_w = &weak_ptr_list;
333 for (w = weak_ptr_list; w; w = w->link) {
334 // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
335 ASSERT(w->header.info == &stg_DEAD_WEAK_info
336 || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
337 tmp = w;
338 evacuate((StgClosure **)&tmp);
339 *last_w = w;
340 last_w = &(w->link);
341 }
342 }
343