From 51f9bd2b99084d2c6f7c43f3b36314577f478436 Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Sat, 2 Jun 2018 22:04:09 -0700 Subject: [PATCH] runtime: handle windows callback on non-go thread - add comments as per reviewer --- src/runtime/proc.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/runtime/proc.go b/src/runtime/proc.go index faebd239f7cae..e6a416bdb8643 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -1293,6 +1293,8 @@ func mstart1() { //go:yeswritebarrierrec func mstartm0() { // Create an extra M for callbacks on threads not created by Go. + // An extra M is also needed on Windows for callbacks created by + // syscall.NewCallback. if (iscgo || GOOS == "windows") && !cgoHasExtraM { cgoHasExtraM = true newextram() @@ -1620,6 +1622,9 @@ func allocm(_p_ *p, fn func()) *m { func needm(x byte) { if (iscgo || GOOS == "windows") && !cgoHasExtraM { // Can happen if C/C++ code calls Go from a global ctor. + // Can also happen on Windows if a global ctor uses a + // callback created using syscall.NewCallback. + // // Can not throw, because scheduler is not initialized yet. write(2, unsafe.Pointer(&earlycgocallback[0]), int32(len(earlycgocallback))) exit(1) @@ -4215,6 +4220,9 @@ func checkdead() { return } + // If we are not running under cgo, but we have an extra M then account + // for it. (It is possible to have an extra M on Windows without cgo to + // accommodate callbacks created by syscall.NewCallback.) var run0 int32 if !iscgo && cgoHasExtraM { run0 = 1