Here is my understanding from Lua 5.4.7 code. If there are any mistakes, please point them out.
For example you have a `G_OLD` and `black` table named `foo`, then setmetabletable for `foo`: `setmetabletable(foo, mt)`. mt is newly created. Then it triggers barrier.
in `luaC_barrier_`
set `mt`'s age to `G_OLD0`, set `mt`'s color to `gray`. the `mt` is in `g-gray` linked list.
next minor collection comes:
in `youngcollection`
in `atomic`
`mt` is traversed. color is `black`.
in `sweepgen`
set `mt`’s age to `G_OLD1`.
the objects which `mt` point to FROM `G_NEW` to `G_SURVIVAL`
next minor collection comes:
in `youngcollection`
in `markold`
set `mt`' age to `G_OLD`. if `mt` is black, then `reallymarkobject`. Make sure `mt` will be traversed in `atomic`
in `atomic`
`mt` is traversed. color is `black`.
the objects which `mt` point to FROM `G_SURVIVAL` to `G_OLD1`.
You can read
https://github.com/lua/lua/blob/master/lgc.h#L126.