** Traverse a thread, marking the elements in the stack up to its top ** and cleaning the rest of the stack in the final traversal. That ** ensures that the entire stack have valid (non-dead) objects. ** Threads have no barriers. In gen. mode, old threads must be visited ** at every cycle, because they might point to young objects. In inc. ** mode, the thread can still be modified before the end
| 619 | ** the propagate phase (which can only happen in incremental mode). |
| 620 | */ |
| 621 | static int traversethread (global_State *g, lua_State *th) { |
| 622 | UpVal *uv; |
| 623 | StkId o = th->stack; |
| 624 | if (isold(th) || g->gcstate == GCSpropagate) |
| 625 | linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ |
| 626 | if (o == NULL) |
| 627 | return 1; /* stack not completely built yet */ |
| 628 | lua_assert(g->gcstate == GCSatomic || |
| 629 | th->openupval == NULL || isintwups(th)); |
| 630 | for (; o < th->top; o++) /* mark live elements in the stack */ |
| 631 | markvalue(g, s2v(o)); |
| 632 | for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) |
| 633 | markobject(g, uv); /* open upvalues cannot be collected */ |
| 634 | if (g->gcstate == GCSatomic) { /* final traversal? */ |
| 635 | for (; o < th->stack_last + EXTRA_STACK; o++) |
| 636 | setnilvalue(s2v(o)); /* clear dead stack slice */ |
| 637 | /* 'remarkupvals' may have removed thread from 'twups' list */ |
| 638 | if (!isintwups(th) && th->openupval != NULL) { |
| 639 | th->twups = g->twups; /* link it back to the list */ |
| 640 | g->twups = th; |
| 641 | } |
| 642 | } |
| 643 | else if (!g->gcemergency) |
| 644 | luaD_shrinkstack(th); /* do not change stack in emergency cycle */ |
| 645 | return 1 + stacksize(th); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | /* |
no test coverage detected