Reputation: 205
I read that goroutines are now preemptible. The preemption is done via a sysmon
goroutine that sends stop signals to goroutines which have used up their time slice. On posix systems, I believe this is done through pthread_kill
. My question is: how does this work in windows since windows doesn't support thread signaling? I had assumed that the go runtime may have been using a posix thread library like pthreads4w
, however I just saw that even in pthreads4w
, pthread_kill
doesn't support sending signals.
Upvotes: -1
Views: 357
Reputation: 8395
The comments in runtime/preempt.go
give an overview of how preemption works in the runtime. Specifically to do with asynchronous preemption:
Preemption at asynchronous safe-points is implemented by suspending the thread using an OS mechanism (e.g., signals) and inspecting its state to determine if the goroutine was at an asynchronous safe-point.
So how does async preemption work on windows? As mentioned in the original proposal for non-cooperative preemption of goroutines:
Other considerations
... signaled preemption is quite easy to support in Windows because it provides
SuspendThread
andGetThreadContext
...
The Windows SuspendThread
function can be used to suspend a thread by its handle, and GetThreadContext
can be used to get the processor state of the thread. The specific usages of these functions are implemented in runtime/os_windows.go
Upvotes: 1