美文网首页
Go runtime包源码

Go runtime包源码

作者: cuishuang | 来源:发表于2020-03-22 22:05 被阅读0次

runtime.go

// Copyright 2009 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 (
    "runtime/internal/atomic"
    _ "unsafe" // for go:linkname
)

//go:generate go run wincallback.go
//go:generate go run mkduff.go
//go:generate go run mkfastlog2table.go

var ticks struct {
    lock mutex
    pad  uint32 // ensure 8-byte alignment of val on 386
    val  uint64
}

// Note: Called by runtime/pprof in addition to runtime code.
func tickspersecond() int64 {
    r := int64(atomic.Load64(&ticks.val))
    if r != 0 {
        return r
    }
    lock(&ticks.lock)
    r = int64(ticks.val)
    if r == 0 {
        t0 := nanotime()
        c0 := cputicks()
        usleep(100 * 1000)
        t1 := nanotime()
        c1 := cputicks()
        if t1 == t0 {
            t1++
        }
        r = (c1 - c0) * 1000 * 1000 * 1000 / (t1 - t0)
        if r == 0 {
            r++
        }
        atomic.Store64(&ticks.val, uint64(r))
    }
    unlock(&ticks.lock)
    return r
}

var envs []string
var argslice []string

//go:linkname syscall_runtime_envs syscall.runtime_envs
func syscall_runtime_envs() []string { return append([]string{}, envs...) }

//go:linkname syscall_Getpagesize syscall.Getpagesize
func syscall_Getpagesize() int { return int(physPageSize) }

//go:linkname os_runtime_args os.runtime_args
func os_runtime_args() []string { return append([]string{}, argslice...) }

//go:linkname syscall_Exit syscall.Exit
//go:nosplit
func syscall_Exit(code int) {
    exit(int32(code))
}

runtime1.go

// Copyright 2009 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 (
    "runtime/internal/atomic"
    "runtime/internal/sys"
    "unsafe"
)

// Keep a cached value to make gotraceback fast,
// since we call it on every call to gentraceback.
// The cached value is a uint32 in which the low bits
// are the "crash" and "all" settings and the remaining
// bits are the traceback value (0 off, 1 on, 2 include system).
const (
    tracebackCrash = 1 << iota
    tracebackAll
    tracebackShift = iota
)

var traceback_cache uint32 = 2 << tracebackShift
var traceback_env uint32

// gotraceback returns the current traceback settings.
//
// If level is 0, suppress all tracebacks.
// If level is 1, show tracebacks, but exclude runtime frames.
// If level is 2, show tracebacks including runtime frames.
// If all is set, print all goroutine stacks. Otherwise, print just the current goroutine.
// If crash is set, crash (core dump, etc) after tracebacking.
//
//go:nosplit
func gotraceback() (level int32, all, crash bool) {
    _g_ := getg()
    t := atomic.Load(&traceback_cache)
    crash = t&tracebackCrash != 0
    all = _g_.m.throwing > 0 || t&tracebackAll != 0
    if _g_.m.traceback != 0 {
        level = int32(_g_.m.traceback)
    } else {
        level = int32(t >> tracebackShift)
    }
    return
}

var (
    argc int32
    argv **byte
)

// nosplit for use in linux startup sysargs
//go:nosplit
func argv_index(argv **byte, i int32) *byte {
    return *(**byte)(add(unsafe.Pointer(argv), uintptr(i)*sys.PtrSize))
}

func args(c int32, v **byte) {
    argc = c
    argv = v
    sysargs(c, v)
}

func goargs() {
    if GOOS == "windows" {
        return
    }
    argslice = make([]string, argc)
    for i := int32(0); i < argc; i++ {
        argslice[i] = gostringnocopy(argv_index(argv, i))
    }
}

func goenvs_unix() {
    // TODO(austin): ppc64 in dynamic linking mode doesn't
    // guarantee env[] will immediately follow argv. Might cause
    // problems.
    n := int32(0)
    for argv_index(argv, argc+1+n) != nil {
        n++
    }

    envs = make([]string, n)
    for i := int32(0); i < n; i++ {
        envs[i] = gostring(argv_index(argv, argc+1+i))
    }
}

func environ() []string {
    return envs
}

// TODO: These should be locals in testAtomic64, but we don't 8-byte
// align stack variables on 386.
var test_z64, test_x64 uint64

func testAtomic64() {
    test_z64 = 42
    test_x64 = 0
    if atomic.Cas64(&test_z64, test_x64, 1) {
        throw("cas64 failed")
    }
    if test_x64 != 0 {
        throw("cas64 failed")
    }
    test_x64 = 42
    if !atomic.Cas64(&test_z64, test_x64, 1) {
        throw("cas64 failed")
    }
    if test_x64 != 42 || test_z64 != 1 {
        throw("cas64 failed")
    }
    if atomic.Load64(&test_z64) != 1 {
        throw("load64 failed")
    }
    atomic.Store64(&test_z64, (1<<40)+1)
    if atomic.Load64(&test_z64) != (1<<40)+1 {
        throw("store64 failed")
    }
    if atomic.Xadd64(&test_z64, (1<<40)+1) != (2<<40)+2 {
        throw("xadd64 failed")
    }
    if atomic.Load64(&test_z64) != (2<<40)+2 {
        throw("xadd64 failed")
    }
    if atomic.Xchg64(&test_z64, (3<<40)+3) != (2<<40)+2 {
        throw("xchg64 failed")
    }
    if atomic.Load64(&test_z64) != (3<<40)+3 {
        throw("xchg64 failed")
    }
}

