mirror of
https://github.com/golang/go.git
synced 2025-05-25 17:31:22 +00:00
In linker, refuse to write conservative (array of pointers) as the garbage collection type for any variable in the data/bss GC program. In the linker, attach the Go type to an already-read C declaration during dedup. This gives us Go types for C globals for free as long as the cmd/dist-generated Go code contains the declaration. (Most runtime C declarations have a corresponding Go declaration. Both are bss declarations and so the linker dedups them.) In cmd/dist, add a few more C files to the auto-Go-declaration list in order to get Go type information for the C declarations into the linker. In C compiler, mark all non-pointer-containing global declarations and all string data as NOPTR. This allows them to exist in C files without any corresponding Go declaration. Count C function pointers as "non-pointer-containing", since we have no heap-allocated C functions. In runtime, add NOPTR to the remaining pointer-containing declarations, none of which refer to Go heap objects. In runtime, also move os.Args and syscall.envs data into runtime-owned variables. Otherwise, in programs that do not import os or syscall, the runtime variables named os.Args and syscall.envs will be missing type information. I believe that this CL eliminates the final source of conservative GC scanning in non-SWIG Go programs, and therefore... Fixes #909. LGTM=iant R=iant CC=golang-codereviews https://golang.org/cl/149770043
224 lines
4.9 KiB
Go
224 lines
4.9 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package runtime
|
|
|
|
import "unsafe"
|
|
|
|
func newsysmon()
|
|
|
|
func runtime_init()
|
|
func main_init()
|
|
func main_main()
|
|
|
|
// The main goroutine.
|
|
func main() {
|
|
g := getg()
|
|
|
|
// Racectx of m0->g0 is used only as the parent of the main goroutine.
|
|
// It must not be used for anything else.
|
|
g.m.g0.racectx = 0
|
|
|
|
// Max stack size is 1 GB on 64-bit, 250 MB on 32-bit.
|
|
// Using decimal instead of binary GB and MB because
|
|
// they look nicer in the stack overflow failure message.
|
|
if ptrSize == 8 {
|
|
maxstacksize = 1000000000
|
|
} else {
|
|
maxstacksize = 250000000
|
|
}
|
|
|
|
onM(newsysmon)
|
|
|
|
// Lock the main goroutine onto this, the main OS thread,
|
|
// during initialization. Most programs won't care, but a few
|
|
// do require certain calls to be made by the main thread.
|
|
// Those can arrange for main.main to run in the main thread
|
|
// by calling runtime.LockOSThread during initialization
|
|
// to preserve the lock.
|
|
lockOSThread()
|
|
|
|
if g.m != &m0 {
|
|
gothrow("runtime.main not on m0")
|
|
}
|
|
|
|
runtime_init() // must be before defer
|
|
|
|
// Defer unlock so that runtime.Goexit during init does the unlock too.
|
|
needUnlock := true
|
|
defer func() {
|
|
if needUnlock {
|
|
unlockOSThread()
|
|
}
|
|
}()
|
|
|
|
memstats.enablegc = true // now that runtime is initialized, GC is okay
|
|
|
|
main_init()
|
|
|
|
needUnlock = false
|
|
unlockOSThread()
|
|
|
|
main_main()
|
|
if raceenabled {
|
|
racefini()
|
|
}
|
|
|
|
// Make racy client program work: if panicking on
|
|
// another goroutine at the same time as main returns,
|
|
// let the other goroutine finish printing the panic trace.
|
|
// Once it does, it will exit. See issue 3934.
|
|
if panicking != 0 {
|
|
gopark(nil, nil, "panicwait")
|
|
}
|
|
|
|
exit(0)
|
|
for {
|
|
var x *int32
|
|
*x = 0
|
|
}
|
|
}
|
|
|
|
var parkunlock_c byte
|
|
|
|
// start forcegc helper goroutine
|
|
func init() {
|
|
go forcegchelper()
|
|
}
|
|
|
|
func forcegchelper() {
|
|
forcegc.g = getg()
|
|
forcegc.g.issystem = true
|
|
for {
|
|
lock(&forcegc.lock)
|
|
if forcegc.idle != 0 {
|
|
gothrow("forcegc: phase error")
|
|
}
|
|
atomicstore(&forcegc.idle, 1)
|
|
goparkunlock(&forcegc.lock, "force gc (idle)")
|
|
// this goroutine is explicitly resumed by sysmon
|
|
if debug.gctrace > 0 {
|
|
println("GC forced")
|
|
}
|
|
gogc(1)
|
|
}
|
|
}
|
|
|
|
//go:nosplit
|
|
|
|
// Gosched yields the processor, allowing other goroutines to run. It does not
|
|
// suspend the current goroutine, so execution resumes automatically.
|
|
func Gosched() {
|
|
mcall(gosched_m)
|
|
}
|
|
|
|
// Puts the current goroutine into a waiting state and calls unlockf.
|
|
// If unlockf returns false, the goroutine is resumed.
|
|
func gopark(unlockf unsafe.Pointer, lock unsafe.Pointer, reason string) {
|
|
mp := acquirem()
|
|
gp := mp.curg
|
|
status := readgstatus(gp)
|
|
if status != _Grunning && status != _Gscanrunning {
|
|
gothrow("gopark: bad g status")
|
|
}
|
|
mp.waitlock = lock
|
|
mp.waitunlockf = unlockf
|
|
gp.waitreason = reason
|
|
releasem(mp)
|
|
// can't do anything that might move the G between Ms here.
|
|
mcall(park_m)
|
|
}
|
|
|
|
// Puts the current goroutine into a waiting state and unlocks the lock.
|
|
// The goroutine can be made runnable again by calling goready(gp).
|
|
func goparkunlock(lock *mutex, reason string) {
|
|
gopark(unsafe.Pointer(&parkunlock_c), unsafe.Pointer(lock), reason)
|
|
}
|
|
|
|
func goready(gp *g) {
|
|
mp := acquirem()
|
|
mp.ptrarg[0] = unsafe.Pointer(gp)
|
|
onM(ready_m)
|
|
releasem(mp)
|
|
}
|
|
|
|
//go:nosplit
|
|
func acquireSudog() *sudog {
|
|
c := gomcache()
|
|
s := c.sudogcache
|
|
if s != nil {
|
|
c.sudogcache = s.next
|
|
return s
|
|
}
|
|
|
|
// Delicate dance: the semaphore implementation calls
|
|
// acquireSudog, acquireSudog calls new(sudog),
|
|
// new calls malloc, malloc can call the garbage collector,
|
|
// and the garbage collector calls the semaphore implementation
|
|
// in stoptheworld.
|
|
// Break the cycle by doing acquirem/releasem around new(sudog).
|
|
// The acquirem/releasem increments m.locks during new(sudog),
|
|
// which keeps the garbage collector from being invoked.
|
|
mp := acquirem()
|
|
p := new(sudog)
|
|
releasem(mp)
|
|
return p
|
|
}
|
|
|
|
//go:nosplit
|
|
func releaseSudog(s *sudog) {
|
|
c := gomcache()
|
|
s.next = c.sudogcache
|
|
c.sudogcache = s
|
|
}
|
|
|
|
// funcPC returns the entry PC of the function f.
|
|
// It assumes that f is a func value. Otherwise the behavior is undefined.
|
|
//go:nosplit
|
|
func funcPC(f interface{}) uintptr {
|
|
return **(**uintptr)(add(unsafe.Pointer(&f), ptrSize))
|
|
}
|
|
|
|
// called from assembly
|
|
func badmcall(fn func(*g)) {
|
|
gothrow("runtime: mcall called on m->g0 stack")
|
|
}
|
|
|
|
func badmcall2(fn func(*g)) {
|
|
gothrow("runtime: mcall function returned")
|
|
}
|
|
|
|
func badreflectcall() {
|
|
panic("runtime: arg size to reflect.call more than 1GB")
|
|
}
|
|
|
|
func lockedOSThread() bool {
|
|
gp := getg()
|
|
return gp.lockedm != nil && gp.m.lockedg != nil
|
|
}
|
|
|
|
func newP() *p {
|
|
return new(p)
|
|
}
|
|
|
|
func newM() *m {
|
|
return new(m)
|
|
}
|
|
|
|
func newG() *g {
|
|
return new(g)
|
|
}
|
|
|
|
func allgadd(gp *g) {
|
|
if readgstatus(gp) == _Gidle {
|
|
gothrow("allgadd: bad status Gidle")
|
|
}
|
|
|
|
lock(&allglock)
|
|
allgs = append(allgs, gp)
|
|
allg = &allgs[0]
|
|
allglen = uintptr(len(allgs))
|
|
unlock(&allglock)
|
|
}
|