From e9d9d0befc634f6e9f906b5ef7476fbd7ebd25e3 Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Wed, 25 Mar 2015 07:10:45 -0400 Subject: [PATCH] runtime, runtime/cgo: make needextram a bool Also invert it, which means it no longer needs to cross the cgo package boundary. Change-Id: I393cd073bda02b591a55d6bc6b8bb94970ea71cd Reviewed-on: https://go-review.googlesource.com/8082 Reviewed-by: Ian Lance Taylor Run-TryBot: David Crawshaw TryBot-Result: Gobot Gobot --- src/runtime/cgo.go | 7 +++++++ src/runtime/cgo/iscgo.go | 3 --- src/runtime/proc1.go | 6 +++--- src/runtime/runtime2.go | 2 -- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/runtime/cgo.go b/src/runtime/cgo.go index 7e6b253af92..5dc83c0be1b 100644 --- a/src/runtime/cgo.go +++ b/src/runtime/cgo.go @@ -21,3 +21,10 @@ var ( _cgo_free unsafe.Pointer _cgo_thread_start unsafe.Pointer ) + +// iscgo is set to true by the runtime/cgo package +var iscgo bool + +// cgoHasExtraM is set on startup when an extra M is created for cgo. +// The extra M must be created before any C/C++ code calls cgocallback. +var cgoHasExtraM bool diff --git a/src/runtime/cgo/iscgo.go b/src/runtime/cgo/iscgo.go index 61cba73d222..54f0a1360ee 100644 --- a/src/runtime/cgo/iscgo.go +++ b/src/runtime/cgo/iscgo.go @@ -15,6 +15,3 @@ import _ "unsafe" // for go:linkname //go:linkname _iscgo runtime.iscgo var _iscgo bool = true - -//go:linkname _needextram runtime.needextram -var _needextram uint32 = 1 // create an extra M on first cgo call diff --git a/src/runtime/proc1.go b/src/runtime/proc1.go index 325955665b2..4459802b158 100644 --- a/src/runtime/proc1.go +++ b/src/runtime/proc1.go @@ -718,8 +718,8 @@ func mstart1() { // prepare the thread to be able to handle the signals. if _g_.m == &m0 { // Create an extra M for callbacks on threads not created by Go. - if needextram == 1 { - needextram = 0 + if iscgo && !cgoHasExtraM { + cgoHasExtraM = true newextram() } initsig() @@ -817,7 +817,7 @@ func allocm(_p_ *p) *m { // put the m back on the list. //go:nosplit func needm(x byte) { - if needextram != 0 { + if iscgo && !cgoHasExtraM { // Can happen if C/C++ code calls Go from a global ctor. // Can not throw, because scheduler is not initialized yet. write(2, unsafe.Pointer(&earlycgocallback[0]), int32(len(earlycgocallback))) diff --git a/src/runtime/runtime2.go b/src/runtime/runtime2.go index 9ea3fd197fa..0d3e542882c 100644 --- a/src/runtime/runtime2.go +++ b/src/runtime/runtime2.go @@ -605,11 +605,9 @@ var ( allm *m allp [_MaxGomaxprocs + 1]*p gomaxprocs int32 - needextram uint32 panicking uint32 goos *int8 ncpu int32 - iscgo bool signote note forcegc forcegcstate sched schedt