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