func check() {
    var (
        a     int8
        b     uint8
        c     int16
        d     uint16
        e     int32
        f     uint32
        g     int64
        h     uint64
        i, i1 float32
        j, j1 float64
        k     unsafe.Pointer
        l     *uint16
        m     [4]byte
    )
    type x1t struct {
        x uint8
    }
    type y1t struct {
        x1 x1t
        y  uint8
    }
    var x1 x1t
    var y1 y1t

    if unsafe.Sizeof(a) != 1 {
        throw("bad a")
    }
    if unsafe.Sizeof(b) != 1 {
        throw("bad b")
    }
    if unsafe.Sizeof(c) != 2 {
        throw("bad c")
    }
    if unsafe.Sizeof(d) != 2 {
        throw("bad d")
    }
    if unsafe.Sizeof(e) != 4 {
        throw("bad e")
    }
    if unsafe.Sizeof(f) != 4 {
        throw("bad f")
    }
    if unsafe.Sizeof(g) != 8 {
        throw("bad g")
    }
    if unsafe.Sizeof(h) != 8 {
        throw("bad h")
    }
    if unsafe.Sizeof(i) != 4 {
        throw("bad i")
    }
    if unsafe.Sizeof(j) != 8 {
        throw("bad j")
    }
    if unsafe.Sizeof(k) != sys.PtrSize {
        throw("bad k")
    }
    if unsafe.Sizeof(l) != sys.PtrSize {
        throw("bad l")
    }
    if unsafe.Sizeof(x1) != 1 {
        throw("bad unsafe.Sizeof x1")
    }
    if unsafe.Offsetof(y1.y) != 1 {
        throw("bad offsetof y1.y")
    }
    if unsafe.Sizeof(y1) != 2 {
        throw("bad unsafe.Sizeof y1")
    }

    if timediv(12345*1000000000+54321, 1000000000, &e) != 12345 || e != 54321 {
        throw("bad timediv")
    }

    var z uint32
    z = 1
    if !atomic.Cas(&z, 1, 2) {
        throw("cas1")
    }
    if z != 2 {
        throw("cas2")
    }

    z = 4
    if atomic.Cas(&z, 5, 6) {
        throw("cas3")
    }
    if z != 4 {
        throw("cas4")
    }

    z = 0xffffffff
    if !atomic.Cas(&z, 0xffffffff, 0xfffffffe) {
        throw("cas5")
    }
    if z != 0xfffffffe {
        throw("cas6")
    }

    m = [4]byte{1, 1, 1, 1}
    atomic.Or8(&m[1], 0xf0)
    if m[0] != 1 || m[1] != 0xf1 || m[2] != 1 || m[3] != 1 {
        throw("atomicor8")
    }

    m = [4]byte{0xff, 0xff, 0xff, 0xff}
    atomic.And8(&m[1], 0x1)
    if m[0] != 0xff || m[1] != 0x1 || m[2] != 0xff || m[3] != 0xff {
        throw("atomicand8")
    }

    *(*uint64)(unsafe.Pointer(&j)) = ^uint64(0)
    if j == j {
        throw("float64nan")
    }
    if !(j != j) {
        throw("float64nan1")
    }

    *(*uint64)(unsafe.Pointer(&j1)) = ^uint64(1)
    if j == j1 {
        throw("float64nan2")
    }
    if !(j != j1) {
        throw("float64nan3")
    }

    *(*uint32)(unsafe.Pointer(&i)) = ^uint32(0)
    if i == i {
        throw("float32nan")
    }
    if i == i {
        throw("float32nan1")
    }

    *(*uint32)(unsafe.Pointer(&i1)) = ^uint32(1)
    if i == i1 {
        throw("float32nan2")
    }
    if i == i1 {
        throw("float32nan3")
    }

    testAtomic64()

    if _FixedStack != round2(_FixedStack) {
        throw("FixedStack is not power-of-2")
    }

    if !checkASM() {
        throw("assembly checks failed")
    }
}

type dbgVar struct {
    name  string
    value *int32
}

// Holds variables parsed from GODEBUG env var,
// except for "memprofilerate" since there is an
// existing int var for that value, which may
// already have an initial value.
var debug struct {
    allocfreetrace     int32
    cgocheck           int32
    clobberfree        int32
    efence             int32
    gccheckmark        int32
    gcpacertrace       int32
    gcshrinkstackoff   int32
    gcstoptheworld     int32
    gctrace            int32
    invalidptr         int32
    madvdontneed       int32 // for Linux; issue 28466
    sbrk               int32
    scavenge           int32
    scheddetail        int32
    schedtrace         int32
    tracebackancestors int32
}

var dbgvars = []dbgVar{
    {"allocfreetrace", &debug.allocfreetrace},
    {"clobberfree", &debug.clobberfree},
    {"cgocheck", &debug.cgocheck},
    {"efence", &debug.efence},
    {"gccheckmark", &debug.gccheckmark},
    {"gcpacertrace", &debug.gcpacertrace},
    {"gcshrinkstackoff", &debug.gcshrinkstackoff},
    {"gcstoptheworld", &debug.gcstoptheworld},
    {"gctrace", &debug.gctrace},
    {"invalidptr", &debug.invalidptr},
    {"madvdontneed", &debug.madvdontneed},
    {"sbrk", &debug.sbrk},
    {"scavenge", &debug.scavenge},
    {"scheddetail", &debug.scheddetail},
    {"schedtrace", &debug.schedtrace},
    {"tracebackancestors", &debug.tracebackancestors},
}

func parsedebugvars() {
    // defaults
    debug.cgocheck = 1
    debug.invalidptr = 1

    for p := gogetenv("GODEBUG"); p != ""; {
        field := ""
        i := index(p, ",")
        if i < 0 {
            field, p = p, ""
        } else {
            field, p = p[:i], p[i+1:]
        }
        i = index(field, "=")
        if i < 0 {
            continue
        }
        key, value := field[:i], field[i+1:]

        // Update MemProfileRate directly here since it
        // is int, not int32, and should only be updated
        // if specified in GODEBUG.
        if key == "memprofilerate" {
            if n, ok := atoi(value); ok {
                MemProfileRate = n
            }
        } else {
            for _, v := range dbgvars {
                if v.name == key {
                    if n, ok := atoi32(value); ok {
                        *v.value = n
                    }
                }
            }
        }
    }

    setTraceback(gogetenv("GOTRACEBACK"))
    traceback_env = traceback_cache
}

//go:linkname setTraceback runtime/debug.SetTraceback
func setTraceback(level string) {
    var t uint32
    switch level {
    case "none":
        t = 0
    case "single", "":
        t = 1 << tracebackShift
    case "all":
        t = 1<<tracebackShift | tracebackAll
    case "system":
        t = 2<<tracebackShift | tracebackAll
    case "crash":
        t = 2<<tracebackShift | tracebackAll | tracebackCrash
    default:
        t = tracebackAll
        if n, ok := atoi(level); ok && n == int(uint32(n)) {
            t |= uint32(n) << tracebackShift
        }
    }
    // when C owns the process, simply exit'ing the process on fatal errors
    // and panics is surprising. Be louder and abort instead.
    if islibrary || isarchive {
        t |= tracebackCrash
    }

    t |= traceback_env

    atomic.Store(&traceback_cache, t)
}

