How exactly is pause/resume implemented for goroutines in Go?
-
Actually, a few questions: How call stack is designed in Go so that goroutines are pausable? What exactly happens when goroutine enters wait state? When exactly goroutine enters wait state? Am I correct it happens only when it dequeues a message from some channel, or there are some other cases for this? What happens if goroutine calls another one without "go" syntax - i.e. calls it synchronously? Is this possible at all? If it's possible, how pause works in this case? Will the caller be paused as well, so its thread will be able to execute other goroutines while the callee waits? Are there some low-level abstractions / types exposing actual asynchronous APIs? If so, where can I look how these APIs look like? How scheduling in Go works? E.g. can I write my own scheduler and make it run a particular goroutine? So I'd like to know all the details. The mystery of how it actually works is a bit frightening for me :)
-
Answer:
I would suggest you would first get a solid understanding of OS threads. The simplest implementation of goroutines, could be with goroutine per thread. Then, you should understand and implement a basic epoll/poll loop, see for example a toy implementation here: https://github.com/elazarl/clogstash/blob/master/poller.c Finally you'll need to understand how the OS thread can change its stack, and thus, a single OS thread can cooperatively run two sub-threads. It's called n:m threading. It's a long journey, but at the end I believe you could have a solid understanding of Go's current implementation.
Elazar Leibovich at Quora Visit the source
Other answers
How call stack is designed in Go so that goroutines are pausable? Each goroutine has its own stack. The stack as such as no special design. When a thread changes goroutines, the stack pointer changes. What exactly happens when goroutine enters wait state? There are two different kinds of wait states. In one, a goroutine is waiting for some event to happen, such as communication on a channel or a read from the network. It enters wait state by being placed on some queue. Then the thread picks a new goroutine. The other kind of wait state is when a goroutine executes a blocking system call, or a call to a C function. In this case the thread blocks waiting for the call to complete. The scheduler's monitor thread notices this, and, if there are goroutines waiting to run and no thread to run them, it starts up a new thread that picks up a goroutine and starts to run it. When exactly goroutine enters wait state? Am I correct it happens only when it dequeues a message from some channel, or there are some other cases for this? As per the previous answer, there are cases other than waiting for a message on a channel. Besides channels and blocking calls, a goroutine can also be preempted. For example, the garbage collector does this when it temporarily needs to suspend all threads. What happens if goroutine calls another one without "go" syntax - i.e. calls it synchronously? Is this possible at all? If it's possible, how pause works in this case? Will the caller be paused as well, so its thread will be able to execute other goroutines while the callee waits? There is no such operation in the language. It doesn't even make sense. Goroutines communicate by sending messages on channels, or by using mutexes or other locking operations. There is no way for one goroutine to call another. Are there some low-level abstractions / types exposing actual asynchronous APIs? If so, where can I look how these APIs look like? You can implement asychronous APIs in Go, of course, but there are no low-level asynchronous APIs. There is no need for them. How scheduling in Go works? E.g. can I write my own scheduler and make it run a particular goroutine? Go does not support writing your own goroutine scheduler. Of course it's a free software project so you could change the runtime yourself.
Ian Lance Taylor
Related Q & A:
- How To Write A Resume?Best solution by Yahoo! Answers
- How To Prepare A Resume?Best solution by Yahoo! Answers
- How do you pause a program until a button is pressed in JAVA?Best solution by Stack Overflow
- How can I pause and restart animations in Android?Best solution by Stack Overflow
- If you want to go about moving to another country, how exactly do you go about it as far as?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.