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 <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/src/runtime/cgo.go b/src/runtime/cgo.go
index 7e6b253..5dc83c0 100644
--- a/src/runtime/cgo.go
+++ b/src/runtime/cgo.go
@@ -21,3 +21,10 @@
 	_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 61cba73..54f0a13 100644
--- a/src/runtime/cgo/iscgo.go
+++ b/src/runtime/cgo/iscgo.go
@@ -15,6 +15,3 @@
 
 //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 3259556..4459802 100644
--- a/src/runtime/proc1.go
+++ b/src/runtime/proc1.go
@@ -718,8 +718,8 @@
 	// 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 @@
 // 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 9ea3fd1..0d3e542 100644
--- a/src/runtime/runtime2.go
+++ b/src/runtime/runtime2.go
@@ -605,11 +605,9 @@
 	allm        *m
 	allp        [_MaxGomaxprocs + 1]*p
 	gomaxprocs  int32
-	needextram  uint32
 	panicking   uint32
 	goos        *int8
 	ncpu        int32
-	iscgo       bool
 	signote     note
 	forcegc     forcegcstate
 	sched       schedt