// Poor mans 64-bit division.
// This is a very special function, do not use it if you are not sure what you are doing.
// int64 division is lowered into _divv() call on 386, which does not fit into nosplit functions.
// Handles overflow in a time-specific manner.
// This keeps us within no-split stack limits on 32-bit processors.
//go:nosplit
func timediv(v int64, div int32, rem *int32) int32 {
    res := int32(0)
    for bit := 30; bit >= 0; bit-- {
        if v >= int64(div)<<uint(bit) {
            v = v - (int64(div) << uint(bit))
            // Before this for loop, res was 0, thus all these
            // power of 2 increments are now just bitsets.
            res |= 1 << uint(bit)
        }
    }
    if v >= int64(div) {
        if rem != nil {
            *rem = 0
        }
        return 0x7fffffff
    }
    if rem != nil {
        *rem = int32(v)
    }
    return res
}

// Helpers for Go. Must be NOSPLIT, must only call NOSPLIT functions, and must not block.

//go:nosplit
func acquirem() *m {
    _g_ := getg()
    _g_.m.locks++
    return _g_.m
}

//go:nosplit
func releasem(mp *m) {
    _g_ := getg()
    mp.locks--
    if mp.locks == 0 && _g_.preempt {
        // restore the preemption request in case we've cleared it in newstack
        _g_.stackguard0 = stackPreempt
    }
}

//go:nosplit
func gomcache() *mcache {
    return getg().m.mcache
}

//go:linkname reflect_typelinks reflect.typelinks
func reflect_typelinks() ([]unsafe.Pointer, [][]int32) {
    modules := activeModules()
    sections := []unsafe.Pointer{unsafe.Pointer(modules[0].types)}
    ret := [][]int32{modules[0].typelinks}
    for _, md := range modules[1:] {
        sections = append(sections, unsafe.Pointer(md.types))
        ret = append(ret, md.typelinks)
    }
    return sections, ret
}

// reflect_resolveNameOff resolves a name offset from a base pointer.
//go:linkname reflect_resolveNameOff reflect.resolveNameOff
func reflect_resolveNameOff(ptrInModule unsafe.Pointer, off int32) unsafe.Pointer {
    return unsafe.Pointer(resolveNameOff(ptrInModule, nameOff(off)).bytes)
}

// reflect_resolveTypeOff resolves an *rtype offset from a base type.
//go:linkname reflect_resolveTypeOff reflect.resolveTypeOff
func reflect_resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
    return unsafe.Pointer((*_type)(rtype).typeOff(typeOff(off)))
}

// reflect_resolveTextOff resolves an function pointer offset from a base type.
//go:linkname reflect_resolveTextOff reflect.resolveTextOff
func reflect_resolveTextOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
    return (*_type)(rtype).textOff(textOff(off))

}

// reflectlite_resolveNameOff resolves a name offset from a base pointer.
//go:linkname reflectlite_resolveNameOff internal/reflectlite.resolveNameOff
func reflectlite_resolveNameOff(ptrInModule unsafe.Pointer, off int32) unsafe.Pointer {
    return unsafe.Pointer(resolveNameOff(ptrInModule, nameOff(off)).bytes)
}

// reflectlite_resolveTypeOff resolves an *rtype offset from a base type.
//go:linkname reflectlite_resolveTypeOff internal/reflectlite.resolveTypeOff
func reflectlite_resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
    return unsafe.Pointer((*_type)(rtype).typeOff(typeOff(off)))
}

// reflect_addReflectOff adds a pointer to the reflection offset lookup map.
//go:linkname reflect_addReflectOff reflect.addReflectOff
func reflect_addReflectOff(ptr unsafe.Pointer) int32 {
    reflectOffsLock()
    if reflectOffs.m == nil {
        reflectOffs.m = make(map[int32]unsafe.Pointer)
        reflectOffs.minv = make(map[unsafe.Pointer]int32)
        reflectOffs.next = -1
    }
    id, found := reflectOffs.minv[ptr]
    if !found {
        id = reflectOffs.next
        reflectOffs.next-- // use negative offsets as IDs to aid debugging
        reflectOffs.m[id] = ptr
        reflectOffs.minv[ptr] = id
    }
    reflectOffsUnlock()
    return id
}

runtime2.go

// Copyright 2009 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 (
    "internal/cpu"
    "runtime/internal/atomic"
    "runtime/internal/sys"
    "unsafe"
)

// defined constants
const (
    // G status
    //
    // Beyond indicating the general state of a G, the G status
    // acts like a lock on the goroutine's stack (and hence its
    // ability to execute user code).
    //
    // If you add to this list, add to the list
    // of "okay during garbage collection" status
    // in mgcmark.go too.
    //
    // TODO(austin): The _Gscan bit could be much lighter-weight.
    // For example, we could choose not to run _Gscanrunnable
    // goroutines found in the run queue, rather than CAS-looping
    // until they become _Grunnable. And transitions like
    // _Gscanwaiting -> _Gscanrunnable are actually okay because
    // they don't affect stack ownership.

    // _Gidle means this goroutine was just allocated and has not
    // yet been initialized.
    _Gidle = iota // 0

    // _Grunnable means this goroutine is on a run queue. It is
    // not currently executing user code. The stack is not owned.
    _Grunnable // 1

    // _Grunning means this goroutine may execute user code. The
    // stack is owned by this goroutine. It is not on a run queue.
    // It is assigned an M and a P.
    _Grunning // 2

    // _Gsyscall means this goroutine is executing a system call.
    // It is not executing user code. The stack is owned by this
    // goroutine. It is not on a run queue. It is assigned an M.
    _Gsyscall // 3

    // _Gwaiting means this goroutine is blocked in the runtime.
    // It is not executing user code. It is not on a run queue,
    // but should be recorded somewhere (e.g., a channel wait
    // queue) so it can be ready()d when necessary. The stack is
    // not owned *except* that a channel operation may read or
    // write parts of the stack under the appropriate channel
    // lock. Otherwise, it is not safe to access the stack after a
    // goroutine enters _Gwaiting (e.g., it may get moved).
    _Gwaiting // 4

    // _Gmoribund_unused is currently unused, but hardcoded in gdb
    // scripts.
    _Gmoribund_unused // 5

    // _Gdead means this goroutine is currently unused. It may be
    // just exited, on a free list, or just being initialized. It
    // is not executing user code. It may or may not have a stack
    // allocated. The G and its stack (if any) are owned by the M
    // that is exiting the G or that obtained the G from the free
    // list.
    _Gdead // 6

    // _Genqueue_unused is currently unused.
    _Genqueue_unused // 7

    // _Gcopystack means this goroutine's stack is being moved. It
    // is not executing user code and is not on a run queue. The
    // stack is owned by the goroutine that put it in _Gcopystack.
    _Gcopystack // 8

    // _Gscan combined with one of the above states other than
    // _Grunning indicates that GC is scanning the stack. The
    // goroutine is not executing user code and the stack is owned
    // by the goroutine that set the _Gscan bit.
    //
    // _Gscanrunning is different: it is used to briefly block
    // state transitions while GC signals the G to scan its own
    // stack. This is otherwise like _Grunning.
    //
    // atomicstatus&~Gscan gives the state the goroutine will
    // return to when the scan completes.
    _Gscan         = 0x1000
    _Gscanrunnable = _Gscan + _Grunnable // 0x1001
    _Gscanrunning  = _Gscan + _Grunning  // 0x1002
    _Gscansyscall  = _Gscan + _Gsyscall  // 0x1003
    _Gscanwaiting  = _Gscan + _Gwaiting  // 0x1004
)

