diff options
Diffstat (limited to 'notes/gc-design.md')
| -rw-r--r-- | notes/gc-design.md | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/notes/gc-design.md b/notes/gc-design.md new file mode 100644 index 0000000..c8e1699 --- /dev/null +++ b/notes/gc-design.md @@ -0,0 +1,87 @@ +There are 4 states: IDLE, GREY, MARK, FREE. + +There is a global state variable for the main GC thread, and then +there is a state for each other thread (mutators and GC workers). + +Store barriers are disabled so long as a mutator believes the GC to be +IDLE and is itself in the IDLE state. + +Upon initiation, the GC sets its state to GREY, and begins waiting for +mutators to react. + +As mutators hit safepoints, they notice the state change one by one, +reacting as follows: + +1. The mutator enables a Dijkstra insertion barrier that will apply to +all pointer writes into objects marked black. It also enables a Yuasa +deletion barrier that will grey any white object removed from a +non-black object. + +(Mutable global variables don't exist as per language semantics; they +must be modeled via "box" heap objects so they can be treated +uniformly with all other shared objects.) + +2. The mutator updates its own state to GREY. This signals the GC to +check if it may continue. + +3. The mutator continues concurrent execution with the barriers +enabled. + +The GC eventually observes that all active mutators have entered the +GREY state. This implies that all GC roots across the program are now +effectively in the grey set. The GC proceeds as follows: + +1. The global GC state variable is set to MARK. + +2. The GC begins the Mark Loop. Based on configuration and heuristics, +it spawns workers that begin in the MARK phase. + +The mechanism of how the work queue is sharded is outside the scope of +this specification; solutions for this should be well-known. The only +relevant detail is that mutators push to the main work queue, and the +GC main thread dispatches work items. + +Objects are marked black with a "release" atomic store *after* they +are fully scanned, so that mutators, who use an "acquire" atomic load +to read the color, cannot see an object as being black before it's +fully scanned. + +Mutators begin to hit safepoints again, noticing the GC has entered +the MARK phase. In reaction to this, one by one, each mutator performs +the following, in order: + +1. Scans its own roots from a safepoint hook, and pushes the pointers +into the GC's work queue. + +2. Sets a local flag that enables Black Allocation, and disables the +Dijkstra barrier. + +3. Sets its own state to MARK, and then continues concurrent +execution. + +The main GC thread's mark loop eventually terminates under the +following condition: All mutators have entered the MARK phase, and no +new objects have been found in the main work queue during the last +iteration. It sets its state to FREE, and begins to wait again. + +Mutators observe, one by one, the new GC state. They ensure that all +work queue writes are flushed, and set their state to FREE as +well. Release/acquire atomic semantics can be used again, this time to +ensure that the GC thread will see the flushed writes without the need +for a lock. + +Whenever a GC worker's work queue is empty, it checks whether the main +GC state is FREE. If so, it sets itself to FREE as well; however, it +continues to accept new work items either way. + +The GC eventually observes that every mutator, and every GC worker, +has entered the FREE state. It checks the work queue one last time +(restarts the mark loop) to finish off stragglers. Once the mark loop +terminates again, all work is truly completed; the GC can sweep. + +The main GC state is set to IDLE already before sweeping begins. This +allows mutators to disable the Yuasa barrier as early as possible. + +The GC pretending to be IDLE while sweeping is not a problem, because +if a new GC initiation signal comes in while it's still sweeping, it +will simply handle it after it's done sweeping. |
