Goroutine to thread mapping

276 views
Skip to first unread message

Piotr Wyderski

unread,
Nov 7, 2022, 12:28:09 PM11/7/22
to golang-nuts
Hello,

A goroutine needs ultimately to be assigned to an OS thread. If a goroutine calls an assembly function F, can the thread assignment change during the execution of F?
In other words, is F guaranteed to return on the same thread it was called?

   Best regards, Piotr
 

Robert Engels

unread,
Nov 7, 2022, 12:46:42 PM11/7/22
to Piotr Wyderski, golang-nuts
Do you mean Go assembly or an assembly function called via CGo?

On Nov 7, 2022, at 11:28 AM, Piotr Wyderski <piotr.w...@gmail.com> wrote:


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/03f2cf25-7e52-41ca-ac20-a04877d5df9dn%40googlegroups.com.

Amnon

unread,
Nov 7, 2022, 1:01:22 PM11/7/22
to golang-nuts
Go makes no guarantees about the affinity between goroutines and threads, 
and the mapping does  typically jump around a lot during the execution of a program.

Robert Engels

unread,
Nov 7, 2022, 1:15:19 PM11/7/22
to Amnon, golang-nuts
You can use runtime.LockOSThread() to control this. 

On Nov 7, 2022, at 12:01 PM, Amnon <amn...@gmail.com> wrote:

Go makes no guarantees about the affinity between goroutines and threads, 

Piotr Wyderski

unread,
Nov 8, 2022, 3:31:25 AM11/8/22
to golang-nuts
I mean the Plan 9 assembler. Also assume the function is a leaf function, just a long one. Can such a function be preempted by Go runtime and re-assigned to another thread or can the reassignment happen only cooperatively, in a number of roughly predictable selected places?

Piotr Wyderski

unread,
Nov 8, 2022, 3:36:59 AM11/8/22
to golang-nuts
I am fine with affinity changes during, say, a mutex or channel operation. But does (or merely can) Go preempt the execution of leaf assembly functions, basically re-creating threads in user space? Or is the currently assigned thread mapping locked until the function decides to return, as cooperative multitasking would suggest?

peterGo

unread,
Nov 8, 2022, 6:17:19 AM11/8/22
to golang-nuts
piotr,

Goroutines are now asynchronously preemptible. As a result, loops without function calls no longer potentially deadlock the scheduler or significantly delay garbage collection. February 2020,  https://go.dev/doc/go1.14#runtime

peter

Robert Engels

unread,
Nov 8, 2022, 7:38:33 AM11/8/22
to peterGo, golang-nuts
To answer the rest of the question, since they are premptable they can be resumed on any thread. Go tries to use the same thread for performance but will issue memory barriers when it cannot. 

On Nov 8, 2022, at 5:17 AM, peterGo <go.pe...@gmail.com> wrote:



Piotr Wyderski

unread,
Nov 10, 2022, 4:01:04 AM11/10/22
to golang-nuts
Thank you for your answer, it is clear now.
Reply all
Reply to author
Forward
0 new messages