const (
    // P status

    // _Pidle means a P is not being used to run user code or the
    // scheduler. Typically, it's on the idle P list and available
    // to the scheduler, but it may just be transitioning between
    // other states.
    //
    // The P is owned by the idle list or by whatever is
    // transitioning its state. Its run queue is empty.
    _Pidle = iota

    // _Prunning means a P is owned by an M and is being used to
    // run user code or the scheduler. Only the M that owns this P
    // is allowed to change the P's status from _Prunning. The M
    // may transition the P to _Pidle (if it has no more work to
    // do), _Psyscall (when entering a syscall), or _Pgcstop (to
    // halt for the GC). The M may also hand ownership of the P
    // off directly to another M (e.g., to schedule a locked G).
    _Prunning

    // _Psyscall means a P is not running user code. It has
    // affinity to an M in a syscall but is not owned by it and
    // may be stolen by another M. This is similar to _Pidle but
    // uses lightweight transitions and maintains M affinity.
    //
    // Leaving _Psyscall must be done with a CAS, either to steal
    // or retake the P. Note that there's an ABA hazard: even if
    // an M successfully CASes its original P back to _Prunning
    // after a syscall, it must understand the P may have been
    // used by another M in the interim.
    _Psyscall

    // _Pgcstop means a P is halted for STW and owned by the M
    // that stopped the world. The M that stopped the world
    // continues to use its P, even in _Pgcstop. Transitioning
    // from _Prunning to _Pgcstop causes an M to release its P and
    // park.
    //
    // The P retains its run queue and startTheWorld will restart
    // the scheduler on Ps with non-empty run queues.
    _Pgcstop

    // _Pdead means a P is no longer used (GOMAXPROCS shrank). We
    // reuse Ps if GOMAXPROCS increases. A dead P is mostly
    // stripped of its resources, though a few things remain
    // (e.g., trace buffers).
    _Pdead
)

// Mutual exclusion locks.  In the uncontended case,
// as fast as spin locks (just a few user-level instructions),
// but on the contention path they sleep in the kernel.
// A zeroed Mutex is unlocked (no need to initialize each lock).
type mutex struct {
    // Futex-based impl treats it as uint32 key,
    // while sema-based impl as M* waitm.
    // Used to be a union, but unions break precise GC.
    key uintptr
}

// sleep and wakeup on one-time events.
// before any calls to notesleep or notewakeup,
// must call noteclear to initialize the Note.
// then, exactly one thread can call notesleep
// and exactly one thread can call notewakeup (once).
// once notewakeup has been called, the notesleep
// will return.  future notesleep will return immediately.
// subsequent noteclear must be called only after
// previous notesleep has returned, e.g. it's disallowed
// to call noteclear straight after notewakeup.
//
// notetsleep is like notesleep but wakes up after
// a given number of nanoseconds even if the event
// has not yet happened.  if a goroutine uses notetsleep to
// wake up early, it must wait to call noteclear until it
// can be sure that no other goroutine is calling
// notewakeup.
//
// notesleep/notetsleep are generally called on g0,
// notetsleepg is similar to notetsleep but is called on user g.
type note struct {
    // Futex-based impl treats it as uint32 key,
    // while sema-based impl as M* waitm.
    // Used to be a union, but unions break precise GC.
    key uintptr
}

type funcval struct {
    fn uintptr
    // variable-size, fn-specific data here
}

type iface struct {
    tab  *itab
    data unsafe.Pointer
}

type eface struct {
    _type *_type
    data  unsafe.Pointer
}

func efaceOf(ep *interface{}) *eface {
    return (*eface)(unsafe.Pointer(ep))
}

// The guintptr, muintptr, and puintptr are all used to bypass write barriers.
// It is particularly important to avoid write barriers when the current P has
// been released, because the GC thinks the world is stopped, and an
// unexpected write barrier would not be synchronized with the GC,
// which can lead to a half-executed write barrier that has marked the object
// but not queued it. If the GC skips the object and completes before the
// queuing can occur, it will incorrectly free the object.
//
// We tried using special assignment functions invoked only when not
// holding a running P, but then some updates to a particular memory
// word went through write barriers and some did not. This breaks the
// write barrier shadow checking mode, and it is also scary: better to have
// a word that is completely ignored by the GC than to have one for which
// only a few updates are ignored.
//
// Gs and Ps are always reachable via true pointers in the
// allgs and allp lists or (during allocation before they reach those lists)
// from stack variables.
//
// Ms are always reachable via true pointers either from allm or
// freem. Unlike Gs and Ps we do free Ms, so it's important that
// nothing ever hold an muintptr across a safe point.

// A guintptr holds a goroutine pointer, but typed as a uintptr
// to bypass write barriers. It is used in the Gobuf goroutine state
// and in scheduling lists that are manipulated without a P.
//
// The Gobuf.g goroutine pointer is almost always updated by assembly code.
// In one of the few places it is updated by Go code - func save - it must be
// treated as a uintptr to avoid a write barrier being emitted at a bad time.
// Instead of figuring out how to emit the write barriers missing in the
// assembly manipulation, we change the type of the field to uintptr,
// so that it does not require write barriers at all.
//
// Goroutine structs are published in the allg list and never freed.
// That will keep the goroutine structs from being collected.
// There is never a time that Gobuf.g's contain the only references
// to a goroutine: the publishing of the goroutine in allg comes first.
// Goroutine pointers are also kept in non-GC-visible places like TLS,
// so I can't see them ever moving. If we did want to start moving data
// in the GC, we'd need to allocate the goroutine structs from an
// alternate arena. Using guintptr doesn't make that problem any worse.
type guintptr uintptr

//go:nosplit
func (gp guintptr) ptr() *g { return (*g)(unsafe.Pointer(gp)) }

//go:nosplit
func (gp *guintptr) set(g *g) { *gp = guintptr(unsafe.Pointer(g)) }

//go:nosplit
func (gp *guintptr) cas(old, new guintptr) bool {
    return atomic.Casuintptr((*uintptr)(unsafe.Pointer(gp)), uintptr(old), uintptr(new))
}

// setGNoWB performs *gp = new without a write barrier.
// For times when it's impractical to use a guintptr.
//go:nosplit
//go:nowritebarrier
func setGNoWB(gp **g, new *g) {
    (*guintptr)(unsafe.Pointer(gp)).set(new)
}

type puintptr uintptr

//go:nosplit
func (pp puintptr) ptr() *p { return (*p)(unsafe.Pointer(pp)) }

//go:nosplit
func (pp *puintptr) set(p *p) { *pp = puintptr(unsafe.Pointer(p)) }

// muintptr is a *m that is not tracked by the garbage collector.
//
// Because we do free Ms, there are some additional constrains on
// muintptrs:
//
// 1. Never hold an muintptr locally across a safe point.
//
// 2. Any muintptr in the heap must be owned by the M itself so it can
//    ensure it is not in use when the last true *m is released.
type muintptr uintptr

//go:nosplit
func (mp muintptr) ptr() *m { return (*m)(unsafe.Pointer(mp)) }

//go:nosplit
func (mp *muintptr) set(m *m) { *mp = muintptr(unsafe.Pointer(m)) }

// setMNoWB performs *mp = new without a write barrier.
// For times when it's impractical to use an muintptr.
//go:nosplit
//go:nowritebarrier
func setMNoWB(mp **m, new *m) {
    (*muintptr)(unsafe.Pointer(mp)).set(new)
}

type gobuf struct {
    // The offsets of sp, pc, and g are known to (hard-coded in) libmach.
    //
    // ctxt is unusual with respect to GC: it may be a
    // heap-allocated funcval, so GC needs to track it, but it
    // needs to be set and cleared from assembly, where it's
    // difficult to have write barriers. However, ctxt is really a
    // saved, live register, and we only ever exchange it between
    // the real register and the gobuf. Hence, we treat it as a
    // root during stack scanning, which means assembly that saves
    // and restores it doesn't need write barriers. It's still
    // typed as a pointer so that any other writes from Go get
    // write barriers.
    sp   uintptr
    pc   uintptr
    g    guintptr
    ctxt unsafe.Pointer
    ret  sys.Uintreg
    lr   uintptr
    bp   uintptr // for GOEXPERIMENT=framepointer
}

// sudog represents a g in a wait list, such as for sending/receiving
// on a channel.
//
// sudog is necessary because the g ↔ synchronization object relation
// is many-to-many. A g can be on many wait lists, so there may be
// many sudogs for one g; and many gs may be waiting on the same
// synchronization object, so there may be many sudogs for one object.
//
// sudogs are allocated from a special pool. Use acquireSudog and
// releaseSudog to allocate and free them.
type sudog struct {
    // The following fields are protected by the hchan.lock of the
    // channel this sudog is blocking on. shrinkstack depends on
    // this for sudogs involved in channel ops.

    g *g

    // isSelect indicates g is participating in a select, so
    // g.selectDone must be CAS'd to win the wake-up race.
    isSelect bool
    next     *sudog
    prev     *sudog
    elem     unsafe.Pointer // data element (may point to stack)

    // The following fields are never accessed concurrently.
    // For channels, waitlink is only accessed by g.
    // For semaphores, all fields (including the ones above)
    // are only accessed when holding a semaRoot lock.

    acquiretime int64
    releasetime int64
    ticket      uint32
    parent      *sudog // semaRoot binary tree
    waitlink    *sudog // g.waiting list or semaRoot
    waittail    *sudog // semaRoot
    c           *hchan // channel
}

type libcall struct {
    fn   uintptr
    n    uintptr // number of parameters
    args uintptr // parameters
    r1   uintptr // return values
    r2   uintptr
    err  uintptr // error number
}

// describes how to handle callback
type wincallbackcontext struct {
    gobody       unsafe.Pointer // go function to call
    argsize      uintptr        // callback arguments size (in bytes)
    restorestack uintptr        // adjust stack on return by (in bytes) (386 only)
    cleanstack   bool
}

// Stack describes a Go execution stack.
// The bounds of the stack are exactly [lo, hi),
// with no implicit data structures on either side.
type stack struct {
    lo uintptr
    hi uintptr
}

