Skip to content

Instantly share code, notes, and snippets.

@agrim123
Created July 30, 2020 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrim123/29c74c636383ce866e5c67f4fae59552 to your computer and use it in GitHub Desktop.
Save agrim123/29c74c636383ce866e5c67f4fae59552 to your computer and use it in GitHub Desktop.
g0

g0 has a fixed and larger stack. This allows Go to perform operations where a bigger stack is needed, and when it is preferable for the stack not to grow.

Responsibilities of g0

Goroutine creation

On calling go func() {}, GO delegates the goroutine creation to g0 before putting it in local queue. Newly created goroutines are placed on top of local queue and are run on priority.

Defer functions allocations

Garbage collector operations

stopping the world, scanning the stack of the goroutines, and some of the marking and sweeping operations.

Stack growth

When needed, Go increases the size of the goroutines. This operation is done by g0 in the prolog functions.

Scheduling goroutine

Go limits the number of OS thread running thanks to GOMAXPROCS variable simultaneously. That means Go has to schedule and manage goroutines on each of the running threads. This role is delegated to a special goroutine, called g0, that is the first goroutine created for EACH OS thread. Then, it will schedule ready goroutines to run on the threads.

Example:

ch := make(chan int)
[...]
ch <- v

When blocking on channels, the current goroutine will be parked (in waiting mode) and will not be pushed to any queues. g0 replaces this parked goroutine and does round one scheduling. The local queue has priority over global queue.

The parked goroutine gets unblocked as soon as a receiver comes with reading the channel.

v := <-ch

The goroutine receiving the message will switch to g0 and unlock the parked goroutine by putting it on the local queue.

@agrim123
Copy link
Author

1_UbwdgTQ2sgQIVVMDiM-qvg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment