FAQ
Hi,

I'm reading the source code of the runtime. I find `mstart` starts an M and
call `schedule` to run the G. But the `schedule` will never return. So how
M schedule the other goroutines? How it knows if there some system calls
that block the thread?

Thanks.

---
Bin Wang

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dmitry Vyukov at Aug 13, 2014 at 9:00 am

    On Wed, Aug 13, 2014 at 11:30 AM, Bin Wang wrote:
    Hi,

    I'm reading the source code of the runtime. I find `mstart` starts an M and
    call `schedule` to run the G. But the `schedule` will never return. So how M
    schedule the other goroutines? How it knows if there some system calls that
    block the thread?
    Each thread (M) has a scheduler stack (g0) on which the thread starts.
    Goroutines run on their own stacks. When a goroutine needs to interact
    with the scheduler it switches the thread back to g0 stack (using
    runtime.mcall) and executes a scheduler function. See e.g.
    runtime.gosched for a goroutine yield functionality.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Bin Wang at Aug 13, 2014 at 9:21 am
    After read mcall it seems to be clear. Thank you.

    2014-08-13 17:00 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 11:30 AM, Bin Wang wrote:
    Hi,

    I'm reading the source code of the runtime. I find `mstart` starts an M and
    call `schedule` to run the G. But the `schedule` will never return. So how M
    schedule the other goroutines? How it knows if there some system calls that
    block the thread?
    Each thread (M) has a scheduler stack (g0) on which the thread starts.
    Goroutines run on their own stacks. When a goroutine needs to interact
    with the scheduler it switches the thread back to g0 stack (using
    runtime.mcall) and executes a scheduler function. See e.g.
    runtime.gosched for a goroutine yield functionality.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Bin Wang at Aug 13, 2014 at 10:26 am
    Hi, Dmitry

    I read the _rt0_go and is a little confused.

    The first M's g0 is the Go programs main goroutine, is that true?

    2014-08-13 17:21 GMT+08:00 Bin Wang <wbin00@gmail.com>:
    After read mcall it seems to be clear. Thank you.

    2014-08-13 17:00 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 11:30 AM, Bin Wang wrote:
    Hi,

    I'm reading the source code of the runtime. I find `mstart` starts an M and
    call `schedule` to run the G. But the `schedule` will never return. So how M
    schedule the other goroutines? How it knows if there some system calls that
    block the thread?
    Each thread (M) has a scheduler stack (g0) on which the thread starts.
    Goroutines run on their own stacks. When a goroutine needs to interact
    with the scheduler it switches the thread back to g0 stack (using
    runtime.mcall) and executes a scheduler function. See e.g.
    runtime.gosched for a goroutine yield functionality.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dmitry Vyukov at Aug 13, 2014 at 11:36 am

    On Wed, Aug 13, 2014 at 2:26 PM, Bin Wang wrote:
    Hi, Dmitry

    I read the _rt0_go and is a little confused.

    The first M's g0 is the Go programs main goroutine, is that true?
    g0 is scheduler/system goroutine/stack associated with the thread.
    THe main goroutine is created during initialization by calling
    runtime.newproc. It initially calls runtime.main which in turn calls
    main.main.


    2014-08-13 17:21 GMT+08:00 Bin Wang <wbin00@gmail.com>:
    After read mcall it seems to be clear. Thank you.

    2014-08-13 17:00 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 11:30 AM, Bin Wang wrote:
    Hi,

    I'm reading the source code of the runtime. I find `mstart` starts an M and
    call `schedule` to run the G. But the `schedule` will never return. So how M
    schedule the other goroutines? How it knows if there some system calls that
    block the thread?
    Each thread (M) has a scheduler stack (g0) on which the thread starts.
    Goroutines run on their own stacks. When a goroutine needs to interact
    with the scheduler it switches the thread back to g0 stack (using
    runtime.mcall) and executes a scheduler function. See e.g.
    runtime.gosched for a goroutine yield functionality.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Bin Wang at Aug 14, 2014 at 3:49 am
    The `schedinit` calls `procresize` to init many Ps. The only place I
    found to init threads to run these Ps is in `starttheworld`. But I
    cannot understand how `starttheworld` is called. Could you help?
    Thanks.

    2014-08-13 19:36 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 2:26 PM, Bin Wang wrote:
    Hi, Dmitry

    I read the _rt0_go and is a little confused.

    The first M's g0 is the Go programs main goroutine, is that true?
    g0 is scheduler/system goroutine/stack associated with the thread.
    THe main goroutine is created during initialization by calling
    runtime.newproc. It initially calls runtime.main which in turn calls
    main.main.


    2014-08-13 17:21 GMT+08:00 Bin Wang <wbin00@gmail.com>:
    After read mcall it seems to be clear. Thank you.

    2014-08-13 17:00 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 11:30 AM, Bin Wang wrote:
    Hi,

    I'm reading the source code of the runtime. I find `mstart` starts an M and
    call `schedule` to run the G. But the `schedule` will never return. So how M
    schedule the other goroutines? How it knows if there some system calls that
    block the thread?
    Each thread (M) has a scheduler stack (g0) on which the thread starts.
    Goroutines run on their own stacks. When a goroutine needs to interact
    with the scheduler it switches the thread back to g0 stack (using
    runtime.mcall) and executes a scheduler function. See e.g.
    runtime.gosched for a goroutine yield functionality.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dmitry Vyukov at Aug 14, 2014 at 7:24 am

    On Thu, Aug 14, 2014 at 7:49 AM, Bin Wang wrote:
    The `schedinit` calls `procresize` to init many Ps. The only place I
    found to init threads to run these Ps is in `starttheworld`. But I
    cannot understand how `starttheworld` is called. Could you help?
    Thanks.

    Function that starts threads is startm. It's called from several places.
    What do you mean by *how* it is called? Most of these function are
    just normally called.
    If you mean where they are called, you can use search to find all call sites:
    http://golang.org/search?q=starttheworld


    2014-08-13 19:36 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 2:26 PM, Bin Wang wrote:
    Hi, Dmitry

    I read the _rt0_go and is a little confused.

    The first M's g0 is the Go programs main goroutine, is that true?
    g0 is scheduler/system goroutine/stack associated with the thread.
    THe main goroutine is created during initialization by calling
    runtime.newproc. It initially calls runtime.main which in turn calls
    main.main.


    2014-08-13 17:21 GMT+08:00 Bin Wang <wbin00@gmail.com>:
    After read mcall it seems to be clear. Thank you.

    2014-08-13 17:00 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 11:30 AM, Bin Wang wrote:
    Hi,

    I'm reading the source code of the runtime. I find `mstart` starts an M and
    call `schedule` to run the G. But the `schedule` will never return. So how M
    schedule the other goroutines? How it knows if there some system calls that
    block the thread?
    Each thread (M) has a scheduler stack (g0) on which the thread starts.
    Goroutines run on their own stacks. When a goroutine needs to interact
    with the scheduler it switches the thread back to g0 stack (using
    runtime.mcall) and executes a scheduler function. See e.g.
    runtime.gosched for a goroutine yield functionality.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Bin Wang at Aug 14, 2014 at 7:35 am
    I mean `schedinit` start many Ps, each P needs an M to run. Will all
    the Ms init at the bootstrap, or init just init one when needed?

    2014-08-14 15:24 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Thu, Aug 14, 2014 at 7:49 AM, Bin Wang wrote:
    The `schedinit` calls `procresize` to init many Ps. The only place I
    found to init threads to run these Ps is in `starttheworld`. But I
    cannot understand how `starttheworld` is called. Could you help?
    Thanks.

    Function that starts threads is startm. It's called from several places.
    What do you mean by *how* it is called? Most of these function are
    just normally called.
    If you mean where they are called, you can use search to find all call sites:
    http://golang.org/search?q=starttheworld


    2014-08-13 19:36 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 2:26 PM, Bin Wang wrote:
    Hi, Dmitry

    I read the _rt0_go and is a little confused.

    The first M's g0 is the Go programs main goroutine, is that true?
    g0 is scheduler/system goroutine/stack associated with the thread.
    THe main goroutine is created during initialization by calling
    runtime.newproc. It initially calls runtime.main which in turn calls
    main.main.


    2014-08-13 17:21 GMT+08:00 Bin Wang <wbin00@gmail.com>:
    After read mcall it seems to be clear. Thank you.

    2014-08-13 17:00 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 11:30 AM, Bin Wang wrote:
    Hi,

    I'm reading the source code of the runtime. I find `mstart` starts an M and
    call `schedule` to run the G. But the `schedule` will never return. So how M
    schedule the other goroutines? How it knows if there some system calls that
    block the thread?
    Each thread (M) has a scheduler stack (g0) on which the thread starts.
    Goroutines run on their own stacks. When a goroutine needs to interact
    with the scheduler it switches the thread back to g0 stack (using
    runtime.mcall) and executes a scheduler function. See e.g.
    runtime.gosched for a goroutine yield functionality.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dmitry Vyukov at Aug 14, 2014 at 7:46 am

    On Thu, Aug 14, 2014 at 11:35 AM, Bin Wang wrote:
    I mean `schedinit` start many Ps, each P needs an M to run. Will all
    the Ms init at the bootstrap, or init just init one when needed?

    They are created when needed.
    Note that there can be significantly more M's than P's. M blocked in
    syscall does not need P.

    2014-08-14 15:24 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Thu, Aug 14, 2014 at 7:49 AM, Bin Wang wrote:
    The `schedinit` calls `procresize` to init many Ps. The only place I
    found to init threads to run these Ps is in `starttheworld`. But I
    cannot understand how `starttheworld` is called. Could you help?
    Thanks.

    Function that starts threads is startm. It's called from several places.
    What do you mean by *how* it is called? Most of these function are
    just normally called.
    If you mean where they are called, you can use search to find all call sites:
    http://golang.org/search?q=starttheworld


    2014-08-13 19:36 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 2:26 PM, Bin Wang wrote:
    Hi, Dmitry

    I read the _rt0_go and is a little confused.

    The first M's g0 is the Go programs main goroutine, is that true?
    g0 is scheduler/system goroutine/stack associated with the thread.
    THe main goroutine is created during initialization by calling
    runtime.newproc. It initially calls runtime.main which in turn calls
    main.main.


    2014-08-13 17:21 GMT+08:00 Bin Wang <wbin00@gmail.com>:
    After read mcall it seems to be clear. Thank you.

    2014-08-13 17:00 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
    On Wed, Aug 13, 2014 at 11:30 AM, Bin Wang wrote:
    Hi,

    I'm reading the source code of the runtime. I find `mstart` starts an M and
    call `schedule` to run the G. But the `schedule` will never return. So how M
    schedule the other goroutines? How it knows if there some system calls that
    block the thread?
    Each thread (M) has a scheduler stack (g0) on which the thread starts.
    Goroutines run on their own stacks. When a goroutine needs to interact
    with the scheduler it switches the thread back to g0 stack (using
    runtime.mcall) and executes a scheduler function. See e.g.
    runtime.gosched for a goroutine yield functionality.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 13, '14 at 7:30a
activeAug 14, '14 at 7:46a
posts9
users2
websitegolang.org

2 users in discussion

Bin Wang: 5 posts Dmitry Vyukov: 4 posts

People

Translate

site design / logo © 2021 Grokbase