type g struct {
    // Stack parameters.
    // stack describes the actual stack memory: [stack.lo, stack.hi).
    // stackguard0 is the stack pointer compared in the Go stack growth prologue.
    // It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption.
    // stackguard1 is the stack pointer compared in the C stack growth prologue.
    // It is stack.lo+StackGuard on g0 and gsignal stacks.
    // It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).
    stack       stack   // offset known to runtime/cgo
    stackguard0 uintptr // offset known to liblink
    stackguard1 uintptr // offset known to liblink

    _panic         *_panic // innermost panic - offset known to liblink
    _defer         *_defer // innermost defer
    m              *m      // current m; offset known to arm liblink
    sched          gobuf
    syscallsp      uintptr        // if status==Gsyscall, syscallsp = sched.sp to use during gc
    syscallpc      uintptr        // if status==Gsyscall, syscallpc = sched.pc to use during gc
    stktopsp       uintptr        // expected sp at top of stack, to check in traceback
    param          unsafe.Pointer // passed parameter on wakeup
    atomicstatus   uint32
    stackLock      uint32 // sigprof/scang lock; TODO: fold in to atomicstatus
    goid           int64
    schedlink      guintptr
    waitsince      int64      // approx time when the g become blocked
    waitreason     waitReason // if status==Gwaiting
    preempt        bool       // preemption signal, duplicates stackguard0 = stackpreempt
    paniconfault   bool       // panic (instead of crash) on unexpected fault address
    preemptscan    bool       // preempted g does scan for gc
    gcscandone     bool       // g has scanned stack; protected by _Gscan bit in status
    gcscanvalid    bool       // false at start of gc cycle, true if G has not run since last scan; TODO: remove?
    throwsplit     bool       // must not split stack
    raceignore     int8       // ignore race detection events
    sysblocktraced bool       // StartTrace has emitted EvGoInSyscall about this goroutine
    sysexitticks   int64      // cputicks when syscall has returned (for tracing)
    traceseq       uint64     // trace event sequencer
    tracelastp     puintptr   // last P emitted an event for this goroutine
    lockedm        muintptr
    sig            uint32
    writebuf       []byte
    sigcode0       uintptr
    sigcode1       uintptr
    sigpc          uintptr
    gopc           uintptr         // pc of go statement that created this goroutine
    ancestors      *[]ancestorInfo // ancestor information goroutine(s) that created this goroutine (only used if debug.tracebackancestors)
    startpc        uintptr         // pc of goroutine function
    racectx        uintptr
    waiting        *sudog         // sudog structures this g is waiting on (that have a valid elem ptr); in lock order
    cgoCtxt        []uintptr      // cgo traceback context
    labels         unsafe.Pointer // profiler labels
    timer          *timer         // cached timer for time.Sleep
    selectDone     uint32         // are we participating in a select and did someone win the race?

    // Per-G GC state

    // gcAssistBytes is this G's GC assist credit in terms of
    // bytes allocated. If this is positive, then the G has credit
    // to allocate gcAssistBytes bytes without assisting. If this
    // is negative, then the G must correct this by performing
    // scan work. We track this in bytes to make it fast to update
    // and check for debt in the malloc hot path. The assist ratio
    // determines how this corresponds to scan work debt.
    gcAssistBytes int64
}

type m struct {
    g0      *g     // goroutine with scheduling stack
    morebuf gobuf  // gobuf arg to morestack
    divmod  uint32 // div/mod denominator for arm - known to liblink

    // Fields not known to debuggers.
    procid        uint64       // for debuggers, but offset not hard-coded
    gsignal       *g           // signal-handling g
    goSigStack    gsignalStack // Go-allocated signal handling stack
    sigmask       sigset       // storage for saved signal mask
    tls           [6]uintptr   // thread-local storage (for x86 extern register)
    mstartfn      func()
    curg          *g       // current running goroutine
    caughtsig     guintptr // goroutine running during fatal signal
    p             puintptr // attached p for executing go code (nil if not executing go code)
    nextp         puintptr
    oldp          puintptr // the p that was attached before executing a syscall
    id            int64
    mallocing     int32
    throwing      int32
    preemptoff    string // if != "", keep curg running on this m
    locks         int32
    dying         int32
    profilehz     int32
    spinning      bool // m is out of work and is actively looking for work
    blocked       bool // m is blocked on a note
    newSigstack   bool // minit on C thread called sigaltstack
    printlock     int8
    incgo         bool   // m is executing a cgo call
    freeWait      uint32 // if == 0, safe to free g0 and delete m (atomic)
    fastrand      [2]uint32
    needextram    bool
    traceback     uint8
    ncgocall      uint64      // number of cgo calls in total
    ncgo          int32       // number of cgo calls currently in progress
    cgoCallersUse uint32      // if non-zero, cgoCallers in use temporarily
    cgoCallers    *cgoCallers // cgo traceback if crashing in cgo call
    park          note
    alllink       *m // on allm
    schedlink     muintptr
    mcache        *mcache
    lockedg       guintptr
    createstack   [32]uintptr // stack that created this thread.
    lockedExt     uint32      // tracking for external LockOSThread
    lockedInt     uint32      // tracking for internal lockOSThread
    nextwaitm     muintptr    // next m waiting for lock
    waitunlockf   func(*g, unsafe.Pointer) bool
    waitlock      unsafe.Pointer
    waittraceev   byte
    waittraceskip int
    startingtrace bool
    syscalltick   uint32
    thread        uintptr // thread handle
    freelink      *m      // on sched.freem

    // these are here because they are too large to be on the stack
    // of low-level NOSPLIT functions.
    libcall   libcall
    libcallpc uintptr // for cpu profiler
    libcallsp uintptr
    libcallg  guintptr
    syscall   libcall // stores syscall parameters on windows

    vdsoSP uintptr // SP for traceback while in VDSO call (0 if not in call)
    vdsoPC uintptr // PC for traceback while in VDSO call

    dlogPerM

    mOS
}

type p struct {
    id          int32
    status      uint32 // one of pidle/prunning/...
    link        puintptr
    schedtick   uint32     // incremented on every scheduler call
    syscalltick uint32     // incremented on every system call
    sysmontick  sysmontick // last tick observed by sysmon
    m           muintptr   // back-link to associated m (nil if idle)
    mcache      *mcache
    raceprocctx uintptr

    deferpool    [5][]*_defer // pool of available defer structs of different sizes (see panic.go)
    deferpoolbuf [5][32]*_defer

    // Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen.
    goidcache    uint64
    goidcacheend uint64

    // Queue of runnable goroutines. Accessed without lock.
    runqhead uint32
    runqtail uint32
    runq     [256]guintptr
    // runnext, if non-nil, is a runnable G that was ready'd by
    // the current G and should be run next instead of what's in
    // runq if there's time remaining in the running G's time
    // slice. It will inherit the time left in the current time
    // slice. If a set of goroutines is locked in a
    // communicate-and-wait pattern, this schedules that set as a
    // unit and eliminates the (potentially large) scheduling
    // latency that otherwise arises from adding the ready'd
    // goroutines to the end of the run queue.
    runnext guintptr

    // Available G's (status == Gdead)
    gFree struct {
        gList
        n int32
    }

    sudogcache []*sudog
    sudogbuf   [128]*sudog

    tracebuf traceBufPtr

    // traceSweep indicates the sweep events should be traced.
    // This is used to defer the sweep start event until a span
    // has actually been swept.
    traceSweep bool
    // traceSwept and traceReclaimed track the number of bytes
    // swept and reclaimed by sweeping in the current sweep loop.
    traceSwept, traceReclaimed uintptr

    palloc persistentAlloc // per-P to avoid mutex

    _ uint32 // Alignment for atomic fields below

    // Per-P GC state
    gcAssistTime         int64    // Nanoseconds in assistAlloc
    gcFractionalMarkTime int64    // Nanoseconds in fractional mark worker (atomic)
    gcBgMarkWorker       guintptr // (atomic)
    gcMarkWorkerMode     gcMarkWorkerMode

    // gcMarkWorkerStartTime is the nanotime() at which this mark
    // worker started.
    gcMarkWorkerStartTime int64

    // gcw is this P's GC work buffer cache. The work buffer is
    // filled by write barriers, drained by mutator assists, and
    // disposed on certain GC state transitions.
    gcw gcWork

    // wbBuf is this P's GC write barrier buffer.
    //
    // TODO: Consider caching this in the running G.
    wbBuf wbBuf

    runSafePointFn uint32 // if 1, run sched.safePointFn at next safe point

    pad cpu.CacheLinePad
}

