simplify weak pointer processing
[ghc-hetmet.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 "PosixSource.h"
15 #include "Rts.h"
16
17 #include "MarkWeak.h"
18 #include "GC.h"
19 #include "GCThread.h"
20 #include "Evac.h"
21 #include "Trace.h"
22 #include "Schedule.h"
23 #include "Weak.h"
24 #include "Storage.h"
25
26 /* -----------------------------------------------------------------------------
27    Weak Pointers
28
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).
32
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
38    dead.
39
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.
44
45    There are three distinct stages to processing weak pointers:
46
47    - weak_stage == WeakPtrs
48
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
53      run them.
54
55    - weak_stage == WeakThreads
56
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.
63
64    - weak_stage == WeakDone
65
66      No more evacuation is done.
67
68    -------------------------------------------------------------------------- */
69
70 /* Which stage of processing various kinds of weak pointer are we at?
71  * (see traverse_weak_ptr_list() below for discussion).
72  */
73 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
74 static WeakStage weak_stage;
75
76 /* Weak pointers
77  */
78 StgWeak *old_weak_ptr_list; // also pending finaliser list
79
80 // List of threads found to be unreachable
81 StgTSO *resurrected_threads;
82
83 // List of blocked threads found to have pending throwTos
84 StgTSO *exception_threads;
85
86 static void resurrectUnreachableThreads (generation *gen);
87 static rtsBool tidyThreadList (generation *gen);
88
89 void
90 initWeakForGC(void)
91 {
92     old_weak_ptr_list = weak_ptr_list;
93     weak_ptr_list = NULL;
94     weak_stage = WeakPtrs;
95     resurrected_threads = END_TSO_QUEUE;
96     exception_threads = END_TSO_QUEUE;
97 }
98
99 rtsBool 
100 traverseWeakPtrList(void)
101 {
102   StgWeak *w, **last_w, *next_w;
103   StgClosure *new;
104   rtsBool flag = rtsFalse;
105   const StgInfoTable *info;
106
107   switch (weak_stage) {
108
109   case WeakDone:
110       return rtsFalse;
111
112   case WeakPtrs:
113       /* doesn't matter where we evacuate values/finalizers to, since
114        * these pointers are treated as roots (iff the keys are alive).
115        */
116       gct->evac_gen = 0;
117       
118       last_w = &old_weak_ptr_list;
119       for (w = old_weak_ptr_list; w != NULL; w = next_w) {
120           
121           /* There might be a DEAD_WEAK on the list if finalizeWeak# was
122            * called on a live weak pointer object.  Just remove it.
123            */
124           if (w->header.info == &stg_DEAD_WEAK_info) {
125               next_w = ((StgDeadWeak *)w)->link;
126               *last_w = next_w;
127               continue;
128           }
129           
130           info = get_itbl(w);
131           switch (info->type) {
132
133           case WEAK:
134               /* Now, check whether the key is reachable.
135                */
136               new = isAlive(w->key);
137               if (new != NULL) {
138                   w->key = new;
139                   // evacuate the value and finalizer 
140                   evacuate(&w->value);
141                   evacuate(&w->finalizer);
142                   // remove this weak ptr from the old_weak_ptr list 
143                   *last_w = w->link;
144                   // and put it on the new weak ptr list 
145                   next_w  = w->link;
146                   w->link = weak_ptr_list;
147                   weak_ptr_list = w;
148                   flag = rtsTrue;
149
150                   debugTrace(DEBUG_weak, 
151                              "weak pointer still alive at %p -> %p",
152                              w, w->key);
153                   continue;
154               }
155               else {
156                   last_w = &(w->link);
157                   next_w = w->link;
158                   continue;
159               }
160
161           default:
162               barf("traverseWeakPtrList: not WEAK");
163           }
164       }
165       
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.
169        */
170       if (flag == rtsFalse) {
171           for (w = old_weak_ptr_list; w; w = w->link) {
172               evacuate(&w->finalizer);
173           }
174
175           // Next, move to the WeakThreads stage after fully
176           // scavenging the finalizers we've just evacuated.
177           weak_stage = WeakThreads;
178       }
179
180       return rtsTrue;
181
182   case WeakThreads:
183       /* Now deal with the step->threads lists, which behave 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.
186        */
187   {
188       nat g;
189           
190       // Traverse thread lists for generations we collected...
191 //      ToDo when we have one gen per capability:
192 //      for (n = 0; n < n_capabilities; n++) {
193 //          if (tidyThreadList(&nurseries[n])) {
194 //              flag = rtsTrue;
195 //          }
196 //      }              
197       for (g = 0; g <= N; g++) {
198           if (tidyThreadList(&generations[g])) {
199               flag = rtsTrue;
200           }
201       }
202
203       /* If we evacuated any threads, we need to go back to the scavenger.
204        */
205       if (flag) return rtsTrue;
206
207       /* And resurrect any threads which were about to become garbage.
208        */
209       {
210           nat g;
211           for (g = 0; g <= N; g++) {
212               resurrectUnreachableThreads(&generations[g]);
213           }
214       }
215         
216       /* Finally, we can update the blackhole_queue.  This queue
217        * simply strings together TSOs blocked on black holes, it is
218        * not intended to keep anything alive.  Hence, we do not follow
219        * pointers on the blackhole_queue until now, when we have
220        * determined which TSOs are otherwise reachable.  We know at
221        * this point that all TSOs have been evacuated, however.
222        */
223       { 
224           StgTSO **pt;
225           for (pt = &blackhole_queue; *pt != END_TSO_QUEUE; pt = &((*pt)->_link)) {
226               *pt = (StgTSO *)isAlive((StgClosure *)*pt);
227               ASSERT(*pt != NULL);
228           }
229       }
230       
231       weak_stage = WeakDone;  // *now* we're done,
232       return rtsTrue;         // but one more round of scavenging, please
233   }
234       
235   default:
236       barf("traverse_weak_ptr_list");
237       return rtsTrue;
238   }
239 }
240   
241   static void resurrectUnreachableThreads (generation *gen)
242 {
243     StgTSO *t, *tmp, *next;
244
245     for (t = gen->old_threads; t != END_TSO_QUEUE; t = next) {
246         next = t->global_link;
247         
248         // ThreadFinished and ThreadComplete: we have to keep
249         // these on the all_threads list until they
250         // become garbage, because they might get
251         // pending exceptions.
252         switch (t->what_next) {
253         case ThreadKilled:
254         case ThreadComplete:
255             continue;
256         default:
257             tmp = t;
258             evacuate((StgClosure **)&tmp);
259             tmp->global_link = resurrected_threads;
260             resurrected_threads = tmp;
261         }
262     }
263 }
264
265 static rtsBool tidyThreadList (generation *gen)
266 {
267     StgTSO *t, *tmp, *next, **prev;
268     rtsBool flag = rtsFalse;
269
270     prev = &gen->old_threads;
271
272     for (t = gen->old_threads; t != END_TSO_QUEUE; t = next) {
273               
274         tmp = (StgTSO *)isAlive((StgClosure *)t);
275         
276         if (tmp != NULL) {
277             t = tmp;
278         }
279         
280         ASSERT(get_itbl(t)->type == TSO);
281         if (t->what_next == ThreadRelocated) {
282             next = t->_link;
283             *prev = next;
284             continue;
285         }
286         
287         next = t->global_link;
288         
289         // This is a good place to check for blocked
290         // exceptions.  It might be the case that a thread is
291         // blocked on delivering an exception to a thread that
292         // is also blocked - we try to ensure that this
293         // doesn't happen in throwTo(), but it's too hard (or
294         // impossible) to close all the race holes, so we
295         // accept that some might get through and deal with
296         // them here.  A GC will always happen at some point,
297         // even if the system is otherwise deadlocked.
298         //
299         // If an unreachable thread has blocked
300         // exceptions, we really want to perform the
301         // blocked exceptions rather than throwing
302         // BlockedIndefinitely exceptions.  This is the
303         // only place we can discover such threads.
304         // The target thread might even be
305         // ThreadFinished or ThreadKilled.  Bugs here
306         // will only be seen when running on a
307         // multiprocessor.
308         if (t->blocked_exceptions != END_TSO_QUEUE) {
309             if (tmp == NULL) {
310                 evacuate((StgClosure **)&t);
311                 flag = rtsTrue;
312             }
313             t->global_link = exception_threads;
314             exception_threads = t;
315             *prev = next;
316             continue;
317         }
318         
319         if (tmp == NULL) {
320             // not alive (yet): leave this thread on the
321             // old_all_threads list.
322             prev = &(t->global_link);
323         } 
324         else {
325             // alive
326             *prev = next;
327             
328             // move this thread onto the correct threads list.
329             generation *new_gen;
330             new_gen = Bdescr((P_)t)->gen;
331             t->global_link = new_gen->threads;
332             new_gen->threads  = t;
333         }
334     }
335
336     return flag;
337 }
338
339 /* -----------------------------------------------------------------------------
340    The blackhole queue
341    
342    Threads on this list behave like weak pointers during the normal
343    phase of garbage collection: if the blackhole is reachable, then
344    the thread is reachable too.
345    -------------------------------------------------------------------------- */
346 rtsBool
347 traverseBlackholeQueue (void)
348 {
349     StgTSO *prev, *t, *tmp;
350     rtsBool flag;
351     nat type;
352
353     flag = rtsFalse;
354     prev = NULL;
355
356     for (t = blackhole_queue; t != END_TSO_QUEUE; prev=t, t = t->_link) {
357         // if the thread is not yet alive...
358         if (! (tmp = (StgTSO *)isAlive((StgClosure*)t))) {
359             // if the closure it is blocked on is either (a) a
360             // reachable BLAKCHOLE or (b) not a BLACKHOLE, then we
361             // make the thread alive.
362             if (!isAlive(t->block_info.closure)) {
363                 type = get_itbl(t->block_info.closure)->type;
364                 if (type == BLACKHOLE || type == CAF_BLACKHOLE) {
365                     continue;
366                 }
367             }
368             evacuate((StgClosure **)&t);
369             if (prev) {
370                 prev->_link = t;
371             } else {
372                 blackhole_queue = t;
373             }
374                  // no write barrier when on the blackhole queue,
375                  // because we traverse the whole queue on every GC.
376             flag = rtsTrue;
377         }
378     }
379     return flag;
380 }
381
382 /* -----------------------------------------------------------------------------
383    Evacuate every weak pointer object on the weak_ptr_list, and update
384    the link fields.
385
386    ToDo: with a lot of weak pointers, this will be expensive.  We
387    should have a per-GC weak pointer list, just like threads.
388    -------------------------------------------------------------------------- */
389
390 void
391 markWeakPtrList ( void )
392 {
393   StgWeak *w, **last_w;
394
395   last_w = &weak_ptr_list;
396   for (w = weak_ptr_list; w; w = w->link) {
397       // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
398       ASSERT(IS_FORWARDING_PTR(w->header.info)
399              || w->header.info == &stg_DEAD_WEAK_info 
400              || get_itbl(w)->type == WEAK);
401       evacuate((StgClosure **)last_w);
402       w = *last_w;
403       if (w->header.info == &stg_DEAD_WEAK_info) {
404           last_w = &(((StgDeadWeak*)w)->link);
405       } else {
406           last_w = &(w->link);
407       }
408   }
409 }
410