What is the system stack?

1,246 views
Skip to first unread message

thwd

unread,
Sep 30, 2015, 6:26:29 AM9/30/15
to golang-nuts
runtime.systemstack() allows a function to be run on the "system stack".

Is that the stack the garbage collector uses? What are the implications on running a func on the system stack?

What happens if said func panic()s? Can other goroutines be scheduled while the system stack is running? Or is it always running anyway?

Curiosity killed the cat [1].

Ian Lance Taylor

unread,
Sep 30, 2015, 10:18:19 AM9/30/15
to thwd, golang-nuts
On Wed, Sep 30, 2015 at 3:26 AM, thwd <sedevel...@gmail.com> wrote:
> runtime.systemstack() allows a function to be run on the "system stack".
>
> Is that the stack the garbage collector uses? What are the implications on
> running a func on the system stack?

Ordinary goroutines run on a small stack. Each ordinary Go function
checks on entry that it has enough stack space to run. If it doesn't,
it allocates a new larger stack and copies the existing stack into the
new space. But of course this copying code needs to run on some
stack, and it clearly can not be the stack that has run out of room.

The system stack is the stack that the operating system creates for a
new thread. It's larger than a goroutine stack. Stack copying is run
on this stack. There is one system stack per thread, not per
goroutine. In general there are many more goroutines than threads.

Some garbage collection steps are also run on the system stack, to do
things like provide a clear separation between what the GC is doing
and examining the stack for live objects that should not be collected.

The implication of running a function on the system stack is that the
function is not going to be able to grow the stack, and it can't be
preempted by the scheduler. So it can only be used for short
self-contained operations.


> What happens if said func panic()s? Can other goroutines be scheduled while
> the system stack is running? Or is it always running anyway?

A function running on the system stack should not panic.

Other goroutines can run, on different threads, while one goroutine is
running on the system stack.

Ian
Reply all
Reply to author
Forward
0 new messages