type schedt struct {
    // accessed atomically. keep at top to ensure alignment on 32-bit systems.
    goidgen  uint64
    lastpoll uint64

    lock mutex

    // When increasing nmidle, nmidlelocked, nmsys, or nmfreed, be
    // sure to call checkdead().

    midle        muintptr // idle m's waiting for work
    nmidle       int32    // number of idle m's waiting for work
    nmidlelocked int32    // number of locked m's waiting for work
    mnext        int64    // number of m's that have been created and next M ID
    maxmcount    int32    // maximum number of m's allowed (or die)
    nmsys        int32    // number of system m's not counted for deadlock
    nmfreed      int64    // cumulative number of freed m's

    ngsys uint32 // number of system goroutines; updated atomically

    pidle      puintptr // idle p's
    npidle     uint32
    nmspinning uint32 // See "Worker thread parking/unparking" comment in proc.go.

    // Global runnable queue.
    runq     gQueue
    runqsize int32

    // disable controls selective disabling of the scheduler.
    //
    // Use schedEnableUser to control this.
    //
    // disable is protected by sched.lock.
    disable struct {
        // user disables scheduling of user goroutines.
        user     bool
        runnable gQueue // pending runnable Gs
        n        int32  // length of runnable
    }

    // Global cache of dead G's.
    gFree struct {
        lock    mutex
        stack   gList // Gs with stacks
        noStack gList // Gs without stacks
        n       int32
    }

    // Central cache of sudog structs.
    sudoglock  mutex
    sudogcache *sudog

    // Central pool of available defer structs of different sizes.
    deferlock mutex
    deferpool [5]*_defer

    // freem is the list of m's waiting to be freed when their
    // m.exited is set. Linked through m.freelink.
    freem *m

    gcwaiting  uint32 // gc is waiting to run
    stopwait   int32
    stopnote   note
    sysmonwait uint32
    sysmonnote note

    // safepointFn should be called on each P at the next GC
    // safepoint if p.runSafePointFn is set.
    safePointFn   func(*p)
    safePointWait int32
    safePointNote note

    profilehz int32 // cpu profiling rate

    procresizetime int64 // nanotime() of last change to gomaxprocs
    totaltime      int64 // ∫gomaxprocs dt up to procresizetime
}

// Values for the flags field of a sigTabT.
const (
    _SigNotify   = 1 << iota // let signal.Notify have signal, even if from kernel
    _SigKill                 // if signal.Notify doesn't take it, exit quietly
    _SigThrow                // if signal.Notify doesn't take it, exit loudly
    _SigPanic                // if the signal is from the kernel, panic
    _SigDefault              // if the signal isn't explicitly requested, don't monitor it
    _SigGoExit               // cause all runtime procs to exit (only used on Plan 9).
    _SigSetStack             // add SA_ONSTACK to libc handler
    _SigUnblock              // always unblock; see blockableSig
    _SigIgn                  // _SIG_DFL action is to ignore the signal
)

// Layout of in-memory per-function information prepared by linker
// See https://golang.org/s/go12symtab.
// Keep in sync with linker (../cmd/link/internal/ld/pcln.go:/pclntab)
// and with package debug/gosym and with symtab.go in package runtime.
type _func struct {
    entry   uintptr // start pc
    nameoff int32   // function name

    args        int32  // in/out args size
    deferreturn uint32 // offset of a deferreturn block from entry, if any.

    pcsp      int32
    pcfile    int32
    pcln      int32
    npcdata   int32
    funcID    funcID  // set for certain special runtime functions
    _         [2]int8 // unused
    nfuncdata uint8   // must be last
}

// Pseudo-Func that is returned for PCs that occur in inlined code.
// A *Func can be either a *_func or a *funcinl, and they are distinguished
// by the first uintptr.
type funcinl struct {
    zero  uintptr // set to 0 to distinguish from _func
    entry uintptr // entry of the real (the "outermost") frame.
    name  string
    file  string
    line  int
}

// layout of Itab known to compilers
// allocated in non-garbage-collected memory
// Needs to be in sync with
// ../cmd/compile/internal/gc/reflect.go:/^func.dumptypestructs.
type itab struct {
    inter *interfacetype
    _type *_type
    hash  uint32 // copy of _type.hash. Used for type switches.
    _     [4]byte
    fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
}

// Lock-free stack node.
// Also known to export_test.go.
type lfnode struct {
    next    uint64
    pushcnt uintptr
}

type forcegcstate struct {
    lock mutex
    g    *g
    idle uint32
}

// startup_random_data holds random bytes initialized at startup. These come from
// the ELF AT_RANDOM auxiliary vector (vdso_linux_amd64.go or os_linux_386.go).
var startupRandomData []byte

// extendRandom extends the random numbers in r[:n] to the whole slice r.
// Treats n<0 as n==0.
func extendRandom(r []byte, n int) {
    if n < 0 {
        n = 0
    }
    for n < len(r) {
        // Extend random bits using hash function & time seed
        w := n
        if w > 16 {
            w = 16
        }
        h := memhash(unsafe.Pointer(&r[n-w]), uintptr(nanotime()), uintptr(w))
        for i := 0; i < sys.PtrSize && n < len(r); i++ {
            r[n] = byte(h)
            n++
            h >>= 8
        }
    }
}

// A _defer holds an entry on the list of deferred calls.
// If you add a field here, add code to clear it in freedefer.
// This struct must match the code in cmd/compile/internal/gc/reflect.go:deferstruct
// and cmd/compile/internal/gc/ssa.go:(*state).call.
// Some defers will be allocated on the stack and some on the heap.
// All defers are logically part of the stack, so write barriers to
// initialize them are not required. All defers must be manually scanned,
// and for heap defers, marked.
type _defer struct {
    siz     int32 // includes both arguments and results
    started bool
    heap    bool
    sp      uintptr // sp at time of defer
    pc      uintptr
    fn      *funcval
    _panic  *_panic // panic that is running defer
    link    *_defer
}

