+}
+
+static rtsBool tidyThreadList (generation *gen)
+{
+ StgTSO *t, *tmp, *next, **prev;
+ rtsBool flag = rtsFalse;
+
+ prev = &gen->old_threads;
+
+ for (t = gen->old_threads; t != END_TSO_QUEUE; t = next) {
+
+ tmp = (StgTSO *)isAlive((StgClosure *)t);
+
+ if (tmp != NULL) {
+ t = tmp;
+ }
+
+ ASSERT(get_itbl(t)->type == TSO);
+ next = t->global_link;
+
+ // if the thread is not masking exceptions but there are
+ // pending exceptions on its queue, then something has gone
+ // wrong. However, pending exceptions are OK if there is an
+ // FFI call.
+ ASSERT(t->blocked_exceptions == END_BLOCKED_EXCEPTIONS_QUEUE
+ || t->why_blocked == BlockedOnCCall
+ || t->why_blocked == BlockedOnCCall_Interruptible
+ || (t->flags & TSO_BLOCKEX));
+
+ if (tmp == NULL) {
+ // not alive (yet): leave this thread on the
+ // old_all_threads list.
+ prev = &(t->global_link);
+ }
+ else {
+ // alive
+ *prev = next;
+
+ // move this thread onto the correct threads list.
+ generation *new_gen;
+ new_gen = Bdescr((P_)t)->gen;
+ t->global_link = new_gen->threads;
+ new_gen->threads = t;
+ }
+ }
+