// A _panic holds information about an active panic.
//
// This is marked go:notinheap because _panic values must only ever
// live on the stack.
//
// The argp and link fields are stack pointers, but don't need special
// handling during stack growth: because they are pointer-typed and
// _panic values only live on the stack, regular stack pointer
// adjustment takes care of them.
//
//go:notinheap
type _panic struct {
    argp      unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink
    arg       interface{}    // argument to panic
    link      *_panic        // link to earlier panic
    recovered bool           // whether this panic is over
    aborted   bool           // the panic was aborted
}

// stack traces
type stkframe struct {
    fn       funcInfo   // function being run
    pc       uintptr    // program counter within fn
    continpc uintptr    // program counter where execution can continue, or 0 if not
    lr       uintptr    // program counter at caller aka link register
    sp       uintptr    // stack pointer at pc
    fp       uintptr    // stack pointer at caller aka frame pointer
    varp     uintptr    // top of local variables
    argp     uintptr    // pointer to function arguments
    arglen   uintptr    // number of bytes at argp
    argmap   *bitvector // force use of this argmap
}

// ancestorInfo records details of where a goroutine was started.
type ancestorInfo struct {
    pcs  []uintptr // pcs from the stack of this goroutine
    goid int64     // goroutine id of this goroutine; original goroutine possibly dead
    gopc uintptr   // pc of go statement that created this goroutine
}

const (
    _TraceRuntimeFrames = 1 << iota // include frames for internal runtime functions.
    _TraceTrap                      // the initial PC, SP are from a trap, not a return PC from a call
    _TraceJumpStack                 // if traceback is on a systemstack, resume trace at g that called into it
)

// The maximum number of frames we print for a traceback
const _TracebackMaxFrames = 100

// A waitReason explains why a goroutine has been stopped.
// See gopark. Do not re-use waitReasons, add new ones.
type waitReason uint8

const (
    waitReasonZero                  waitReason = iota // ""
    waitReasonGCAssistMarking                         // "GC assist marking"
    waitReasonIOWait                                  // "IO wait"
    waitReasonChanReceiveNilChan                      // "chan receive (nil chan)"
    waitReasonChanSendNilChan                         // "chan send (nil chan)"
    waitReasonDumpingHeap                             // "dumping heap"
    waitReasonGarbageCollection                       // "garbage collection"
    waitReasonGarbageCollectionScan                   // "garbage collection scan"
    waitReasonPanicWait                               // "panicwait"
    waitReasonSelect                                  // "select"
    waitReasonSelectNoCases                           // "select (no cases)"
    waitReasonGCAssistWait                            // "GC assist wait"
    waitReasonGCSweepWait                             // "GC sweep wait"
    waitReasonGCScavengeWait                          // "GC scavenge wait"
    waitReasonChanReceive                             // "chan receive"
    waitReasonChanSend                                // "chan send"
    waitReasonFinalizerWait                           // "finalizer wait"
    waitReasonForceGGIdle                             // "force gc (idle)"
    waitReasonSemacquire                              // "semacquire"
    waitReasonSleep                                   // "sleep"
    waitReasonSyncCondWait                            // "sync.Cond.Wait"
    waitReasonTimerGoroutineIdle                      // "timer goroutine (idle)"
    waitReasonTraceReaderBlocked                      // "trace reader (blocked)"
    waitReasonWaitForGCCycle                          // "wait for GC cycle"
    waitReasonGCWorkerIdle                            // "GC worker (idle)"
)

var waitReasonStrings = [...]string{
    waitReasonZero:                  "",
    waitReasonGCAssistMarking:       "GC assist marking",
    waitReasonIOWait:                "IO wait",
    waitReasonChanReceiveNilChan:    "chan receive (nil chan)",
    waitReasonChanSendNilChan:       "chan send (nil chan)",
    waitReasonDumpingHeap:           "dumping heap",
    waitReasonGarbageCollection:     "garbage collection",
    waitReasonGarbageCollectionScan: "garbage collection scan",
    waitReasonPanicWait:             "panicwait",
    waitReasonSelect:                "select",
    waitReasonSelectNoCases:         "select (no cases)",
    waitReasonGCAssistWait:          "GC assist wait",
    waitReasonGCSweepWait:           "GC sweep wait",
    waitReasonGCScavengeWait:        "GC scavenge wait",
    waitReasonChanReceive:           "chan receive",
    waitReasonChanSend:              "chan send",
    waitReasonFinalizerWait:         "finalizer wait",
    waitReasonForceGGIdle:           "force gc (idle)",
    waitReasonSemacquire:            "semacquire",
    waitReasonSleep:                 "sleep",
    waitReasonSyncCondWait:          "sync.Cond.Wait",
    waitReasonTimerGoroutineIdle:    "timer goroutine (idle)",
    waitReasonTraceReaderBlocked:    "trace reader (blocked)",
    waitReasonWaitForGCCycle:        "wait for GC cycle",
    waitReasonGCWorkerIdle:          "GC worker (idle)",
}

func (w waitReason) String() string {
    if w < 0 || w >= waitReason(len(waitReasonStrings)) {
        return "unknown wait reason"
    }
    return waitReasonStrings[w]
}

var (
    allglen    uintptr
    allm       *m
    allp       []*p  // len(allp) == gomaxprocs; may change at safe points, otherwise immutable
    allpLock   mutex // Protects P-less reads of allp and all writes
    gomaxprocs int32
    ncpu       int32
    forcegc    forcegcstate
    sched      schedt
    newprocs   int32

    // Information about what cpu features are available.
    // Packages outside the runtime should not use these
    // as they are not an external api.
    // Set on startup in asm_{386,amd64,amd64p32}.s
    processorVersionInfo uint32
    isIntel              bool
    lfenceBeforeRdtsc    bool

    goarm                uint8 // set by cmd/link on arm systems
    framepointer_enabled bool  // set by cmd/link
)

// Set by the linker so the runtime can determine the buildmode.
var (
    islibrary bool // -buildmode=c-shared
    isarchive bool // -buildmode=c-archive
)

相关文章

网友评论

      本文标题:Go runtime包源码

      本文链接:https://www.haomeiwen.com/subject/cijbyhtx.html