Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/proc.go

Documentation: runtime

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime
     6  
     7  import (
     8  	"internal/cpu"
     9  	"runtime/internal/atomic"
    10  	"runtime/internal/sys"
    11  	"unsafe"
    12  )
    13  
    14  var buildVersion = sys.TheVersion
    15  
    16  // set using cmd/go/internal/modload.ModInfoProg
    17  var modinfo string
    18  
    19  // Goroutine scheduler
    20  // The scheduler's job is to distribute ready-to-run goroutines over worker threads.
    21  //
    22  // The main concepts are:
    23  // G - goroutine.
    24  // M - worker thread, or machine.
    25  // P - processor, a resource that is required to execute Go code.
    26  //     M must have an associated P to execute Go code, however it can be
    27  //     blocked or in a syscall w/o an associated P.
    28  //
    29  // Design doc at https://golang.org/s/go11sched.
    30  
    31  // Worker thread parking/unparking.
    32  // We need to balance between keeping enough running worker threads to utilize
    33  // available hardware parallelism and parking excessive running worker threads
    34  // to conserve CPU resources and power. This is not simple for two reasons:
    35  // (1) scheduler state is intentionally distributed (in particular, per-P work
    36  // queues), so it is not possible to compute global predicates on fast paths;
    37  // (2) for optimal thread management we would need to know the future (don't park
    38  // a worker thread when a new goroutine will be readied in near future).
    39  //
    40  // Three rejected approaches that would work badly:
    41  // 1. Centralize all scheduler state (would inhibit scalability).
    42  // 2. Direct goroutine handoff. That is, when we ready a new goroutine and there
    43  //    is a spare P, unpark a thread and handoff it the thread and the goroutine.
    44  //    This would lead to thread state thrashing, as the thread that readied the
    45  //    goroutine can be out of work the very next moment, we will need to park it.
    46  //    Also, it would destroy locality of computation as we want to preserve
    47  //    dependent goroutines on the same thread; and introduce additional latency.
    48  // 3. Unpark an additional thread whenever we ready a goroutine and there is an
    49  //    idle P, but don't do handoff. This would lead to excessive thread parking/
    50  //    unparking as the additional threads will instantly park without discovering
    51  //    any work to do.
    52  //
    53  // The current approach:
    54  // We unpark an additional thread when we ready a goroutine if (1) there is an
    55  // idle P and there are no "spinning" worker threads. A worker thread is considered
    56  // spinning if it is out of local work and did not find work in global run queue/
    57  // netpoller; the spinning state is denoted in m.spinning and in sched.nmspinning.
    58  // Threads unparked this way are also considered spinning; we don't do goroutine
    59  // handoff so such threads are out of work initially. Spinning threads do some
    60  // spinning looking for work in per-P run queues before parking. If a spinning
    61  // thread finds work it takes itself out of the spinning state and proceeds to
    62  // execution. If it does not find work it takes itself out of the spinning state
    63  // and then parks.
    64  // If there is at least one spinning thread (sched.nmspinning>1), we don't unpark
    65  // new threads when readying goroutines. To compensate for that, if the last spinning
    66  // thread finds work and stops spinning, it must unpark a new spinning thread.
    67  // This approach smooths out unjustified spikes of thread unparking,
    68  // but at the same time guarantees eventual maximal CPU parallelism utilization.
    69  //
    70  // The main implementation complication is that we need to be very careful during
    71  // spinning->non-spinning thread transition. This transition can race with submission
    72  // of a new goroutine, and either one part or another needs to unpark another worker
    73  // thread. If they both fail to do that, we can end up with semi-persistent CPU
    74  // underutilization. The general pattern for goroutine readying is: submit a goroutine
    75  // to local work queue, #StoreLoad-style memory barrier, check sched.nmspinning.
    76  // The general pattern for spinning->non-spinning transition is: decrement nmspinning,
    77  // #StoreLoad-style memory barrier, check all per-P work queues for new work.
    78  // Note that all this complexity does not apply to global run queue as we are not
    79  // sloppy about thread unparking when submitting to global queue. Also see comments
    80  // for nmspinning manipulation.
    81  
    82  var (
    83  	m0           m
    84  	g0           g
    85  	mcache0      *mcache
    86  	raceprocctx0 uintptr
    87  )
    88  
    89  //go:linkname runtime_inittask runtime..inittask
    90  var runtime_inittask initTask
    91  
    92  //go:linkname main_inittask main..inittask
    93  var main_inittask initTask
    94  
    95  // main_init_done is a signal used by cgocallbackg that initialization
    96  // has been completed. It is made before _cgo_notify_runtime_init_done,
    97  // so all cgo calls can rely on it existing. When main_init is complete,
    98  // it is closed, meaning cgocallbackg can reliably receive from it.
    99  var main_init_done chan bool
   100  
   101  //go:linkname main_main main.main
   102  func main_main()
   103  
   104  // mainStarted indicates that the main M has started.
   105  var mainStarted bool
   106  
   107  // runtimeInitTime is the nanotime() at which the runtime started.
   108  var runtimeInitTime int64
   109  
   110  // Value to use for signal mask for newly created M's.
   111  var initSigmask sigset
   112  
   113  // The main goroutine.
   114  func main() {
   115  	g := getg()
   116  
   117  	// Racectx of m0->g0 is used only as the parent of the main goroutine.
   118  	// It must not be used for anything else.
   119  	g.m.g0.racectx = 0
   120  
   121  	// Max stack size is 1 GB on 64-bit, 250 MB on 32-bit.
   122  	// Using decimal instead of binary GB and MB because
   123  	// they look nicer in the stack overflow failure message.
   124  	if sys.PtrSize == 8 {
   125  		maxstacksize = 1000000000
   126  	} else {
   127  		maxstacksize = 250000000
   128  	}
   129  
   130  	// Allow newproc to start new Ms.
   131  	mainStarted = true
   132  
   133  	if GOARCH != "wasm" { // no threads on wasm yet, so no sysmon
   134  		systemstack(func() {
   135  			newm(sysmon, nil, -1)
   136  		})
   137  	}
   138  
   139  	// Lock the main goroutine onto this, the main OS thread,
   140  	// during initialization. Most programs won't care, but a few
   141  	// do require certain calls to be made by the main thread.
   142  	// Those can arrange for main.main to run in the main thread
   143  	// by calling runtime.LockOSThread during initialization
   144  	// to preserve the lock.
   145  	lockOSThread()
   146  
   147  	if g.m != &m0 {
   148  		throw("runtime.main not on m0")
   149  	}
   150  
   151  	doInit(&runtime_inittask) // must be before defer
   152  	if nanotime() == 0 {
   153  		throw("nanotime returning zero")
   154  	}
   155  
   156  	// Defer unlock so that runtime.Goexit during init does the unlock too.
   157  	needUnlock := true
   158  	defer func() {
   159  		if needUnlock {
   160  			unlockOSThread()
   161  		}
   162  	}()
   163  
   164  	// Record when the world started.
   165  	runtimeInitTime = nanotime()
   166  
   167  	gcenable()
   168  
   169  	main_init_done = make(chan bool)
   170  	if iscgo {
   171  		if _cgo_thread_start == nil {
   172  			throw("_cgo_thread_start missing")
   173  		}
   174  		if GOOS != "windows" {
   175  			if _cgo_setenv == nil {
   176  				throw("_cgo_setenv missing")
   177  			}
   178  			if _cgo_unsetenv == nil {
   179  				throw("_cgo_unsetenv missing")
   180  			}
   181  		}
   182  		if _cgo_notify_runtime_init_done == nil {
   183  			throw("_cgo_notify_runtime_init_done missing")
   184  		}
   185  		// Start the template thread in case we enter Go from
   186  		// a C-created thread and need to create a new thread.
   187  		startTemplateThread()
   188  		cgocall(_cgo_notify_runtime_init_done, nil)
   189  	}
   190  
   191  	doInit(&main_inittask)
   192  
   193  	close(main_init_done)
   194  
   195  	needUnlock = false
   196  	unlockOSThread()
   197  
   198  	if isarchive || islibrary {
   199  		// A program compiled with -buildmode=c-archive or c-shared
   200  		// has a main, but it is not executed.
   201  		return
   202  	}
   203  	fn := main_main // make an indirect call, as the linker doesn't know the address of the main package when laying down the runtime
   204  	fn()
   205  	if raceenabled {
   206  		racefini()
   207  	}
   208  
   209  	// Make racy client program work: if panicking on
   210  	// another goroutine at the same time as main returns,
   211  	// let the other goroutine finish printing the panic trace.
   212  	// Once it does, it will exit. See issues 3934 and 20018.
   213  	if atomic.Load(&runningPanicDefers) != 0 {
   214  		// Running deferred functions should not take long.
   215  		for c := 0; c < 1000; c++ {
   216  			if atomic.Load(&runningPanicDefers) == 0 {
   217  				break
   218  			}
   219  			Gosched()
   220  		}
   221  	}
   222  	if atomic.Load(&panicking) != 0 {
   223  		gopark(nil, nil, waitReasonPanicWait, traceEvGoStop, 1)
   224  	}
   225  
   226  	exit(0)
   227  	for {
   228  		var x *int32
   229  		*x = 0
   230  	}
   231  }
   232  
   233  // os_beforeExit is called from os.Exit(0).
   234  //go:linkname os_beforeExit os.runtime_beforeExit
   235  func os_beforeExit() {
   236  	if raceenabled {
   237  		racefini()
   238  	}
   239  }
   240  
   241  // start forcegc helper goroutine
   242  func init() {
   243  	go forcegchelper()
   244  }
   245  
   246  func forcegchelper() {
   247  	forcegc.g = getg()
   248  	lockInit(&forcegc.lock, lockRankForcegc)
   249  	for {
   250  		lock(&forcegc.lock)
   251  		if forcegc.idle != 0 {
   252  			throw("forcegc: phase error")
   253  		}
   254  		atomic.Store(&forcegc.idle, 1)
   255  		goparkunlock(&forcegc.lock, waitReasonForceGCIdle, traceEvGoBlock, 1)
   256  		// this goroutine is explicitly resumed by sysmon
   257  		if debug.gctrace > 0 {
   258  			println("GC forced")
   259  		}
   260  		// Time-triggered, fully concurrent.
   261  		gcStart(gcTrigger{kind: gcTriggerTime, now: nanotime()})
   262  	}
   263  }
   264  
   265  //go:nosplit
   266  
   267  // Gosched yields the processor, allowing other goroutines to run. It does not
   268  // suspend the current goroutine, so execution resumes automatically.
   269  func Gosched() {
   270  	checkTimeouts()
   271  	mcall(gosched_m)
   272  }
   273  
   274  // goschedguarded yields the processor like gosched, but also checks
   275  // for forbidden states and opts out of the yield in those cases.
   276  //go:nosplit
   277  func goschedguarded() {
   278  	mcall(goschedguarded_m)
   279  }
   280  
   281  // Puts the current goroutine into a waiting state and calls unlockf.
   282  // If unlockf returns false, the goroutine is resumed.
   283  // unlockf must not access this G's stack, as it may be moved between
   284  // the call to gopark and the call to unlockf.
   285  // Reason explains why the goroutine has been parked.
   286  // It is displayed in stack traces and heap dumps.
   287  // Reasons should be unique and descriptive.
   288  // Do not re-use reasons, add new ones.
   289  func gopark(unlockf func(*g, unsafe.Pointer) bool, lock unsafe.Pointer, reason waitReason, traceEv byte, traceskip int) {
   290  	if reason != waitReasonSleep {
   291  		checkTimeouts() // timeouts may expire while two goroutines keep the scheduler busy
   292  	}
   293  	mp := acquirem()
   294  	gp := mp.curg
   295  	status := readgstatus(gp)
   296  	if status != _Grunning && status != _Gscanrunning {
   297  		throw("gopark: bad g status")
   298  	}
   299  	mp.waitlock = lock
   300  	mp.waitunlockf = unlockf
   301  	gp.waitreason = reason
   302  	mp.waittraceev = traceEv
   303  	mp.waittraceskip = traceskip
   304  	releasem(mp)
   305  	// can't do anything that might move the G between Ms here.
   306  	mcall(park_m)
   307  }
   308  
   309  // Puts the current goroutine into a waiting state and unlocks the lock.
   310  // The goroutine can be made runnable again by calling goready(gp).
   311  func goparkunlock(lock *mutex, reason waitReason, traceEv byte, traceskip int) {
   312  	gopark(parkunlock_c, unsafe.Pointer(lock), reason, traceEv, traceskip)
   313  }
   314  
   315  func goready(gp *g, traceskip int) {
   316  	systemstack(func() {
   317  		ready(gp, traceskip, true)
   318  	})
   319  }
   320  
   321  //go:nosplit
   322  func acquireSudog() *sudog {
   323  	// Delicate dance: the semaphore implementation calls
   324  	// acquireSudog, acquireSudog calls new(sudog),
   325  	// new calls malloc, malloc can call the garbage collector,
   326  	// and the garbage collector calls the semaphore implementation
   327  	// in stopTheWorld.
   328  	// Break the cycle by doing acquirem/releasem around new(sudog).
   329  	// The acquirem/releasem increments m.locks during new(sudog),
   330  	// which keeps the garbage collector from being invoked.
   331  	mp := acquirem()
   332  	pp := mp.p.ptr()
   333  	if len(pp.sudogcache) == 0 {
   334  		lock(&sched.sudoglock)
   335  		// First, try to grab a batch from central cache.
   336  		for len(pp.sudogcache) < cap(pp.sudogcache)/2 && sched.sudogcache != nil {
   337  			s := sched.sudogcache
   338  			sched.sudogcache = s.next
   339  			s.next = nil
   340  			pp.sudogcache = append(pp.sudogcache, s)
   341  		}
   342  		unlock(&sched.sudoglock)
   343  		// If the central cache is empty, allocate a new one.
   344  		if len(pp.sudogcache) == 0 {
   345  			pp.sudogcache = append(pp.sudogcache, new(sudog))
   346  		}
   347  	}
   348  	n := len(pp.sudogcache)
   349  	s := pp.sudogcache[n-1]
   350  	pp.sudogcache[n-1] = nil
   351  	pp.sudogcache = pp.sudogcache[:n-1]
   352  	if s.elem != nil {
   353  		throw("acquireSudog: found s.elem != nil in cache")
   354  	}
   355  	releasem(mp)
   356  	return s
   357  }
   358  
   359  //go:nosplit
   360  func releaseSudog(s *sudog) {
   361  	if s.elem != nil {
   362  		throw("runtime: sudog with non-nil elem")
   363  	}
   364  	if s.isSelect {
   365  		throw("runtime: sudog with non-false isSelect")
   366  	}
   367  	if s.next != nil {
   368  		throw("runtime: sudog with non-nil next")
   369  	}
   370  	if s.prev != nil {
   371  		throw("runtime: sudog with non-nil prev")
   372  	}
   373  	if s.waitlink != nil {
   374  		throw("runtime: sudog with non-nil waitlink")
   375  	}
   376  	if s.c != nil {
   377  		throw("runtime: sudog with non-nil c")
   378  	}
   379  	gp := getg()
   380  	if gp.param != nil {
   381  		throw("runtime: releaseSudog with non-nil gp.param")
   382  	}
   383  	mp := acquirem() // avoid rescheduling to another P
   384  	pp := mp.p.ptr()
   385  	if len(pp.sudogcache) == cap(pp.sudogcache) {
   386  		// Transfer half of local cache to the central cache.
   387  		var first, last *sudog
   388  		for len(pp.sudogcache) > cap(pp.sudogcache)/2 {
   389  			n := len(pp.sudogcache)
   390  			p := pp.sudogcache[n-1]
   391  			pp.sudogcache[n-1] = nil
   392  			pp.sudogcache = pp.sudogcache[:n-1]
   393  			if first == nil {
   394  				first = p
   395  			} else {
   396  				last.next = p
   397  			}
   398  			last = p
   399  		}
   400  		lock(&sched.sudoglock)
   401  		last.next = sched.sudogcache
   402  		sched.sudogcache = first
   403  		unlock(&sched.sudoglock)
   404  	}
   405  	pp.sudogcache = append(pp.sudogcache, s)
   406  	releasem(mp)
   407  }
   408  
   409  // funcPC returns the entry PC of the function f.
   410  // It assumes that f is a func value. Otherwise the behavior is undefined.
   411  // CAREFUL: In programs with plugins, funcPC can return different values
   412  // for the same function (because there are actually multiple copies of
   413  // the same function in the address space). To be safe, don't use the
   414  // results of this function in any == expression. It is only safe to
   415  // use the result as an address at which to start executing code.
   416  //go:nosplit
   417  func funcPC(f interface{}) uintptr {
   418  	return *(*uintptr)(efaceOf(&f).data)
   419  }
   420  
   421  // called from assembly
   422  func badmcall(fn func(*g)) {
   423  	throw("runtime: mcall called on m->g0 stack")
   424  }
   425  
   426  func badmcall2(fn func(*g)) {
   427  	throw("runtime: mcall function returned")
   428  }
   429  
   430  func badreflectcall() {
   431  	panic(plainError("arg size to reflect.call more than 1GB"))
   432  }
   433  
   434  var badmorestackg0Msg = "fatal: morestack on g0\n"
   435  
   436  //go:nosplit
   437  //go:nowritebarrierrec
   438  func badmorestackg0() {
   439  	sp := stringStructOf(&badmorestackg0Msg)
   440  	write(2, sp.str, int32(sp.len))
   441  }
   442  
   443  var badmorestackgsignalMsg = "fatal: morestack on gsignal\n"
   444  
   445  //go:nosplit
   446  //go:nowritebarrierrec
   447  func badmorestackgsignal() {
   448  	sp := stringStructOf(&badmorestackgsignalMsg)
   449  	write(2, sp.str, int32(sp.len))
   450  }
   451  
   452  //go:nosplit
   453  func badctxt() {
   454  	throw("ctxt != 0")
   455  }
   456  
   457  func lockedOSThread() bool {
   458  	gp := getg()
   459  	return gp.lockedm != 0 && gp.m.lockedg != 0
   460  }
   461  
   462  var (
   463  	allgs    []*g
   464  	allglock mutex
   465  )
   466  
   467  func allgadd(gp *g) {
   468  	if readgstatus(gp) == _Gidle {
   469  		throw("allgadd: bad status Gidle")
   470  	}
   471  
   472  	lock(&allglock)
   473  	allgs = append(allgs, gp)
   474  	allglen = uintptr(len(allgs))
   475  	unlock(&allglock)
   476  }
   477  
   478  const (
   479  	// Number of goroutine ids to grab from sched.goidgen to local per-P cache at once.
   480  	// 16 seems to provide enough amortization, but other than that it's mostly arbitrary number.
   481  	_GoidCacheBatch = 16
   482  )
   483  
   484  // cpuinit extracts the environment variable GODEBUG from the environment on
   485  // Unix-like operating systems and calls internal/cpu.Initialize.
   486  func cpuinit() {
   487  	const prefix = "GODEBUG="
   488  	var env string
   489  
   490  	switch GOOS {
   491  	case "aix", "darwin", "dragonfly", "freebsd", "netbsd", "openbsd", "illumos", "solaris", "linux":
   492  		cpu.DebugOptions = true
   493  
   494  		// Similar to goenv_unix but extracts the environment value for
   495  		// GODEBUG directly.
   496  		// TODO(moehrmann): remove when general goenvs() can be called before cpuinit()
   497  		n := int32(0)
   498  		for argv_index(argv, argc+1+n) != nil {
   499  			n++
   500  		}
   501  
   502  		for i := int32(0); i < n; i++ {
   503  			p := argv_index(argv, argc+1+i)
   504  			s := *(*string)(unsafe.Pointer(&stringStruct{unsafe.Pointer(p), findnull(p)}))
   505  
   506  			if hasPrefix(s, prefix) {
   507  				env = gostring(p)[len(prefix):]
   508  				break
   509  			}
   510  		}
   511  	}
   512  
   513  	cpu.Initialize(env)
   514  
   515  	// Support cpu feature variables are used in code generated by the compiler
   516  	// to guard execution of instructions that can not be assumed to be always supported.
   517  	x86HasPOPCNT = cpu.X86.HasPOPCNT
   518  	x86HasSSE41 = cpu.X86.HasSSE41
   519  	x86HasFMA = cpu.X86.HasFMA
   520  
   521  	armHasVFPv4 = cpu.ARM.HasVFPv4
   522  
   523  	arm64HasATOMICS = cpu.ARM64.HasATOMICS
   524  }
   525  
   526  // The bootstrap sequence is:
   527  //
   528  //	call osinit
   529  //	call schedinit
   530  //	make & queue new G
   531  //	call runtime·mstart
   532  //
   533  // The new G calls runtime·main.
   534  func schedinit() {
   535  	lockInit(&sched.lock, lockRankSched)
   536  	lockInit(&sched.sysmonlock, lockRankSysmon)
   537  	lockInit(&sched.deferlock, lockRankDefer)
   538  	lockInit(&sched.sudoglock, lockRankSudog)
   539  	lockInit(&deadlock, lockRankDeadlock)
   540  	lockInit(&paniclk, lockRankPanic)
   541  	lockInit(&allglock, lockRankAllg)
   542  	lockInit(&allpLock, lockRankAllp)
   543  	lockInit(&reflectOffs.lock, lockRankReflectOffs)
   544  	lockInit(&finlock, lockRankFin)
   545  	lockInit(&trace.bufLock, lockRankTraceBuf)
   546  	lockInit(&trace.stringsLock, lockRankTraceStrings)
   547  	lockInit(&trace.lock, lockRankTrace)
   548  	lockInit(&cpuprof.lock, lockRankCpuprof)
   549  	lockInit(&trace.stackTab.lock, lockRankTraceStackTab)
   550  
   551  	// raceinit must be the first call to race detector.
   552  	// In particular, it must be done before mallocinit below calls racemapshadow.
   553  	_g_ := getg()
   554  	if raceenabled {
   555  		_g_.racectx, raceprocctx0 = raceinit()
   556  	}
   557  
   558  	sched.maxmcount = 10000
   559  
   560  	tracebackinit()
   561  	moduledataverify()
   562  	stackinit()
   563  	mallocinit()
   564  	fastrandinit() // must run before mcommoninit
   565  	mcommoninit(_g_.m, -1)
   566  	cpuinit()       // must run before alginit
   567  	alginit()       // maps must not be used before this call
   568  	modulesinit()   // provides activeModules
   569  	typelinksinit() // uses maps, activeModules
   570  	itabsinit()     // uses activeModules
   571  
   572  	sigsave(&_g_.m.sigmask)
   573  	initSigmask = _g_.m.sigmask
   574  
   575  	goargs()
   576  	goenvs()
   577  	parsedebugvars()
   578  	gcinit()
   579  
   580  	sched.lastpoll = uint64(nanotime())
   581  	procs := ncpu
   582  	if n, ok := atoi32(gogetenv("GOMAXPROCS")); ok && n > 0 {
   583  		procs = n
   584  	}
   585  	if procresize(procs) != nil {
   586  		throw("unknown runnable goroutine during bootstrap")
   587  	}
   588  
   589  	// For cgocheck > 1, we turn on the write barrier at all times
   590  	// and check all pointer writes. We can't do this until after
   591  	// procresize because the write barrier needs a P.
   592  	if debug.cgocheck > 1 {
   593  		writeBarrier.cgo = true
   594  		writeBarrier.enabled = true
   595  		for _, p := range allp {
   596  			p.wbBuf.reset()
   597  		}
   598  	}
   599  
   600  	if buildVersion == "" {
   601  		// Condition should never trigger. This code just serves
   602  		// to ensure runtime·buildVersion is kept in the resulting binary.
   603  		buildVersion = "unknown"
   604  	}
   605  	if len(modinfo) == 1 {
   606  		// Condition should never trigger. This code just serves
   607  		// to ensure runtime·modinfo is kept in the resulting binary.
   608  		modinfo = ""
   609  	}
   610  }
   611  
   612  func dumpgstatus(gp *g) {
   613  	_g_ := getg()
   614  	print("runtime: gp: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
   615  	print("runtime:  g:  g=", _g_, ", goid=", _g_.goid, ",  g->atomicstatus=", readgstatus(_g_), "\n")
   616  }
   617  
   618  func checkmcount() {
   619  	// sched lock is held
   620  	if mcount() > sched.maxmcount {
   621  		print("runtime: program exceeds ", sched.maxmcount, "-thread limit\n")
   622  		throw("thread exhaustion")
   623  	}
   624  }
   625  
   626  // mReserveID returns the next ID to use for a new m. This new m is immediately
   627  // considered 'running' by checkdead.
   628  //
   629  // sched.lock must be held.
   630  func mReserveID() int64 {
   631  	if sched.mnext+1 < sched.mnext {
   632  		throw("runtime: thread ID overflow")
   633  	}
   634  	id := sched.mnext
   635  	sched.mnext++
   636  	checkmcount()
   637  	return id
   638  }
   639  
   640  // Pre-allocated ID may be passed as 'id', or omitted by passing -1.
   641  func mcommoninit(mp *m, id int64) {
   642  	_g_ := getg()
   643  
   644  	// g0 stack won't make sense for user (and is not necessary unwindable).
   645  	if _g_ != _g_.m.g0 {
   646  		callers(1, mp.createstack[:])
   647  	}
   648  
   649  	lock(&sched.lock)
   650  
   651  	if id >= 0 {
   652  		mp.id = id
   653  	} else {
   654  		mp.id = mReserveID()
   655  	}
   656  
   657  	mp.fastrand[0] = uint32(int64Hash(uint64(mp.id), fastrandseed))
   658  	mp.fastrand[1] = uint32(int64Hash(uint64(cputicks()), ^fastrandseed))
   659  	if mp.fastrand[0]|mp.fastrand[1] == 0 {
   660  		mp.fastrand[1] = 1
   661  	}
   662  
   663  	mpreinit(mp)
   664  	if mp.gsignal != nil {
   665  		mp.gsignal.stackguard1 = mp.gsignal.stack.lo + _StackGuard
   666  	}
   667  
   668  	// Add to allm so garbage collector doesn't free g->m
   669  	// when it is just in a register or thread-local storage.
   670  	mp.alllink = allm
   671  
   672  	// NumCgoCall() iterates over allm w/o schedlock,
   673  	// so we need to publish it safely.
   674  	atomicstorep(unsafe.Pointer(&allm), unsafe.Pointer(mp))
   675  	unlock(&sched.lock)
   676  
   677  	// Allocate memory to hold a cgo traceback if the cgo call crashes.
   678  	if iscgo || GOOS == "solaris" || GOOS == "illumos" || GOOS == "windows" {
   679  		mp.cgoCallers = new(cgoCallers)
   680  	}
   681  }
   682  
   683  var fastrandseed uintptr
   684  
   685  func fastrandinit() {
   686  	s := (*[unsafe.Sizeof(fastrandseed)]byte)(unsafe.Pointer(&fastrandseed))[:]
   687  	getRandomData(s)
   688  }
   689  
   690  // Mark gp ready to run.
   691  func ready(gp *g, traceskip int, next bool) {
   692  	if trace.enabled {
   693  		traceGoUnpark(gp, traceskip)
   694  	}
   695  
   696  	status := readgstatus(gp)
   697  
   698  	// Mark runnable.
   699  	_g_ := getg()
   700  	mp := acquirem() // disable preemption because it can be holding p in a local var
   701  	if status&^_Gscan != _Gwaiting {
   702  		dumpgstatus(gp)
   703  		throw("bad g->status in ready")
   704  	}
   705  
   706  	// status is Gwaiting or Gscanwaiting, make Grunnable and put on runq
   707  	casgstatus(gp, _Gwaiting, _Grunnable)
   708  	runqput(_g_.m.p.ptr(), gp, next)
   709  	wakep()
   710  	releasem(mp)
   711  }
   712  
   713  // freezeStopWait is a large value that freezetheworld sets
   714  // sched.stopwait to in order to request that all Gs permanently stop.
   715  const freezeStopWait = 0x7fffffff
   716  
   717  // freezing is set to non-zero if the runtime is trying to freeze the
   718  // world.
   719  var freezing uint32
   720  
   721  // Similar to stopTheWorld but best-effort and can be called several times.
   722  // There is no reverse operation, used during crashing.
   723  // This function must not lock any mutexes.
   724  func freezetheworld() {
   725  	atomic.Store(&freezing, 1)
   726  	// stopwait and preemption requests can be lost
   727  	// due to races with concurrently executing threads,
   728  	// so try several times
   729  	for i := 0; i < 5; i++ {
   730  		// this should tell the scheduler to not start any new goroutines
   731  		sched.stopwait = freezeStopWait
   732  		atomic.Store(&sched.gcwaiting, 1)
   733  		// this should stop running goroutines
   734  		if !preemptall() {
   735  			break // no running goroutines
   736  		}
   737  		usleep(1000)
   738  	}
   739  	// to be sure
   740  	usleep(1000)
   741  	preemptall()
   742  	usleep(1000)
   743  }
   744  
   745  // All reads and writes of g's status go through readgstatus, casgstatus
   746  // castogscanstatus, casfrom_Gscanstatus.
   747  //go:nosplit
   748  func readgstatus(gp *g) uint32 {
   749  	return atomic.Load(&gp.atomicstatus)
   750  }
   751  
   752  // The Gscanstatuses are acting like locks and this releases them.
   753  // If it proves to be a performance hit we should be able to make these
   754  // simple atomic stores but for now we are going to throw if
   755  // we see an inconsistent state.
   756  func casfrom_Gscanstatus(gp *g, oldval, newval uint32) {
   757  	success := false
   758  
   759  	// Check that transition is valid.
   760  	switch oldval {
   761  	default:
   762  		print("runtime: casfrom_Gscanstatus bad oldval gp=", gp, ", oldval=", hex(oldval), ", newval=", hex(newval), "\n")
   763  		dumpgstatus(gp)
   764  		throw("casfrom_Gscanstatus:top gp->status is not in scan state")
   765  	case _Gscanrunnable,
   766  		_Gscanwaiting,
   767  		_Gscanrunning,
   768  		_Gscansyscall,
   769  		_Gscanpreempted:
   770  		if newval == oldval&^_Gscan {
   771  			success = atomic.Cas(&gp.atomicstatus, oldval, newval)
   772  		}
   773  	}
   774  	if !success {
   775  		print("runtime: casfrom_Gscanstatus failed gp=", gp, ", oldval=", hex(oldval), ", newval=", hex(newval), "\n")
   776  		dumpgstatus(gp)
   777  		throw("casfrom_Gscanstatus: gp->status is not in scan state")
   778  	}
   779  	releaseLockRank(lockRankGscan)
   780  }
   781  
   782  // This will return false if the gp is not in the expected status and the cas fails.
   783  // This acts like a lock acquire while the casfromgstatus acts like a lock release.
   784  func castogscanstatus(gp *g, oldval, newval uint32) bool {
   785  	switch oldval {
   786  	case _Grunnable,
   787  		_Grunning,
   788  		_Gwaiting,
   789  		_Gsyscall:
   790  		if newval == oldval|_Gscan {
   791  			r := atomic.Cas(&gp.atomicstatus, oldval, newval)
   792  			if r {
   793  				acquireLockRank(lockRankGscan)
   794  			}
   795  			return r
   796  
   797  		}
   798  	}
   799  	print("runtime: castogscanstatus oldval=", hex(oldval), " newval=", hex(newval), "\n")
   800  	throw("castogscanstatus")
   801  	panic("not reached")
   802  }
   803  
   804  // If asked to move to or from a Gscanstatus this will throw. Use the castogscanstatus
   805  // and casfrom_Gscanstatus instead.
   806  // casgstatus will loop if the g->atomicstatus is in a Gscan status until the routine that
   807  // put it in the Gscan state is finished.
   808  //go:nosplit
   809  func casgstatus(gp *g, oldval, newval uint32) {
   810  	if (oldval&_Gscan != 0) || (newval&_Gscan != 0) || oldval == newval {
   811  		systemstack(func() {
   812  			print("runtime: casgstatus: oldval=", hex(oldval), " newval=", hex(newval), "\n")
   813  			throw("casgstatus: bad incoming values")
   814  		})
   815  	}
   816  
   817  	acquireLockRank(lockRankGscan)
   818  	releaseLockRank(lockRankGscan)
   819  
   820  	// See https://golang.org/cl/21503 for justification of the yield delay.
   821  	const yieldDelay = 5 * 1000
   822  	var nextYield int64
   823  
   824  	// loop if gp->atomicstatus is in a scan state giving
   825  	// GC time to finish and change the state to oldval.
   826  	for i := 0; !atomic.Cas(&gp.atomicstatus, oldval, newval); i++ {
   827  		if oldval == _Gwaiting && gp.atomicstatus == _Grunnable {
   828  			throw("casgstatus: waiting for Gwaiting but is Grunnable")
   829  		}
   830  		if i == 0 {
   831  			nextYield = nanotime() + yieldDelay
   832  		}
   833  		if nanotime() < nextYield {
   834  			for x := 0; x < 10 && gp.atomicstatus != oldval; x++ {
   835  				procyield(1)
   836  			}
   837  		} else {
   838  			osyield()
   839  			nextYield = nanotime() + yieldDelay/2
   840  		}
   841  	}
   842  }
   843  
   844  // casgstatus(gp, oldstatus, Gcopystack), assuming oldstatus is Gwaiting or Grunnable.
   845  // Returns old status. Cannot call casgstatus directly, because we are racing with an
   846  // async wakeup that might come in from netpoll. If we see Gwaiting from the readgstatus,
   847  // it might have become Grunnable by the time we get to the cas. If we called casgstatus,
   848  // it would loop waiting for the status to go back to Gwaiting, which it never will.
   849  //go:nosplit
   850  func casgcopystack(gp *g) uint32 {
   851  	for {
   852  		oldstatus := readgstatus(gp) &^ _Gscan
   853  		if oldstatus != _Gwaiting && oldstatus != _Grunnable {
   854  			throw("copystack: bad status, not Gwaiting or Grunnable")
   855  		}
   856  		if atomic.Cas(&gp.atomicstatus, oldstatus, _Gcopystack) {
   857  			return oldstatus
   858  		}
   859  	}
   860  }
   861  
   862  // casGToPreemptScan transitions gp from _Grunning to _Gscan|_Gpreempted.
   863  //
   864  // TODO(austin): This is the only status operation that both changes
   865  // the status and locks the _Gscan bit. Rethink this.
   866  func casGToPreemptScan(gp *g, old, new uint32) {
   867  	if old != _Grunning || new != _Gscan|_Gpreempted {
   868  		throw("bad g transition")
   869  	}
   870  	acquireLockRank(lockRankGscan)
   871  	for !atomic.Cas(&gp.atomicstatus, _Grunning, _Gscan|_Gpreempted) {
   872  	}
   873  }
   874  
   875  // casGFromPreempted attempts to transition gp from _Gpreempted to
   876  // _Gwaiting. If successful, the caller is responsible for
   877  // re-scheduling gp.
   878  func casGFromPreempted(gp *g, old, new uint32) bool {
   879  	if old != _Gpreempted || new != _Gwaiting {
   880  		throw("bad g transition")
   881  	}
   882  	return atomic.Cas(&gp.atomicstatus, _Gpreempted, _Gwaiting)
   883  }
   884  
   885  // stopTheWorld stops all P's from executing goroutines, interrupting
   886  // all goroutines at GC safe points and records reason as the reason
   887  // for the stop. On return, only the current goroutine's P is running.
   888  // stopTheWorld must not be called from a system stack and the caller
   889  // must not hold worldsema. The caller must call startTheWorld when
   890  // other P's should resume execution.
   891  //
   892  // stopTheWorld is safe for multiple goroutines to call at the
   893  // same time. Each will execute its own stop, and the stops will
   894  // be serialized.
   895  //
   896  // This is also used by routines that do stack dumps. If the system is
   897  // in panic or being exited, this may not reliably stop all
   898  // goroutines.
   899  func stopTheWorld(reason string) {
   900  	semacquire(&worldsema)
   901  	gp := getg()
   902  	gp.m.preemptoff = reason
   903  	systemstack(func() {
   904  		// Mark the goroutine which called stopTheWorld preemptible so its
   905  		// stack may be scanned.
   906  		// This lets a mark worker scan us while we try to stop the world
   907  		// since otherwise we could get in a mutual preemption deadlock.
   908  		// We must not modify anything on the G stack because a stack shrink
   909  		// may occur. A stack shrink is otherwise OK though because in order
   910  		// to return from this function (and to leave the system stack) we
   911  		// must have preempted all goroutines, including any attempting
   912  		// to scan our stack, in which case, any stack shrinking will
   913  		// have already completed by the time we exit.
   914  		casgstatus(gp, _Grunning, _Gwaiting)
   915  		stopTheWorldWithSema()
   916  		casgstatus(gp, _Gwaiting, _Grunning)
   917  	})
   918  }
   919  
   920  // startTheWorld undoes the effects of stopTheWorld.
   921  func startTheWorld() {
   922  	systemstack(func() { startTheWorldWithSema(false) })
   923  	// worldsema must be held over startTheWorldWithSema to ensure
   924  	// gomaxprocs cannot change while worldsema is held.
   925  	semrelease(&worldsema)
   926  	getg().m.preemptoff = ""
   927  }
   928  
   929  // stopTheWorldGC has the same effect as stopTheWorld, but blocks
   930  // until the GC is not running. It also blocks a GC from starting
   931  // until startTheWorldGC is called.
   932  func stopTheWorldGC(reason string) {
   933  	semacquire(&gcsema)
   934  	stopTheWorld(reason)
   935  }
   936  
   937  // startTheWorldGC undoes the effects of stopTheWorldGC.
   938  func startTheWorldGC() {
   939  	startTheWorld()
   940  	semrelease(&gcsema)
   941  }
   942  
   943  // Holding worldsema grants an M the right to try to stop the world.
   944  var worldsema uint32 = 1
   945  
   946  // Holding gcsema grants the M the right to block a GC, and blocks
   947  // until the current GC is done. In particular, it prevents gomaxprocs
   948  // from changing concurrently.
   949  //
   950  // TODO(mknyszek): Once gomaxprocs and the execution tracer can handle
   951  // being changed/enabled during a GC, remove this.
   952  var gcsema uint32 = 1
   953  
   954  // stopTheWorldWithSema is the core implementation of stopTheWorld.
   955  // The caller is responsible for acquiring worldsema and disabling
   956  // preemption first and then should stopTheWorldWithSema on the system
   957  // stack:
   958  //
   959  //	semacquire(&worldsema, 0)
   960  //	m.preemptoff = "reason"
   961  //	systemstack(stopTheWorldWithSema)
   962  //
   963  // When finished, the caller must either call startTheWorld or undo
   964  // these three operations separately:
   965  //
   966  //	m.preemptoff = ""
   967  //	systemstack(startTheWorldWithSema)
   968  //	semrelease(&worldsema)
   969  //
   970  // It is allowed to acquire worldsema once and then execute multiple
   971  // startTheWorldWithSema/stopTheWorldWithSema pairs.
   972  // Other P's are able to execute between successive calls to
   973  // startTheWorldWithSema and stopTheWorldWithSema.
   974  // Holding worldsema causes any other goroutines invoking
   975  // stopTheWorld to block.
   976  func stopTheWorldWithSema() {
   977  	_g_ := getg()
   978  
   979  	// If we hold a lock, then we won't be able to stop another M
   980  	// that is blocked trying to acquire the lock.
   981  	if _g_.m.locks > 0 {
   982  		throw("stopTheWorld: holding locks")
   983  	}
   984  
   985  	lock(&sched.lock)
   986  	sched.stopwait = gomaxprocs
   987  	atomic.Store(&sched.gcwaiting, 1)
   988  	preemptall()
   989  	// stop current P
   990  	_g_.m.p.ptr().status = _Pgcstop // Pgcstop is only diagnostic.
   991  	sched.stopwait--
   992  	// try to retake all P's in Psyscall status
   993  	for _, p := range allp {
   994  		s := p.status
   995  		if s == _Psyscall && atomic.Cas(&p.status, s, _Pgcstop) {
   996  			if trace.enabled {
   997  				traceGoSysBlock(p)
   998  				traceProcStop(p)
   999  			}
  1000  			p.syscalltick++
  1001  			sched.stopwait--
  1002  		}
  1003  	}
  1004  	// stop idle P's
  1005  	for {
  1006  		p := pidleget()
  1007  		if p == nil {
  1008  			break
  1009  		}
  1010  		p.status = _Pgcstop
  1011  		sched.stopwait--
  1012  	}
  1013  	wait := sched.stopwait > 0
  1014  	unlock(&sched.lock)
  1015  
  1016  	// wait for remaining P's to stop voluntarily
  1017  	if wait {
  1018  		for {
  1019  			// wait for 100us, then try to re-preempt in case of any races
  1020  			if notetsleep(&sched.stopnote, 100*1000) {
  1021  				noteclear(&sched.stopnote)
  1022  				break
  1023  			}
  1024  			preemptall()
  1025  		}
  1026  	}
  1027  
  1028  	// sanity checks
  1029  	bad := ""
  1030  	if sched.stopwait != 0 {
  1031  		bad = "stopTheWorld: not stopped (stopwait != 0)"
  1032  	} else {
  1033  		for _, p := range allp {
  1034  			if p.status != _Pgcstop {
  1035  				bad = "stopTheWorld: not stopped (status != _Pgcstop)"
  1036  			}
  1037  		}
  1038  	}
  1039  	if atomic.Load(&freezing) != 0 {
  1040  		// Some other thread is panicking. This can cause the
  1041  		// sanity checks above to fail if the panic happens in
  1042  		// the signal handler on a stopped thread. Either way,
  1043  		// we should halt this thread.
  1044  		lock(&deadlock)
  1045  		lock(&deadlock)
  1046  	}
  1047  	if bad != "" {
  1048  		throw(bad)
  1049  	}
  1050  }
  1051  
  1052  func startTheWorldWithSema(emitTraceEvent bool) int64 {
  1053  	mp := acquirem() // disable preemption because it can be holding p in a local var
  1054  	if netpollinited() {
  1055  		list := netpoll(0) // non-blocking
  1056  		injectglist(&list)
  1057  	}
  1058  	lock(&sched.lock)
  1059  
  1060  	procs := gomaxprocs
  1061  	if newprocs != 0 {
  1062  		procs = newprocs
  1063  		newprocs = 0
  1064  	}
  1065  	p1 := procresize(procs)
  1066  	sched.gcwaiting = 0
  1067  	if sched.sysmonwait != 0 {
  1068  		sched.sysmonwait = 0
  1069  		notewakeup(&sched.sysmonnote)
  1070  	}
  1071  	unlock(&sched.lock)
  1072  
  1073  	for p1 != nil {
  1074  		p := p1
  1075  		p1 = p1.link.ptr()
  1076  		if p.m != 0 {
  1077  			mp := p.m.ptr()
  1078  			p.m = 0
  1079  			if mp.nextp != 0 {
  1080  				throw("startTheWorld: inconsistent mp->nextp")
  1081  			}
  1082  			mp.nextp.set(p)
  1083  			notewakeup(&mp.park)
  1084  		} else {
  1085  			// Start M to run P.  Do not start another M below.
  1086  			newm(nil, p, -1)
  1087  		}
  1088  	}
  1089  
  1090  	// Capture start-the-world time before doing clean-up tasks.
  1091  	startTime := nanotime()
  1092  	if emitTraceEvent {
  1093  		traceGCSTWDone()
  1094  	}
  1095  
  1096  	// Wakeup an additional proc in case we have excessive runnable goroutines
  1097  	// in local queues or in the global queue. If we don't, the proc will park itself.
  1098  	// If we have lots of excessive work, resetspinning will unpark additional procs as necessary.
  1099  	wakep()
  1100  
  1101  	releasem(mp)
  1102  
  1103  	return startTime
  1104  }
  1105  
  1106  // mstart is the entry-point for new Ms.
  1107  //
  1108  // This must not split the stack because we may not even have stack
  1109  // bounds set up yet.
  1110  //
  1111  // May run during STW (because it doesn't have a P yet), so write
  1112  // barriers are not allowed.
  1113  //
  1114  //go:nosplit
  1115  //go:nowritebarrierrec
  1116  func mstart() {
  1117  	_g_ := getg()
  1118  
  1119  	osStack := _g_.stack.lo == 0
  1120  	if osStack {
  1121  		// Initialize stack bounds from system stack.
  1122  		// Cgo may have left stack size in stack.hi.
  1123  		// minit may update the stack bounds.
  1124  		size := _g_.stack.hi
  1125  		if size == 0 {
  1126  			size = 8192 * sys.StackGuardMultiplier
  1127  		}
  1128  		_g_.stack.hi = uintptr(noescape(unsafe.Pointer(&size)))
  1129  		_g_.stack.lo = _g_.stack.hi - size + 1024
  1130  	}
  1131  	// Initialize stack guard so that we can start calling regular
  1132  	// Go code.
  1133  	_g_.stackguard0 = _g_.stack.lo + _StackGuard
  1134  	// This is the g0, so we can also call go:systemstack
  1135  	// functions, which check stackguard1.
  1136  	_g_.stackguard1 = _g_.stackguard0
  1137  	mstart1()
  1138  
  1139  	// Exit this thread.
  1140  	switch GOOS {
  1141  	case "windows", "solaris", "illumos", "plan9", "darwin", "aix":
  1142  		// Windows, Solaris, illumos, Darwin, AIX and Plan 9 always system-allocate
  1143  		// the stack, but put it in _g_.stack before mstart,
  1144  		// so the logic above hasn't set osStack yet.
  1145  		osStack = true
  1146  	}
  1147  	mexit(osStack)
  1148  }
  1149  
  1150  func mstart1() {
  1151  	_g_ := getg()
  1152  
  1153  	if _g_ != _g_.m.g0 {
  1154  		throw("bad runtime·mstart")
  1155  	}
  1156  
  1157  	// Record the caller for use as the top of stack in mcall and
  1158  	// for terminating the thread.
  1159  	// We're never coming back to mstart1 after we call schedule,
  1160  	// so other calls can reuse the current frame.
  1161  	save(getcallerpc(), getcallersp())
  1162  	asminit()
  1163  	minit()
  1164  
  1165  	// Install signal handlers; after minit so that minit can
  1166  	// prepare the thread to be able to handle the signals.
  1167  	if _g_.m == &m0 {
  1168  		mstartm0()
  1169  	}
  1170  
  1171  	if fn := _g_.m.mstartfn; fn != nil {
  1172  		fn()
  1173  	}
  1174  
  1175  	if _g_.m != &m0 {
  1176  		acquirep(_g_.m.nextp.ptr())
  1177  		_g_.m.nextp = 0
  1178  	}
  1179  	schedule()
  1180  }
  1181  
  1182  // mstartm0 implements part of mstart1 that only runs on the m0.
  1183  //
  1184  // Write barriers are allowed here because we know the GC can't be
  1185  // running yet, so they'll be no-ops.
  1186  //
  1187  //go:yeswritebarrierrec
  1188  func mstartm0() {
  1189  	// Create an extra M for callbacks on threads not created by Go.
  1190  	// An extra M is also needed on Windows for callbacks created by
  1191  	// syscall.NewCallback. See issue #6751 for details.
  1192  	if (iscgo || GOOS == "windows") && !cgoHasExtraM {
  1193  		cgoHasExtraM = true
  1194  		newextram()
  1195  	}
  1196  	initsig(false)
  1197  }
  1198  
  1199  // mexit tears down and exits the current thread.
  1200  //
  1201  // Don't call this directly to exit the thread, since it must run at
  1202  // the top of the thread stack. Instead, use gogo(&_g_.m.g0.sched) to
  1203  // unwind the stack to the point that exits the thread.
  1204  //
  1205  // It is entered with m.p != nil, so write barriers are allowed. It
  1206  // will release the P before exiting.
  1207  //
  1208  //go:yeswritebarrierrec
  1209  func mexit(osStack bool) {
  1210  	g := getg()
  1211  	m := g.m
  1212  
  1213  	if m == &m0 {
  1214  		// This is the main thread. Just wedge it.
  1215  		//
  1216  		// On Linux, exiting the main thread puts the process
  1217  		// into a non-waitable zombie state. On Plan 9,
  1218  		// exiting the main thread unblocks wait even though
  1219  		// other threads are still running. On Solaris we can
  1220  		// neither exitThread nor return from mstart. Other
  1221  		// bad things probably happen on other platforms.
  1222  		//
  1223  		// We could try to clean up this M more before wedging
  1224  		// it, but that complicates signal handling.
  1225  		handoffp(releasep())
  1226  		lock(&sched.lock)
  1227  		sched.nmfreed++
  1228  		checkdead()
  1229  		unlock(&sched.lock)
  1230  		notesleep(&m.park)
  1231  		throw("locked m0 woke up")
  1232  	}
  1233  
  1234  	sigblock()
  1235  	unminit()
  1236  
  1237  	// Free the gsignal stack.
  1238  	if m.gsignal != nil {
  1239  		stackfree(m.gsignal.stack)
  1240  		// On some platforms, when calling into VDSO (e.g. nanotime)
  1241  		// we store our g on the gsignal stack, if there is one.
  1242  		// Now the stack is freed, unlink it from the m, so we
  1243  		// won't write to it when calling VDSO code.
  1244  		m.gsignal = nil
  1245  	}
  1246  
  1247  	// Remove m from allm.
  1248  	lock(&sched.lock)
  1249  	for pprev := &allm; *pprev != nil; pprev = &(*pprev).alllink {
  1250  		if *pprev == m {
  1251  			*pprev = m.alllink
  1252  			goto found
  1253  		}
  1254  	}
  1255  	throw("m not found in allm")
  1256  found:
  1257  	if !osStack {
  1258  		// Delay reaping m until it's done with the stack.
  1259  		//
  1260  		// If this is using an OS stack, the OS will free it
  1261  		// so there's no need for reaping.
  1262  		atomic.Store(&m.freeWait, 1)
  1263  		// Put m on the free list, though it will not be reaped until
  1264  		// freeWait is 0. Note that the free list must not be linked
  1265  		// through alllink because some functions walk allm without
  1266  		// locking, so may be using alllink.
  1267  		m.freelink = sched.freem
  1268  		sched.freem = m
  1269  	}
  1270  	unlock(&sched.lock)
  1271  
  1272  	// Release the P.
  1273  	handoffp(releasep())
  1274  	// After this point we must not have write barriers.
  1275  
  1276  	// Invoke the deadlock detector. This must happen after
  1277  	// handoffp because it may have started a new M to take our
  1278  	// P's work.
  1279  	lock(&sched.lock)
  1280  	sched.nmfreed++
  1281  	checkdead()
  1282  	unlock(&sched.lock)
  1283  
  1284  	if GOOS == "darwin" {
  1285  		// Make sure pendingPreemptSignals is correct when an M exits.
  1286  		// For #41702.
  1287  		if atomic.Load(&m.signalPending) != 0 {
  1288  			atomic.Xadd(&pendingPreemptSignals, -1)
  1289  		}
  1290  	}
  1291  
  1292  	if osStack {
  1293  		// Return from mstart and let the system thread
  1294  		// library free the g0 stack and terminate the thread.
  1295  		return
  1296  	}
  1297  
  1298  	// mstart is the thread's entry point, so there's nothing to
  1299  	// return to. Exit the thread directly. exitThread will clear
  1300  	// m.freeWait when it's done with the stack and the m can be
  1301  	// reaped.
  1302  	exitThread(&m.freeWait)
  1303  }
  1304  
  1305  // forEachP calls fn(p) for every P p when p reaches a GC safe point.
  1306  // If a P is currently executing code, this will bring the P to a GC
  1307  // safe point and execute fn on that P. If the P is not executing code
  1308  // (it is idle or in a syscall), this will call fn(p) directly while
  1309  // preventing the P from exiting its state. This does not ensure that
  1310  // fn will run on every CPU executing Go code, but it acts as a global
  1311  // memory barrier. GC uses this as a "ragged barrier."
  1312  //
  1313  // The caller must hold worldsema.
  1314  //
  1315  //go:systemstack
  1316  func forEachP(fn func(*p)) {
  1317  	mp := acquirem()
  1318  	_p_ := getg().m.p.ptr()
  1319  
  1320  	lock(&sched.lock)
  1321  	if sched.safePointWait != 0 {
  1322  		throw("forEachP: sched.safePointWait != 0")
  1323  	}
  1324  	sched.safePointWait = gomaxprocs - 1
  1325  	sched.safePointFn = fn
  1326  
  1327  	// Ask all Ps to run the safe point function.
  1328  	for _, p := range allp {
  1329  		if p != _p_ {
  1330  			atomic.Store(&p.runSafePointFn, 1)
  1331  		}
  1332  	}
  1333  	preemptall()
  1334  
  1335  	// Any P entering _Pidle or _Psyscall from now on will observe
  1336  	// p.runSafePointFn == 1 and will call runSafePointFn when
  1337  	// changing its status to _Pidle/_Psyscall.
  1338  
  1339  	// Run safe point function for all idle Ps. sched.pidle will
  1340  	// not change because we hold sched.lock.
  1341  	for p := sched.pidle.ptr(); p != nil; p = p.link.ptr() {
  1342  		if atomic.Cas(&p.runSafePointFn, 1, 0) {
  1343  			fn(p)
  1344  			sched.safePointWait--
  1345  		}
  1346  	}
  1347  
  1348  	wait := sched.safePointWait > 0
  1349  	unlock(&sched.lock)
  1350  
  1351  	// Run fn for the current P.
  1352  	fn(_p_)
  1353  
  1354  	// Force Ps currently in _Psyscall into _Pidle and hand them
  1355  	// off to induce safe point function execution.
  1356  	for _, p := range allp {
  1357  		s := p.status
  1358  		if s == _Psyscall && p.runSafePointFn == 1 && atomic.Cas(&p.status, s, _Pidle) {
  1359  			if trace.enabled {
  1360  				traceGoSysBlock(p)
  1361  				traceProcStop(p)
  1362  			}
  1363  			p.syscalltick++
  1364  			handoffp(p)
  1365  		}
  1366  	}
  1367  
  1368  	// Wait for remaining Ps to run fn.
  1369  	if wait {
  1370  		for {
  1371  			// Wait for 100us, then try to re-preempt in
  1372  			// case of any races.
  1373  			//
  1374  			// Requires system stack.
  1375  			if notetsleep(&sched.safePointNote, 100*1000) {
  1376  				noteclear(&sched.safePointNote)
  1377  				break
  1378  			}
  1379  			preemptall()
  1380  		}
  1381  	}
  1382  	if sched.safePointWait != 0 {
  1383  		throw("forEachP: not done")
  1384  	}
  1385  	for _, p := range allp {
  1386  		if p.runSafePointFn != 0 {
  1387  			throw("forEachP: P did not run fn")
  1388  		}
  1389  	}
  1390  
  1391  	lock(&sched.lock)
  1392  	sched.safePointFn = nil
  1393  	unlock(&sched.lock)
  1394  	releasem(mp)
  1395  }
  1396  
  1397  // runSafePointFn runs the safe point function, if any, for this P.
  1398  // This should be called like
  1399  //
  1400  //     if getg().m.p.runSafePointFn != 0 {
  1401  //         runSafePointFn()
  1402  //     }
  1403  //
  1404  // runSafePointFn must be checked on any transition in to _Pidle or
  1405  // _Psyscall to avoid a race where forEachP sees that the P is running
  1406  // just before the P goes into _Pidle/_Psyscall and neither forEachP
  1407  // nor the P run the safe-point function.
  1408  func runSafePointFn() {
  1409  	p := getg().m.p.ptr()
  1410  	// Resolve the race between forEachP running the safe-point
  1411  	// function on this P's behalf and this P running the
  1412  	// safe-point function directly.
  1413  	if !atomic.Cas(&p.runSafePointFn, 1, 0) {
  1414  		return
  1415  	}
  1416  	sched.safePointFn(p)
  1417  	lock(&sched.lock)
  1418  	sched.safePointWait--
  1419  	if sched.safePointWait == 0 {
  1420  		notewakeup(&sched.safePointNote)
  1421  	}
  1422  	unlock(&sched.lock)
  1423  }
  1424  
  1425  // When running with cgo, we call _cgo_thread_start
  1426  // to start threads for us so that we can play nicely with
  1427  // foreign code.
  1428  var cgoThreadStart unsafe.Pointer
  1429  
  1430  type cgothreadstart struct {
  1431  	g   guintptr
  1432  	tls *uint64
  1433  	fn  unsafe.Pointer
  1434  }
  1435  
  1436  // Allocate a new m unassociated with any thread.
  1437  // Can use p for allocation context if needed.
  1438  // fn is recorded as the new m's m.mstartfn.
  1439  // id is optional pre-allocated m ID. Omit by passing -1.
  1440  //
  1441  // This function is allowed to have write barriers even if the caller
  1442  // isn't because it borrows _p_.
  1443  //
  1444  //go:yeswritebarrierrec
  1445  func allocm(_p_ *p, fn func(), id int64) *m {
  1446  	_g_ := getg()
  1447  	acquirem() // disable GC because it can be called from sysmon
  1448  	if _g_.m.p == 0 {
  1449  		acquirep(_p_) // temporarily borrow p for mallocs in this function
  1450  	}
  1451  
  1452  	// Release the free M list. We need to do this somewhere and
  1453  	// this may free up a stack we can use.
  1454  	if sched.freem != nil {
  1455  		lock(&sched.lock)
  1456  		var newList *m
  1457  		for freem := sched.freem; freem != nil; {
  1458  			if freem.freeWait != 0 {
  1459  				next := freem.freelink
  1460  				freem.freelink = newList
  1461  				newList = freem
  1462  				freem = next
  1463  				continue
  1464  			}
  1465  			stackfree(freem.g0.stack)
  1466  			freem = freem.freelink
  1467  		}
  1468  		sched.freem = newList
  1469  		unlock(&sched.lock)
  1470  	}
  1471  
  1472  	mp := new(m)
  1473  	mp.mstartfn = fn
  1474  	mcommoninit(mp, id)
  1475  
  1476  	// In case of cgo or Solaris or illumos or Darwin, pthread_create will make us a stack.
  1477  	// Windows and Plan 9 will layout sched stack on OS stack.
  1478  	if iscgo || GOOS == "solaris" || GOOS == "illumos" || GOOS == "windows" || GOOS == "plan9" || GOOS == "darwin" {
  1479  		mp.g0 = malg(-1)
  1480  	} else {
  1481  		mp.g0 = malg(8192 * sys.StackGuardMultiplier)
  1482  	}
  1483  	mp.g0.m = mp
  1484  
  1485  	if _p_ == _g_.m.p.ptr() {
  1486  		releasep()
  1487  	}
  1488  	releasem(_g_.m)
  1489  
  1490  	return mp
  1491  }
  1492  
  1493  // needm is called when a cgo callback happens on a
  1494  // thread without an m (a thread not created by Go).
  1495  // In this case, needm is expected to find an m to use
  1496  // and return with m, g initialized correctly.
  1497  // Since m and g are not set now (likely nil, but see below)
  1498  // needm is limited in what routines it can call. In particular
  1499  // it can only call nosplit functions (textflag 7) and cannot
  1500  // do any scheduling that requires an m.
  1501  //
  1502  // In order to avoid needing heavy lifting here, we adopt
  1503  // the following strategy: there is a stack of available m's
  1504  // that can be stolen. Using compare-and-swap
  1505  // to pop from the stack has ABA races, so we simulate
  1506  // a lock by doing an exchange (via Casuintptr) to steal the stack
  1507  // head and replace the top pointer with MLOCKED (1).
  1508  // This serves as a simple spin lock that we can use even
  1509  // without an m. The thread that locks the stack in this way
  1510  // unlocks the stack by storing a valid stack head pointer.
  1511  //
  1512  // In order to make sure that there is always an m structure
  1513  // available to be stolen, we maintain the invariant that there
  1514  // is always one more than needed. At the beginning of the
  1515  // program (if cgo is in use) the list is seeded with a single m.
  1516  // If needm finds that it has taken the last m off the list, its job
  1517  // is - once it has installed its own m so that it can do things like
  1518  // allocate memory - to create a spare m and put it on the list.
  1519  //
  1520  // Each of these extra m's also has a g0 and a curg that are
  1521  // pressed into service as the scheduling stack and current
  1522  // goroutine for the duration of the cgo callback.
  1523  //
  1524  // When the callback is done with the m, it calls dropm to
  1525  // put the m back on the list.
  1526  //go:nosplit
  1527  func needm(x byte) {
  1528  	if (iscgo || GOOS == "windows") && !cgoHasExtraM {
  1529  		// Can happen if C/C++ code calls Go from a global ctor.
  1530  		// Can also happen on Windows if a global ctor uses a
  1531  		// callback created by syscall.NewCallback. See issue #6751
  1532  		// for details.
  1533  		//
  1534  		// Can not throw, because scheduler is not initialized yet.
  1535  		write(2, unsafe.Pointer(&earlycgocallback[0]), int32(len(earlycgocallback)))
  1536  		exit(1)
  1537  	}
  1538  
  1539  	// Save and block signals before getting an M.
  1540  	// The signal handler may call needm itself,
  1541  	// and we must avoid a deadlock. Also, once g is installed,
  1542  	// any incoming signals will try to execute,
  1543  	// but we won't have the sigaltstack settings and other data
  1544  	// set up appropriately until the end of minit, which will
  1545  	// unblock the signals. This is the same dance as when
  1546  	// starting a new m to run Go code via newosproc.
  1547  	var sigmask sigset
  1548  	sigsave(&sigmask)
  1549  	sigblock()
  1550  
  1551  	// Lock extra list, take head, unlock popped list.
  1552  	// nilokay=false is safe here because of the invariant above,
  1553  	// that the extra list always contains or will soon contain
  1554  	// at least one m.
  1555  	mp := lockextra(false)
  1556  
  1557  	// Set needextram when we've just emptied the list,
  1558  	// so that the eventual call into cgocallbackg will
  1559  	// allocate a new m for the extra list. We delay the
  1560  	// allocation until then so that it can be done
  1561  	// after exitsyscall makes sure it is okay to be
  1562  	// running at all (that is, there's no garbage collection
  1563  	// running right now).
  1564  	mp.needextram = mp.schedlink == 0
  1565  	extraMCount--
  1566  	unlockextra(mp.schedlink.ptr())
  1567  
  1568  	// Store the original signal mask for use by minit.
  1569  	mp.sigmask = sigmask
  1570  
  1571  	// Install g (= m->g0) and set the stack bounds
  1572  	// to match the current stack. We don't actually know
  1573  	// how big the stack is, like we don't know how big any
  1574  	// scheduling stack is, but we assume there's at least 32 kB,
  1575  	// which is more than enough for us.
  1576  	setg(mp.g0)
  1577  	_g_ := getg()
  1578  	_g_.stack.hi = uintptr(noescape(unsafe.Pointer(&x))) + 1024
  1579  	_g_.stack.lo = uintptr(noescape(unsafe.Pointer(&x))) - 32*1024
  1580  	_g_.stackguard0 = _g_.stack.lo + _StackGuard
  1581  
  1582  	// Initialize this thread to use the m.
  1583  	asminit()
  1584  	minit()
  1585  
  1586  	// mp.curg is now a real goroutine.
  1587  	casgstatus(mp.curg, _Gdead, _Gsyscall)
  1588  	atomic.Xadd(&sched.ngsys, -1)
  1589  }
  1590  
  1591  var earlycgocallback = []byte("fatal error: cgo callback before cgo call\n")
  1592  
  1593  // newextram allocates m's and puts them on the extra list.
  1594  // It is called with a working local m, so that it can do things
  1595  // like call schedlock and allocate.
  1596  func newextram() {
  1597  	c := atomic.Xchg(&extraMWaiters, 0)
  1598  	if c > 0 {
  1599  		for i := uint32(0); i < c; i++ {
  1600  			oneNewExtraM()
  1601  		}
  1602  	} else {
  1603  		// Make sure there is at least one extra M.
  1604  		mp := lockextra(true)
  1605  		unlockextra(mp)
  1606  		if mp == nil {
  1607  			oneNewExtraM()
  1608  		}
  1609  	}
  1610  }
  1611  
  1612  // oneNewExtraM allocates an m and puts it on the extra list.
  1613  func oneNewExtraM() {
  1614  	// Create extra goroutine locked to extra m.
  1615  	// The goroutine is the context in which the cgo callback will run.
  1616  	// The sched.pc will never be returned to, but setting it to
  1617  	// goexit makes clear to the traceback routines where
  1618  	// the goroutine stack ends.
  1619  	mp := allocm(nil, nil, -1)
  1620  	gp := malg(4096)
  1621  	gp.sched.pc = funcPC(goexit) + sys.PCQuantum
  1622  	gp.sched.sp = gp.stack.hi
  1623  	gp.sched.sp -= 4 * sys.RegSize // extra space in case of reads slightly beyond frame
  1624  	gp.sched.lr = 0
  1625  	gp.sched.g = guintptr(unsafe.Pointer(gp))
  1626  	gp.syscallpc = gp.sched.pc
  1627  	gp.syscallsp = gp.sched.sp
  1628  	gp.stktopsp = gp.sched.sp
  1629  	// malg returns status as _Gidle. Change to _Gdead before
  1630  	// adding to allg where GC can see it. We use _Gdead to hide
  1631  	// this from tracebacks and stack scans since it isn't a
  1632  	// "real" goroutine until needm grabs it.
  1633  	casgstatus(gp, _Gidle, _Gdead)
  1634  	gp.m = mp
  1635  	mp.curg = gp
  1636  	mp.lockedInt++
  1637  	mp.lockedg.set(gp)
  1638  	gp.lockedm.set(mp)
  1639  	gp.goid = int64(atomic.Xadd64(&sched.goidgen, 1))
  1640  	if raceenabled {
  1641  		gp.racectx = racegostart(funcPC(newextram) + sys.PCQuantum)
  1642  	}
  1643  	// put on allg for garbage collector
  1644  	allgadd(gp)
  1645  
  1646  	// gp is now on the allg list, but we don't want it to be
  1647  	// counted by gcount. It would be more "proper" to increment
  1648  	// sched.ngfree, but that requires locking. Incrementing ngsys
  1649  	// has the same effect.
  1650  	atomic.Xadd(&sched.ngsys, +1)
  1651  
  1652  	// Add m to the extra list.
  1653  	mnext := lockextra(true)
  1654  	mp.schedlink.set(mnext)
  1655  	extraMCount++
  1656  	unlockextra(mp)
  1657  }
  1658  
  1659  // dropm is called when a cgo callback has called needm but is now
  1660  // done with the callback and returning back into the non-Go thread.
  1661  // It puts the current m back onto the extra list.
  1662  //
  1663  // The main expense here is the call to signalstack to release the
  1664  // m's signal stack, and then the call to needm on the next callback
  1665  // from this thread. It is tempting to try to save the m for next time,
  1666  // which would eliminate both these costs, but there might not be
  1667  // a next time: the current thread (which Go does not control) might exit.
  1668  // If we saved the m for that thread, there would be an m leak each time
  1669  // such a thread exited. Instead, we acquire and release an m on each
  1670  // call. These should typically not be scheduling operations, just a few
  1671  // atomics, so the cost should be small.
  1672  //
  1673  // TODO(rsc): An alternative would be to allocate a dummy pthread per-thread
  1674  // variable using pthread_key_create. Unlike the pthread keys we already use
  1675  // on OS X, this dummy key would never be read by Go code. It would exist
  1676  // only so that we could register at thread-exit-time destructor.
  1677  // That destructor would put the m back onto the extra list.
  1678  // This is purely a performance optimization. The current version,
  1679  // in which dropm happens on each cgo call, is still correct too.
  1680  // We may have to keep the current version on systems with cgo
  1681  // but without pthreads, like Windows.
  1682  func dropm() {
  1683  	// Clear m and g, and return m to the extra list.
  1684  	// After the call to setg we can only call nosplit functions
  1685  	// with no pointer manipulation.
  1686  	mp := getg().m
  1687  
  1688  	// Return mp.curg to dead state.
  1689  	casgstatus(mp.curg, _Gsyscall, _Gdead)
  1690  	mp.curg.preemptStop = false
  1691  	atomic.Xadd(&sched.ngsys, +1)
  1692  
  1693  	// Block signals before unminit.
  1694  	// Unminit unregisters the signal handling stack (but needs g on some systems).
  1695  	// Setg(nil) clears g, which is the signal handler's cue not to run Go handlers.
  1696  	// It's important not to try to handle a signal between those two steps.
  1697  	sigmask := mp.sigmask
  1698  	sigblock()
  1699  	unminit()
  1700  
  1701  	mnext := lockextra(true)
  1702  	extraMCount++
  1703  	mp.schedlink.set(mnext)
  1704  
  1705  	setg(nil)
  1706  
  1707  	// Commit the release of mp.
  1708  	unlockextra(mp)
  1709  
  1710  	msigrestore(sigmask)
  1711  }
  1712  
  1713  // A helper function for EnsureDropM.
  1714  func getm() uintptr {
  1715  	return uintptr(unsafe.Pointer(getg().m))
  1716  }
  1717  
  1718  var extram uintptr
  1719  var extraMCount uint32 // Protected by lockextra
  1720  var extraMWaiters uint32
  1721  
  1722  // lockextra locks the extra list and returns the list head.
  1723  // The caller must unlock the list by storing a new list head
  1724  // to extram. If nilokay is true, then lockextra will
  1725  // return a nil list head if that's what it finds. If nilokay is false,
  1726  // lockextra will keep waiting until the list head is no longer nil.
  1727  //go:nosplit
  1728  func lockextra(nilokay bool) *m {
  1729  	const locked = 1
  1730  
  1731  	incr := false
  1732  	for {
  1733  		old := atomic.Loaduintptr(&extram)
  1734  		if old == locked {
  1735  			osyield()
  1736  			continue
  1737  		}
  1738  		if old == 0 && !nilokay {
  1739  			if !incr {
  1740  				// Add 1 to the number of threads
  1741  				// waiting for an M.
  1742  				// This is cleared by newextram.
  1743  				atomic.Xadd(&extraMWaiters, 1)
  1744  				incr = true
  1745  			}
  1746  			usleep(1)
  1747  			continue
  1748  		}
  1749  		if atomic.Casuintptr(&extram, old, locked) {
  1750  			return (*m)(unsafe.Pointer(old))
  1751  		}
  1752  		osyield()
  1753  		continue
  1754  	}
  1755  }
  1756  
  1757  //go:nosplit
  1758  func unlockextra(mp *m) {
  1759  	atomic.Storeuintptr(&extram, uintptr(unsafe.Pointer(mp)))
  1760  }
  1761  
  1762  // execLock serializes exec and clone to avoid bugs or unspecified behaviour
  1763  // around exec'ing while creating/destroying threads.  See issue #19546.
  1764  var execLock rwmutex
  1765  
  1766  // newmHandoff contains a list of m structures that need new OS threads.
  1767  // This is used by newm in situations where newm itself can't safely
  1768  // start an OS thread.
  1769  var newmHandoff struct {
  1770  	lock mutex
  1771  
  1772  	// newm points to a list of M structures that need new OS
  1773  	// threads. The list is linked through m.schedlink.
  1774  	newm muintptr
  1775  
  1776  	// waiting indicates that wake needs to be notified when an m
  1777  	// is put on the list.
  1778  	waiting bool
  1779  	wake    note
  1780  
  1781  	// haveTemplateThread indicates that the templateThread has
  1782  	// been started. This is not protected by lock. Use cas to set
  1783  	// to 1.
  1784  	haveTemplateThread uint32
  1785  }
  1786  
  1787  // Create a new m. It will start off with a call to fn, or else the scheduler.
  1788  // fn needs to be static and not a heap allocated closure.
  1789  // May run with m.p==nil, so write barriers are not allowed.
  1790  //
  1791  // id is optional pre-allocated m ID. Omit by passing -1.
  1792  //go:nowritebarrierrec
  1793  func newm(fn func(), _p_ *p, id int64) {
  1794  	mp := allocm(_p_, fn, id)
  1795  	mp.nextp.set(_p_)
  1796  	mp.sigmask = initSigmask
  1797  	if gp := getg(); gp != nil && gp.m != nil && (gp.m.lockedExt != 0 || gp.m.incgo) && GOOS != "plan9" {
  1798  		// We're on a locked M or a thread that may have been
  1799  		// started by C. The kernel state of this thread may
  1800  		// be strange (the user may have locked it for that
  1801  		// purpose). We don't want to clone that into another
  1802  		// thread. Instead, ask a known-good thread to create
  1803  		// the thread for us.
  1804  		//
  1805  		// This is disabled on Plan 9. See golang.org/issue/22227.
  1806  		//
  1807  		// TODO: This may be unnecessary on Windows, which
  1808  		// doesn't model thread creation off fork.
  1809  		lock(&newmHandoff.lock)
  1810  		if newmHandoff.haveTemplateThread == 0 {
  1811  			throw("on a locked thread with no template thread")
  1812  		}
  1813  		mp.schedlink = newmHandoff.newm
  1814  		newmHandoff.newm.set(mp)
  1815  		if newmHandoff.waiting {
  1816  			newmHandoff.waiting = false
  1817  			notewakeup(&newmHandoff.wake)
  1818  		}
  1819  		unlock(&newmHandoff.lock)
  1820  		return
  1821  	}
  1822  	newm1(mp)
  1823  }
  1824  
  1825  func newm1(mp *m) {
  1826  	if iscgo {
  1827  		var ts cgothreadstart
  1828  		if _cgo_thread_start == nil {
  1829  			throw("_cgo_thread_start missing")
  1830  		}
  1831  		ts.g.set(mp.g0)
  1832  		ts.tls = (*uint64)(unsafe.Pointer(&mp.tls[0]))
  1833  		ts.fn = unsafe.Pointer(funcPC(mstart))
  1834  		if msanenabled {
  1835  			msanwrite(unsafe.Pointer(&ts), unsafe.Sizeof(ts))
  1836  		}
  1837  		execLock.rlock() // Prevent process clone.
  1838  		asmcgocall(_cgo_thread_start, unsafe.Pointer(&ts))
  1839  		execLock.runlock()
  1840  		return
  1841  	}
  1842  	execLock.rlock() // Prevent process clone.
  1843  	newosproc(mp)
  1844  	execLock.runlock()
  1845  }
  1846  
  1847  // startTemplateThread starts the template thread if it is not already
  1848  // running.
  1849  //
  1850  // The calling thread must itself be in a known-good state.
  1851  func startTemplateThread() {
  1852  	if GOARCH == "wasm" { // no threads on wasm yet
  1853  		return
  1854  	}
  1855  
  1856  	// Disable preemption to guarantee that the template thread will be
  1857  	// created before a park once haveTemplateThread is set.
  1858  	mp := acquirem()
  1859  	if !atomic.Cas(&newmHandoff.haveTemplateThread, 0, 1) {
  1860  		releasem(mp)
  1861  		return
  1862  	}
  1863  	newm(templateThread, nil, -1)
  1864  	releasem(mp)
  1865  }
  1866  
  1867  // templateThread is a thread in a known-good state that exists solely
  1868  // to start new threads in known-good states when the calling thread
  1869  // may not be in a good state.
  1870  //
  1871  // Many programs never need this, so templateThread is started lazily
  1872  // when we first enter a state that might lead to running on a thread
  1873  // in an unknown state.
  1874  //
  1875  // templateThread runs on an M without a P, so it must not have write
  1876  // barriers.
  1877  //
  1878  //go:nowritebarrierrec
  1879  func templateThread() {
  1880  	lock(&sched.lock)
  1881  	sched.nmsys++
  1882  	checkdead()
  1883  	unlock(&sched.lock)
  1884  
  1885  	for {
  1886  		lock(&newmHandoff.lock)
  1887  		for newmHandoff.newm != 0 {
  1888  			newm := newmHandoff.newm.ptr()
  1889  			newmHandoff.newm = 0
  1890  			unlock(&newmHandoff.lock)
  1891  			for newm != nil {
  1892  				next := newm.schedlink.ptr()
  1893  				newm.schedlink = 0
  1894  				newm1(newm)
  1895  				newm = next
  1896  			}
  1897  			lock(&newmHandoff.lock)
  1898  		}
  1899  		newmHandoff.waiting = true
  1900  		noteclear(&newmHandoff.wake)
  1901  		unlock(&newmHandoff.lock)
  1902  		notesleep(&newmHandoff.wake)
  1903  	}
  1904  }
  1905  
  1906  // Stops execution of the current m until new work is available.
  1907  // Returns with acquired P.
  1908  func stopm() {
  1909  	_g_ := getg()
  1910  
  1911  	if _g_.m.locks != 0 {
  1912  		throw("stopm holding locks")
  1913  	}
  1914  	if _g_.m.p != 0 {
  1915  		throw("stopm holding p")
  1916  	}
  1917  	if _g_.m.spinning {
  1918  		throw("stopm spinning")
  1919  	}
  1920  
  1921  	lock(&sched.lock)
  1922  	mput(_g_.m)
  1923  	unlock(&sched.lock)
  1924  	notesleep(&_g_.m.park)
  1925  	noteclear(&_g_.m.park)
  1926  	acquirep(_g_.m.nextp.ptr())
  1927  	_g_.m.nextp = 0
  1928  }
  1929  
  1930  func mspinning() {
  1931  	// startm's caller incremented nmspinning. Set the new M's spinning.
  1932  	getg().m.spinning = true
  1933  }
  1934  
  1935  // Schedules some M to run the p (creates an M if necessary).
  1936  // If p==nil, tries to get an idle P, if no idle P's does nothing.
  1937  // May run with m.p==nil, so write barriers are not allowed.
  1938  // If spinning is set, the caller has incremented nmspinning and startm will
  1939  // either decrement nmspinning or set m.spinning in the newly started M.
  1940  //go:nowritebarrierrec
  1941  func startm(_p_ *p, spinning bool) {
  1942  	lock(&sched.lock)
  1943  	if _p_ == nil {
  1944  		_p_ = pidleget()
  1945  		if _p_ == nil {
  1946  			unlock(&sched.lock)
  1947  			if spinning {
  1948  				// The caller incremented nmspinning, but there are no idle Ps,
  1949  				// so it's okay to just undo the increment and give up.
  1950  				if int32(atomic.Xadd(&sched.nmspinning, -1)) < 0 {
  1951  					throw("startm: negative nmspinning")
  1952  				}
  1953  			}
  1954  			return
  1955  		}
  1956  	}
  1957  	mp := mget()
  1958  	if mp == nil {
  1959  		// No M is available, we must drop sched.lock and call newm.
  1960  		// However, we already own a P to assign to the M.
  1961  		//
  1962  		// Once sched.lock is released, another G (e.g., in a syscall),
  1963  		// could find no idle P while checkdead finds a runnable G but
  1964  		// no running M's because this new M hasn't started yet, thus
  1965  		// throwing in an apparent deadlock.
  1966  		//
  1967  		// Avoid this situation by pre-allocating the ID for the new M,
  1968  		// thus marking it as 'running' before we drop sched.lock. This
  1969  		// new M will eventually run the scheduler to execute any
  1970  		// queued G's.
  1971  		id := mReserveID()
  1972  		unlock(&sched.lock)
  1973  
  1974  		var fn func()
  1975  		if spinning {
  1976  			// The caller incremented nmspinning, so set m.spinning in the new M.
  1977  			fn = mspinning
  1978  		}
  1979  		newm(fn, _p_, id)
  1980  		return
  1981  	}
  1982  	unlock(&sched.lock)
  1983  	if mp.spinning {
  1984  		throw("startm: m is spinning")
  1985  	}
  1986  	if mp.nextp != 0 {
  1987  		throw("startm: m has p")
  1988  	}
  1989  	if spinning && !runqempty(_p_) {
  1990  		throw("startm: p has runnable gs")
  1991  	}
  1992  	// The caller incremented nmspinning, so set m.spinning in the new M.
  1993  	mp.spinning = spinning
  1994  	mp.nextp.set(_p_)
  1995  	notewakeup(&mp.park)
  1996  }
  1997  
  1998  // Hands off P from syscall or locked M.
  1999  // Always runs without a P, so write barriers are not allowed.
  2000  //go:nowritebarrierrec
  2001  func handoffp(_p_ *p) {
  2002  	// handoffp must start an M in any situation where
  2003  	// findrunnable would return a G to run on _p_.
  2004  
  2005  	// if it has local work, start it straight away
  2006  	if !runqempty(_p_) || sched.runqsize != 0 {
  2007  		startm(_p_, false)
  2008  		return
  2009  	}
  2010  	// if it has GC work, start it straight away
  2011  	if gcBlackenEnabled != 0 && gcMarkWorkAvailable(_p_) {
  2012  		startm(_p_, false)
  2013  		return
  2014  	}
  2015  	// no local work, check that there are no spinning/idle M's,
  2016  	// otherwise our help is not required
  2017  	if atomic.Load(&sched.nmspinning)+atomic.Load(&sched.npidle) == 0 && atomic.Cas(&sched.nmspinning, 0, 1) { // TODO: fast atomic
  2018  		startm(_p_, true)
  2019  		return
  2020  	}
  2021  	lock(&sched.lock)
  2022  	if sched.gcwaiting != 0 {
  2023  		_p_.status = _Pgcstop
  2024  		sched.stopwait--
  2025  		if sched.stopwait == 0 {
  2026  			notewakeup(&sched.stopnote)
  2027  		}
  2028  		unlock(&sched.lock)
  2029  		return
  2030  	}
  2031  	if _p_.runSafePointFn != 0 && atomic.Cas(&_p_.runSafePointFn, 1, 0) {
  2032  		sched.safePointFn(_p_)
  2033  		sched.safePointWait--
  2034  		if sched.safePointWait == 0 {
  2035  			notewakeup(&sched.safePointNote)
  2036  		}
  2037  	}
  2038  	if sched.runqsize != 0 {
  2039  		unlock(&sched.lock)
  2040  		startm(_p_, false)
  2041  		return
  2042  	}
  2043  	// If this is the last running P and nobody is polling network,
  2044  	// need to wakeup another M to poll network.
  2045  	if sched.npidle == uint32(gomaxprocs-1) && atomic.Load64(&sched.lastpoll) != 0 {
  2046  		unlock(&sched.lock)
  2047  		startm(_p_, false)
  2048  		return
  2049  	}
  2050  	if when := nobarrierWakeTime(_p_); when != 0 {
  2051  		wakeNetPoller(when)
  2052  	}
  2053  	pidleput(_p_)
  2054  	unlock(&sched.lock)
  2055  }
  2056  
  2057  // Tries to add one more P to execute G's.
  2058  // Called when a G is made runnable (newproc, ready).
  2059  func wakep() {
  2060  	if atomic.Load(&sched.npidle) == 0 {
  2061  		return
  2062  	}
  2063  	// be conservative about spinning threads
  2064  	if atomic.Load(&sched.nmspinning) != 0 || !atomic.Cas(&sched.nmspinning, 0, 1) {
  2065  		return
  2066  	}
  2067  	startm(nil, true)
  2068  }
  2069  
  2070  // Stops execution of the current m that is locked to a g until the g is runnable again.
  2071  // Returns with acquired P.
  2072  func stoplockedm() {
  2073  	_g_ := getg()
  2074  
  2075  	if _g_.m.lockedg == 0 || _g_.m.lockedg.ptr().lockedm.ptr() != _g_.m {
  2076  		throw("stoplockedm: inconsistent locking")
  2077  	}
  2078  	if _g_.m.p != 0 {
  2079  		// Schedule another M to run this p.
  2080  		_p_ := releasep()
  2081  		handoffp(_p_)
  2082  	}
  2083  	incidlelocked(1)
  2084  	// Wait until another thread schedules lockedg again.
  2085  	notesleep(&_g_.m.park)
  2086  	noteclear(&_g_.m.park)
  2087  	status := readgstatus(_g_.m.lockedg.ptr())
  2088  	if status&^_Gscan != _Grunnable {
  2089  		print("runtime:stoplockedm: g is not Grunnable or Gscanrunnable\n")
  2090  		dumpgstatus(_g_)
  2091  		throw("stoplockedm: not runnable")
  2092  	}
  2093  	acquirep(_g_.m.nextp.ptr())
  2094  	_g_.m.nextp = 0
  2095  }
  2096  
  2097  // Schedules the locked m to run the locked gp.
  2098  // May run during STW, so write barriers are not allowed.
  2099  //go:nowritebarrierrec
  2100  func startlockedm(gp *g) {
  2101  	_g_ := getg()
  2102  
  2103  	mp := gp.lockedm.ptr()
  2104  	if mp == _g_.m {
  2105  		throw("startlockedm: locked to me")
  2106  	}
  2107  	if mp.nextp != 0 {
  2108  		throw("startlockedm: m has p")
  2109  	}
  2110  	// directly handoff current P to the locked m
  2111  	incidlelocked(-1)
  2112  	_p_ := releasep()
  2113  	mp.nextp.set(_p_)
  2114  	notewakeup(&mp.park)
  2115  	stopm()
  2116  }
  2117  
  2118  // Stops the current m for stopTheWorld.
  2119  // Returns when the world is restarted.
  2120  func gcstopm() {
  2121  	_g_ := getg()
  2122  
  2123  	if sched.gcwaiting == 0 {
  2124  		throw("gcstopm: not waiting for gc")
  2125  	}
  2126  	if _g_.m.spinning {
  2127  		_g_.m.spinning = false
  2128  		// OK to just drop nmspinning here,
  2129  		// startTheWorld will unpark threads as necessary.
  2130  		if int32(atomic.Xadd(&sched.nmspinning, -1)) < 0 {
  2131  			throw("gcstopm: negative nmspinning")
  2132  		}
  2133  	}
  2134  	_p_ := releasep()
  2135  	lock(&sched.lock)
  2136  	_p_.status = _Pgcstop
  2137  	sched.stopwait--
  2138  	if sched.stopwait == 0 {
  2139  		notewakeup(&sched.stopnote)
  2140  	}
  2141  	unlock(&sched.lock)
  2142  	stopm()
  2143  }
  2144  
  2145  // Schedules gp to run on the current M.
  2146  // If inheritTime is true, gp inherits the remaining time in the
  2147  // current time slice. Otherwise, it starts a new time slice.
  2148  // Never returns.
  2149  //
  2150  // Write barriers are allowed because this is called immediately after
  2151  // acquiring a P in several places.
  2152  //
  2153  //go:yeswritebarrierrec
  2154  func execute(gp *g, inheritTime bool) {
  2155  	_g_ := getg()
  2156  
  2157  	// Assign gp.m before entering _Grunning so running Gs have an
  2158  	// M.
  2159  	_g_.m.curg = gp
  2160  	gp.m = _g_.m
  2161  	casgstatus(gp, _Grunnable, _Grunning)
  2162  	gp.waitsince = 0
  2163  	gp.preempt = false
  2164  	gp.stackguard0 = gp.stack.lo + _StackGuard
  2165  	if !inheritTime {
  2166  		_g_.m.p.ptr().schedtick++
  2167  	}
  2168  
  2169  	// Check whether the profiler needs to be turned on or off.
  2170  	hz := sched.profilehz
  2171  	if _g_.m.profilehz != hz {
  2172  		setThreadCPUProfiler(hz)
  2173  	}
  2174  
  2175  	if trace.enabled {
  2176  		// GoSysExit has to happen when we have a P, but before GoStart.
  2177  		// So we emit it here.
  2178  		if gp.syscallsp != 0 && gp.sysblocktraced {
  2179  			traceGoSysExit(gp.sysexitticks)
  2180  		}
  2181  		traceGoStart()
  2182  	}
  2183  
  2184  	gogo(&gp.sched)
  2185  }
  2186  
  2187  // Finds a runnable goroutine to execute.
  2188  // Tries to steal from other P's, get g from local or global queue, poll network.
  2189  func findrunnable() (gp *g, inheritTime bool) {
  2190  	_g_ := getg()
  2191  
  2192  	// The conditions here and in handoffp must agree: if
  2193  	// findrunnable would return a G to run, handoffp must start
  2194  	// an M.
  2195  
  2196  top:
  2197  	_p_ := _g_.m.p.ptr()
  2198  	if sched.gcwaiting != 0 {
  2199  		gcstopm()
  2200  		goto top
  2201  	}
  2202  	if _p_.runSafePointFn != 0 {
  2203  		runSafePointFn()
  2204  	}
  2205  
  2206  	now, pollUntil, _ := checkTimers(_p_, 0)
  2207  
  2208  	if fingwait && fingwake {
  2209  		if gp := wakefing(); gp != nil {
  2210  			ready(gp, 0, true)
  2211  		}
  2212  	}
  2213  	if *cgo_yield != nil {
  2214  		asmcgocall(*cgo_yield, nil)
  2215  	}
  2216  
  2217  	// local runq
  2218  	if gp, inheritTime := runqget(_p_); gp != nil {
  2219  		return gp, inheritTime
  2220  	}
  2221  
  2222  	// global runq
  2223  	if sched.runqsize != 0 {
  2224  		lock(&sched.lock)
  2225  		gp := globrunqget(_p_, 0)
  2226  		unlock(&sched.lock)
  2227  		if gp != nil {
  2228  			return gp, false
  2229  		}
  2230  	}
  2231  
  2232  	// Poll network.
  2233  	// This netpoll is only an optimization before we resort to stealing.
  2234  	// We can safely skip it if there are no waiters or a thread is blocked
  2235  	// in netpoll already. If there is any kind of logical race with that
  2236  	// blocked thread (e.g. it has already returned from netpoll, but does
  2237  	// not set lastpoll yet), this thread will do blocking netpoll below
  2238  	// anyway.
  2239  	if netpollinited() && atomic.Load(&netpollWaiters) > 0 && atomic.Load64(&sched.lastpoll) != 0 {
  2240  		if list := netpoll(0); !list.empty() { // non-blocking
  2241  			gp := list.pop()
  2242  			injectglist(&list)
  2243  			casgstatus(gp, _Gwaiting, _Grunnable)
  2244  			if trace.enabled {
  2245  				traceGoUnpark(gp, 0)
  2246  			}
  2247  			return gp, false
  2248  		}
  2249  	}
  2250  
  2251  	// Steal work from other P's.
  2252  	procs := uint32(gomaxprocs)
  2253  	ranTimer := false
  2254  	// If number of spinning M's >= number of busy P's, block.
  2255  	// This is necessary to prevent excessive CPU consumption
  2256  	// when GOMAXPROCS>>1 but the program parallelism is low.
  2257  	if !_g_.m.spinning && 2*atomic.Load(&sched.nmspinning) >= procs-atomic.Load(&sched.npidle) {
  2258  		goto stop
  2259  	}
  2260  	if !_g_.m.spinning {
  2261  		_g_.m.spinning = true
  2262  		atomic.Xadd(&sched.nmspinning, 1)
  2263  	}
  2264  	for i := 0; i < 4; i++ {
  2265  		for enum := stealOrder.start(fastrand()); !enum.done(); enum.next() {
  2266  			if sched.gcwaiting != 0 {
  2267  				goto top
  2268  			}
  2269  			stealRunNextG := i > 2 // first look for ready queues with more than 1 g
  2270  			p2 := allp[enum.position()]
  2271  			if _p_ == p2 {
  2272  				continue
  2273  			}
  2274  			if gp := runqsteal(_p_, p2, stealRunNextG); gp != nil {
  2275  				return gp, false
  2276  			}
  2277  
  2278  			// Consider stealing timers from p2.
  2279  			// This call to checkTimers is the only place where
  2280  			// we hold a lock on a different P's timers.
  2281  			// Lock contention can be a problem here, so
  2282  			// initially avoid grabbing the lock if p2 is running
  2283  			// and is not marked for preemption. If p2 is running
  2284  			// and not being preempted we assume it will handle its
  2285  			// own timers.
  2286  			// If we're still looking for work after checking all
  2287  			// the P's, then go ahead and steal from an active P.
  2288  			if i > 2 || (i > 1 && shouldStealTimers(p2)) {
  2289  				tnow, w, ran := checkTimers(p2, now)
  2290  				now = tnow
  2291  				if w != 0 && (pollUntil == 0 || w < pollUntil) {
  2292  					pollUntil = w
  2293  				}
  2294  				if ran {
  2295  					// Running the timers may have
  2296  					// made an arbitrary number of G's
  2297  					// ready and added them to this P's
  2298  					// local run queue. That invalidates
  2299  					// the assumption of runqsteal
  2300  					// that is always has room to add
  2301  					// stolen G's. So check now if there
  2302  					// is a local G to run.
  2303  					if gp, inheritTime := runqget(_p_); gp != nil {
  2304  						return gp, inheritTime
  2305  					}
  2306  					ranTimer = true
  2307  				}
  2308  			}
  2309  		}
  2310  	}
  2311  	if ranTimer {
  2312  		// Running a timer may have made some goroutine ready.
  2313  		goto top
  2314  	}
  2315  
  2316  stop:
  2317  
  2318  	// We have nothing to do. If we're in the GC mark phase, can
  2319  	// safely scan and blacken objects, and have work to do, run
  2320  	// idle-time marking rather than give up the P.
  2321  	if gcBlackenEnabled != 0 && _p_.gcBgMarkWorker != 0 && gcMarkWorkAvailable(_p_) {
  2322  		_p_.gcMarkWorkerMode = gcMarkWorkerIdleMode
  2323  		gp := _p_.gcBgMarkWorker.ptr()
  2324  		casgstatus(gp, _Gwaiting, _Grunnable)
  2325  		if trace.enabled {
  2326  			traceGoUnpark(gp, 0)
  2327  		}
  2328  		return gp, false
  2329  	}
  2330  
  2331  	delta := int64(-1)
  2332  	if pollUntil != 0 {
  2333  		// checkTimers ensures that polluntil > now.
  2334  		delta = pollUntil - now
  2335  	}
  2336  
  2337  	// wasm only:
  2338  	// If a callback returned and no other goroutine is awake,
  2339  	// then wake event handler goroutine which pauses execution
  2340  	// until a callback was triggered.
  2341  	gp, otherReady := beforeIdle(delta)
  2342  	if gp != nil {
  2343  		casgstatus(gp, _Gwaiting, _Grunnable)
  2344  		if trace.enabled {
  2345  			traceGoUnpark(gp, 0)
  2346  		}
  2347  		return gp, false
  2348  	}
  2349  	if otherReady {
  2350  		goto top
  2351  	}
  2352  
  2353  	// Before we drop our P, make a snapshot of the allp slice,
  2354  	// which can change underfoot once we no longer block
  2355  	// safe-points. We don't need to snapshot the contents because
  2356  	// everything up to cap(allp) is immutable.
  2357  	allpSnapshot := allp
  2358  
  2359  	// return P and block
  2360  	lock(&sched.lock)
  2361  	if sched.gcwaiting != 0 || _p_.runSafePointFn != 0 {
  2362  		unlock(&sched.lock)
  2363  		goto top
  2364  	}
  2365  	if sched.runqsize != 0 {
  2366  		gp := globrunqget(_p_, 0)
  2367  		unlock(&sched.lock)
  2368  		return gp, false
  2369  	}
  2370  	if releasep() != _p_ {
  2371  		throw("findrunnable: wrong p")
  2372  	}
  2373  	pidleput(_p_)
  2374  	unlock(&sched.lock)
  2375  
  2376  	// Delicate dance: thread transitions from spinning to non-spinning state,
  2377  	// potentially concurrently with submission of new goroutines. We must
  2378  	// drop nmspinning first and then check all per-P queues again (with
  2379  	// #StoreLoad memory barrier in between). If we do it the other way around,
  2380  	// another thread can submit a goroutine after we've checked all run queues
  2381  	// but before we drop nmspinning; as the result nobody will unpark a thread
  2382  	// to run the goroutine.
  2383  	// If we discover new work below, we need to restore m.spinning as a signal
  2384  	// for resetspinning to unpark a new worker thread (because there can be more
  2385  	// than one starving goroutine). However, if after discovering new work
  2386  	// we also observe no idle Ps, it is OK to just park the current thread:
  2387  	// the system is fully loaded so no spinning threads are required.
  2388  	// Also see "Worker thread parking/unparking" comment at the top of the file.
  2389  	wasSpinning := _g_.m.spinning
  2390  	if _g_.m.spinning {
  2391  		_g_.m.spinning = false
  2392  		if int32(atomic.Xadd(&sched.nmspinning, -1)) < 0 {
  2393  			throw("findrunnable: negative nmspinning")
  2394  		}
  2395  	}
  2396  
  2397  	// check all runqueues once again
  2398  	for _, _p_ := range allpSnapshot {
  2399  		if !runqempty(_p_) {
  2400  			lock(&sched.lock)
  2401  			_p_ = pidleget()
  2402  			unlock(&sched.lock)
  2403  			if _p_ != nil {
  2404  				acquirep(_p_)
  2405  				if wasSpinning {
  2406  					_g_.m.spinning = true
  2407  					atomic.Xadd(&sched.nmspinning, 1)
  2408  				}
  2409  				goto top
  2410  			}
  2411  			break
  2412  		}
  2413  	}
  2414  
  2415  	// Check for idle-priority GC work again.
  2416  	if gcBlackenEnabled != 0 && gcMarkWorkAvailable(nil) {
  2417  		lock(&sched.lock)
  2418  		_p_ = pidleget()
  2419  		if _p_ != nil && _p_.gcBgMarkWorker == 0 {
  2420  			pidleput(_p_)
  2421  			_p_ = nil
  2422  		}
  2423  		unlock(&sched.lock)
  2424  		if _p_ != nil {
  2425  			acquirep(_p_)
  2426  			if wasSpinning {
  2427  				_g_.m.spinning = true
  2428  				atomic.Xadd(&sched.nmspinning, 1)
  2429  			}
  2430  			// Go back to idle GC check.
  2431  			goto stop
  2432  		}
  2433  	}
  2434  
  2435  	// poll network
  2436  	if netpollinited() && (atomic.Load(&netpollWaiters) > 0 || pollUntil != 0) && atomic.Xchg64(&sched.lastpoll, 0) != 0 {
  2437  		atomic.Store64(&sched.pollUntil, uint64(pollUntil))
  2438  		if _g_.m.p != 0 {
  2439  			throw("findrunnable: netpoll with p")
  2440  		}
  2441  		if _g_.m.spinning {
  2442  			throw("findrunnable: netpoll with spinning")
  2443  		}
  2444  		if faketime != 0 {
  2445  			// When using fake time, just poll.
  2446  			delta = 0
  2447  		}
  2448  		list := netpoll(delta) // block until new work is available
  2449  		atomic.Store64(&sched.pollUntil, 0)
  2450  		atomic.Store64(&sched.lastpoll, uint64(nanotime()))
  2451  		if faketime != 0 && list.empty() {
  2452  			// Using fake time and nothing is ready; stop M.
  2453  			// When all M's stop, checkdead will call timejump.
  2454  			stopm()
  2455  			goto top
  2456  		}
  2457  		lock(&sched.lock)
  2458  		_p_ = pidleget()
  2459  		unlock(&sched.lock)
  2460  		if _p_ == nil {
  2461  			injectglist(&list)
  2462  		} else {
  2463  			acquirep(_p_)
  2464  			if !list.empty() {
  2465  				gp := list.pop()
  2466  				injectglist(&list)
  2467  				casgstatus(gp, _Gwaiting, _Grunnable)
  2468  				if trace.enabled {
  2469  					traceGoUnpark(gp, 0)
  2470  				}
  2471  				return gp, false
  2472  			}
  2473  			if wasSpinning {
  2474  				_g_.m.spinning = true
  2475  				atomic.Xadd(&sched.nmspinning, 1)
  2476  			}
  2477  			goto top
  2478  		}
  2479  	} else if pollUntil != 0 && netpollinited() {
  2480  		pollerPollUntil := int64(atomic.Load64(&sched.pollUntil))
  2481  		if pollerPollUntil == 0 || pollerPollUntil > pollUntil {
  2482  			netpollBreak()
  2483  		}
  2484  	}
  2485  	stopm()
  2486  	goto top
  2487  }
  2488  
  2489  // pollWork reports whether there is non-background work this P could
  2490  // be doing. This is a fairly lightweight check to be used for
  2491  // background work loops, like idle GC. It checks a subset of the
  2492  // conditions checked by the actual scheduler.
  2493  func pollWork() bool {
  2494  	if sched.runqsize != 0 {
  2495  		return true
  2496  	}
  2497  	p := getg().m.p.ptr()
  2498  	if !runqempty(p) {
  2499  		return true
  2500  	}
  2501  	if netpollinited() && atomic.Load(&netpollWaiters) > 0 && sched.lastpoll != 0 {
  2502  		if list := netpoll(0); !list.empty() {
  2503  			injectglist(&list)
  2504  			return true
  2505  		}
  2506  	}
  2507  	return false
  2508  }
  2509  
  2510  // wakeNetPoller wakes up the thread sleeping in the network poller,
  2511  // if there is one, and if it isn't going to wake up anyhow before
  2512  // the when argument.
  2513  func wakeNetPoller(when int64) {
  2514  	if atomic.Load64(&sched.lastpoll) == 0 {
  2515  		// In findrunnable we ensure that when polling the pollUntil
  2516  		// field is either zero or the time to which the current
  2517  		// poll is expected to run. This can have a spurious wakeup
  2518  		// but should never miss a wakeup.
  2519  		pollerPollUntil := int64(atomic.Load64(&sched.pollUntil))
  2520  		if pollerPollUntil == 0 || pollerPollUntil > when {
  2521  			netpollBreak()
  2522  		}
  2523  	}
  2524  }
  2525  
  2526  func resetspinning() {
  2527  	_g_ := getg()
  2528  	if !_g_.m.spinning {
  2529  		throw("resetspinning: not a spinning m")
  2530  	}
  2531  	_g_.m.spinning = false
  2532  	nmspinning := atomic.Xadd(&sched.nmspinning, -1)
  2533  	if int32(nmspinning) < 0 {
  2534  		throw("findrunnable: negative nmspinning")
  2535  	}
  2536  	// M wakeup policy is deliberately somewhat conservative, so check if we
  2537  	// need to wakeup another P here. See "Worker thread parking/unparking"
  2538  	// comment at the top of the file for details.
  2539  	wakep()
  2540  }
  2541  
  2542  // injectglist adds each runnable G on the list to some run queue,
  2543  // and clears glist. If there is no current P, they are added to the
  2544  // global queue, and up to npidle M's are started to run them.
  2545  // Otherwise, for each idle P, this adds a G to the global queue
  2546  // and starts an M. Any remaining G's are added to the current P's
  2547  // local run queue.
  2548  // This may temporarily acquire the scheduler lock.
  2549  // Can run concurrently with GC.
  2550  func injectglist(glist *gList) {
  2551  	if glist.empty() {
  2552  		return
  2553  	}
  2554  	if trace.enabled {
  2555  		for gp := glist.head.ptr(); gp != nil; gp = gp.schedlink.ptr() {
  2556  			traceGoUnpark(gp, 0)
  2557  		}
  2558  	}
  2559  
  2560  	// Mark all the goroutines as runnable before we put them
  2561  	// on the run queues.
  2562  	head := glist.head.ptr()
  2563  	var tail *g
  2564  	qsize := 0
  2565  	for gp := head; gp != nil; gp = gp.schedlink.ptr() {
  2566  		tail = gp
  2567  		qsize++
  2568  		casgstatus(gp, _Gwaiting, _Grunnable)
  2569  	}
  2570  
  2571  	// Turn the gList into a gQueue.
  2572  	var q gQueue
  2573  	q.head.set(head)
  2574  	q.tail.set(tail)
  2575  	*glist = gList{}
  2576  
  2577  	startIdle := func(n int) {
  2578  		for ; n != 0 && sched.npidle != 0; n-- {
  2579  			startm(nil, false)
  2580  		}
  2581  	}
  2582  
  2583  	pp := getg().m.p.ptr()
  2584  	if pp == nil {
  2585  		lock(&sched.lock)
  2586  		globrunqputbatch(&q, int32(qsize))
  2587  		unlock(&sched.lock)
  2588  		startIdle(qsize)
  2589  		return
  2590  	}
  2591  
  2592  	lock(&sched.lock)
  2593  	npidle := int(sched.npidle)
  2594  	var n int
  2595  	for n = 0; n < npidle && !q.empty(); n++ {
  2596  		globrunqput(q.pop())
  2597  	}
  2598  	unlock(&sched.lock)
  2599  	startIdle(n)
  2600  	qsize -= n
  2601  
  2602  	if !q.empty() {
  2603  		runqputbatch(pp, &q, qsize)
  2604  	}
  2605  }
  2606  
  2607  // One round of scheduler: find a runnable goroutine and execute it.
  2608  // Never returns.
  2609  func schedule() {
  2610  	_g_ := getg()
  2611  
  2612  	if _g_.m.locks != 0 {
  2613  		throw("schedule: holding locks")
  2614  	}
  2615  
  2616  	if _g_.m.lockedg != 0 {
  2617  		stoplockedm()
  2618  		execute(_g_.m.lockedg.ptr(), false) // Never returns.
  2619  	}
  2620  
  2621  	// We should not schedule away from a g that is executing a cgo call,
  2622  	// since the cgo call is using the m's g0 stack.
  2623  	if _g_.m.incgo {
  2624  		throw("schedule: in cgo")
  2625  	}
  2626  
  2627  top:
  2628  	pp := _g_.m.p.ptr()
  2629  	pp.preempt = false
  2630  
  2631  	if sched.gcwaiting != 0 {
  2632  		gcstopm()
  2633  		goto top
  2634  	}
  2635  	if pp.runSafePointFn != 0 {
  2636  		runSafePointFn()
  2637  	}
  2638  
  2639  	// Sanity check: if we are spinning, the run queue should be empty.
  2640  	// Check this before calling checkTimers, as that might call
  2641  	// goready to put a ready goroutine on the local run queue.
  2642  	if _g_.m.spinning && (pp.runnext != 0 || pp.runqhead != pp.runqtail) {
  2643  		throw("schedule: spinning with local work")
  2644  	}
  2645  
  2646  	checkTimers(pp, 0)
  2647  
  2648  	var gp *g
  2649  	var inheritTime bool
  2650  
  2651  	// Normal goroutines will check for need to wakeP in ready,
  2652  	// but GCworkers and tracereaders will not, so the check must
  2653  	// be done here instead.
  2654  	tryWakeP := false
  2655  	if trace.enabled || trace.shutdown {
  2656  		gp = traceReader()
  2657  		if gp != nil {
  2658  			casgstatus(gp, _Gwaiting, _Grunnable)
  2659  			traceGoUnpark(gp, 0)
  2660  			tryWakeP = true
  2661  		}
  2662  	}
  2663  	if gp == nil && gcBlackenEnabled != 0 {
  2664  		gp = gcController.findRunnableGCWorker(_g_.m.p.ptr())
  2665  		tryWakeP = tryWakeP || gp != nil
  2666  	}
  2667  	if gp == nil {
  2668  		// Check the global runnable queue once in a while to ensure fairness.
  2669  		// Otherwise two goroutines can completely occupy the local runqueue
  2670  		// by constantly respawning each other.
  2671  		if _g_.m.p.ptr().schedtick%61 == 0 && sched.runqsize > 0 {
  2672  			lock(&sched.lock)
  2673  			gp = globrunqget(_g_.m.p.ptr(), 1)
  2674  			unlock(&sched.lock)
  2675  		}
  2676  	}
  2677  	if gp == nil {
  2678  		gp, inheritTime = runqget(_g_.m.p.ptr())
  2679  		// We can see gp != nil here even if the M is spinning,
  2680  		// if checkTimers added a local goroutine via goready.
  2681  	}
  2682  	if gp == nil {
  2683  		gp, inheritTime = findrunnable() // blocks until work is available
  2684  	}
  2685  
  2686  	// This thread is going to run a goroutine and is not spinning anymore,
  2687  	// so if it was marked as spinning we need to reset it now and potentially
  2688  	// start a new spinning M.
  2689  	if _g_.m.spinning {
  2690  		resetspinning()
  2691  	}
  2692  
  2693  	if sched.disable.user && !schedEnabled(gp) {
  2694  		// Scheduling of this goroutine is disabled. Put it on
  2695  		// the list of pending runnable goroutines for when we
  2696  		// re-enable user scheduling and look again.
  2697  		lock(&sched.lock)
  2698  		if schedEnabled(gp) {
  2699  			// Something re-enabled scheduling while we
  2700  			// were acquiring the lock.
  2701  			unlock(&sched.lock)
  2702  		} else {
  2703  			sched.disable.runnable.pushBack(gp)
  2704  			sched.disable.n++
  2705  			unlock(&sched.lock)
  2706  			goto top
  2707  		}
  2708  	}
  2709  
  2710  	// If about to schedule a not-normal goroutine (a GCworker or tracereader),
  2711  	// wake a P if there is one.
  2712  	if tryWakeP {
  2713  		wakep()
  2714  	}
  2715  	if gp.lockedm != 0 {
  2716  		// Hands off own p to the locked m,
  2717  		// then blocks waiting for a new p.
  2718  		startlockedm(gp)
  2719  		goto top
  2720  	}
  2721  
  2722  	execute(gp, inheritTime)
  2723  }
  2724  
  2725  // dropg removes the association between m and the current goroutine m->curg (gp for short).
  2726  // Typically a caller sets gp's status away from Grunning and then
  2727  // immediately calls dropg to finish the job. The caller is also responsible
  2728  // for arranging that gp will be restarted using ready at an
  2729  // appropriate time. After calling dropg and arranging for gp to be
  2730  // readied later, the caller can do other work but eventually should
  2731  // call schedule to restart the scheduling of goroutines on this m.
  2732  func dropg() {
  2733  	_g_ := getg()
  2734  
  2735  	setMNoWB(&_g_.m.curg.m, nil)
  2736  	setGNoWB(&_g_.m.curg, nil)
  2737  }
  2738  
  2739  // checkTimers runs any timers for the P that are ready.
  2740  // If now is not 0 it is the current time.
  2741  // It returns the current time or 0 if it is not known,
  2742  // and the time when the next timer should run or 0 if there is no next timer,
  2743  // and reports whether it ran any timers.
  2744  // If the time when the next timer should run is not 0,
  2745  // it is always larger than the returned time.
  2746  // We pass now in and out to avoid extra calls of nanotime.
  2747  //go:yeswritebarrierrec
  2748  func checkTimers(pp *p, now int64) (rnow, pollUntil int64, ran bool) {
  2749  	// If there are no timers to adjust, and the first timer on
  2750  	// the heap is not yet ready to run, then there is nothing to do.
  2751  	if atomic.Load(&pp.adjustTimers) == 0 {
  2752  		next := int64(atomic.Load64(&pp.timer0When))
  2753  		if next == 0 {
  2754  			return now, 0, false
  2755  		}
  2756  		if now == 0 {
  2757  			now = nanotime()
  2758  		}
  2759  		if now < next {
  2760  			// Next timer is not ready to run.
  2761  			// But keep going if we would clear deleted timers.
  2762  			// This corresponds to the condition below where
  2763  			// we decide whether to call clearDeletedTimers.
  2764  			if pp != getg().m.p.ptr() || int(atomic.Load(&pp.deletedTimers)) <= int(atomic.Load(&pp.numTimers)/4) {
  2765  				return now, next, false
  2766  			}
  2767  		}
  2768  	}
  2769  
  2770  	lock(&pp.timersLock)
  2771  
  2772  	adjusttimers(pp)
  2773  
  2774  	rnow = now
  2775  	if len(pp.timers) > 0 {
  2776  		if rnow == 0 {
  2777  			rnow = nanotime()
  2778  		}
  2779  		for len(pp.timers) > 0 {
  2780  			// Note that runtimer may temporarily unlock
  2781  			// pp.timersLock.
  2782  			if tw := runtimer(pp, rnow); tw != 0 {
  2783  				if tw > 0 {
  2784  					pollUntil = tw
  2785  				}
  2786  				break
  2787  			}
  2788  			ran = true
  2789  		}
  2790  	}
  2791  
  2792  	// If this is the local P, and there are a lot of deleted timers,
  2793  	// clear them out. We only do this for the local P to reduce
  2794  	// lock contention on timersLock.
  2795  	if pp == getg().m.p.ptr() && int(atomic.Load(&pp.deletedTimers)) > len(pp.timers)/4 {
  2796  		clearDeletedTimers(pp)
  2797  	}
  2798  
  2799  	unlock(&pp.timersLock)
  2800  
  2801  	return rnow, pollUntil, ran
  2802  }
  2803  
  2804  // shouldStealTimers reports whether we should try stealing the timers from p2.
  2805  // We don't steal timers from a running P that is not marked for preemption,
  2806  // on the assumption that it will run its own timers. This reduces
  2807  // contention on the timers lock.
  2808  func shouldStealTimers(p2 *p) bool {
  2809  	if p2.status != _Prunning {
  2810  		return true
  2811  	}
  2812  	mp := p2.m.ptr()
  2813  	if mp == nil || mp.locks > 0 {
  2814  		return false
  2815  	}
  2816  	gp := mp.curg
  2817  	if gp == nil || gp.atomicstatus != _Grunning || !gp.preempt {
  2818  		return false
  2819  	}
  2820  	return true
  2821  }
  2822  
  2823  func parkunlock_c(gp *g, lock unsafe.Pointer) bool {
  2824  	unlock((*mutex)(lock))
  2825  	return true
  2826  }
  2827  
  2828  // park continuation on g0.
  2829  func park_m(gp *g) {
  2830  	_g_ := getg()
  2831  
  2832  	if trace.enabled {
  2833  		traceGoPark(_g_.m.waittraceev, _g_.m.waittraceskip)
  2834  	}
  2835  
  2836  	casgstatus(gp, _Grunning, _Gwaiting)
  2837  	dropg()
  2838  
  2839  	if fn := _g_.m.waitunlockf; fn != nil {
  2840  		ok := fn(gp, _g_.m.waitlock)
  2841  		_g_.m.waitunlockf = nil
  2842  		_g_.m.waitlock = nil
  2843  		if !ok {
  2844  			if trace.enabled {
  2845  				traceGoUnpark(gp, 2)
  2846  			}
  2847  			casgstatus(gp, _Gwaiting, _Grunnable)
  2848  			execute(gp, true) // Schedule it back, never returns.
  2849  		}
  2850  	}
  2851  	schedule()
  2852  }
  2853  
  2854  func goschedImpl(gp *g) {
  2855  	status := readgstatus(gp)
  2856  	if status&^_Gscan != _Grunning {
  2857  		dumpgstatus(gp)
  2858  		throw("bad g status")
  2859  	}
  2860  	casgstatus(gp, _Grunning, _Grunnable)
  2861  	dropg()
  2862  	lock(&sched.lock)
  2863  	globrunqput(gp)
  2864  	unlock(&sched.lock)
  2865  
  2866  	schedule()
  2867  }
  2868  
  2869  // Gosched continuation on g0.
  2870  func gosched_m(gp *g) {
  2871  	if trace.enabled {
  2872  		traceGoSched()
  2873  	}
  2874  	goschedImpl(gp)
  2875  }
  2876  
  2877  // goschedguarded is a forbidden-states-avoided version of gosched_m
  2878  func goschedguarded_m(gp *g) {
  2879  
  2880  	if !canPreemptM(gp.m) {
  2881  		gogo(&gp.sched) // never return
  2882  	}
  2883  
  2884  	if trace.enabled {
  2885  		traceGoSched()
  2886  	}
  2887  	goschedImpl(gp)
  2888  }
  2889  
  2890  func gopreempt_m(gp *g) {
  2891  	if trace.enabled {
  2892  		traceGoPreempt()
  2893  	}
  2894  	goschedImpl(gp)
  2895  }
  2896  
  2897  // preemptPark parks gp and puts it in _Gpreempted.
  2898  //
  2899  //go:systemstack
  2900  func preemptPark(gp *g) {
  2901  	if trace.enabled {
  2902  		traceGoPark(traceEvGoBlock, 0)
  2903  	}
  2904  	status := readgstatus(gp)
  2905  	if status&^_Gscan != _Grunning {
  2906  		dumpgstatus(gp)
  2907  		throw("bad g status")
  2908  	}
  2909  	gp.waitreason = waitReasonPreempted
  2910  	// Transition from _Grunning to _Gscan|_Gpreempted. We can't
  2911  	// be in _Grunning when we dropg because then we'd be running
  2912  	// without an M, but the moment we're in _Gpreempted,
  2913  	// something could claim this G before we've fully cleaned it
  2914  	// up. Hence, we set the scan bit to lock down further
  2915  	// transitions until we can dropg.
  2916  	casGToPreemptScan(gp, _Grunning, _Gscan|_Gpreempted)
  2917  	dropg()
  2918  	casfrom_Gscanstatus(gp, _Gscan|_Gpreempted, _Gpreempted)
  2919  	schedule()
  2920  }
  2921  
  2922  // goyield is like Gosched, but it:
  2923  // - emits a GoPreempt trace event instead of a GoSched trace event
  2924  // - puts the current G on the runq of the current P instead of the globrunq
  2925  func goyield() {
  2926  	checkTimeouts()
  2927  	mcall(goyield_m)
  2928  }
  2929  
  2930  func goyield_m(gp *g) {
  2931  	if trace.enabled {
  2932  		traceGoPreempt()
  2933  	}
  2934  	pp := gp.m.p.ptr()
  2935  	casgstatus(gp, _Grunning, _Grunnable)
  2936  	dropg()
  2937  	runqput(pp, gp, false)
  2938  	schedule()
  2939  }
  2940  
  2941  // Finishes execution of the current goroutine.
  2942  func goexit1() {
  2943  	if raceenabled {
  2944  		racegoend()
  2945  	}
  2946  	if trace.enabled {
  2947  		traceGoEnd()
  2948  	}
  2949  	mcall(goexit0)
  2950  }
  2951  
  2952  // goexit continuation on g0.
  2953  func goexit0(gp *g) {
  2954  	_g_ := getg()
  2955  
  2956  	casgstatus(gp, _Grunning, _Gdead)
  2957  	if isSystemGoroutine(gp, false) {
  2958  		atomic.Xadd(&sched.ngsys, -1)
  2959  	}
  2960  	gp.m = nil
  2961  	locked := gp.lockedm != 0
  2962  	gp.lockedm = 0
  2963  	_g_.m.lockedg = 0
  2964  	gp.preemptStop = false
  2965  	gp.paniconfault = false
  2966  	gp._defer = nil // should be true already but just in case.
  2967  	gp._panic = nil // non-nil for Goexit during panic. points at stack-allocated data.
  2968  	gp.writebuf = nil
  2969  	gp.waitreason = 0
  2970  	gp.param = nil
  2971  	gp.labels = nil
  2972  	gp.timer = nil
  2973  
  2974  	if gcBlackenEnabled != 0 && gp.gcAssistBytes > 0 {
  2975  		// Flush assist credit to the global pool. This gives
  2976  		// better information to pacing if the application is
  2977  		// rapidly creating an exiting goroutines.
  2978  		scanCredit := int64(gcController.assistWorkPerByte * float64(gp.gcAssistBytes))
  2979  		atomic.Xaddint64(&gcController.bgScanCredit, scanCredit)
  2980  		gp.gcAssistBytes = 0
  2981  	}
  2982  
  2983  	dropg()
  2984  
  2985  	if GOARCH == "wasm" { // no threads yet on wasm
  2986  		gfput(_g_.m.p.ptr(), gp)
  2987  		schedule() // never returns
  2988  	}
  2989  
  2990  	if _g_.m.lockedInt != 0 {
  2991  		print("invalid m->lockedInt = ", _g_.m.lockedInt, "\n")
  2992  		throw("internal lockOSThread error")
  2993  	}
  2994  	gfput(_g_.m.p.ptr(), gp)
  2995  	if locked {
  2996  		// The goroutine may have locked this thread because
  2997  		// it put it in an unusual kernel state. Kill it
  2998  		// rather than returning it to the thread pool.
  2999  
  3000  		// Return to mstart, which will release the P and exit
  3001  		// the thread.
  3002  		if GOOS != "plan9" { // See golang.org/issue/22227.
  3003  			gogo(&_g_.m.g0.sched)
  3004  		} else {
  3005  			// Clear lockedExt on plan9 since we may end up re-using
  3006  			// this thread.
  3007  			_g_.m.lockedExt = 0
  3008  		}
  3009  	}
  3010  	schedule()
  3011  }
  3012  
  3013  // save updates getg().sched to refer to pc and sp so that a following
  3014  // gogo will restore pc and sp.
  3015  //
  3016  // save must not have write barriers because invoking a write barrier
  3017  // can clobber getg().sched.
  3018  //
  3019  //go:nosplit
  3020  //go:nowritebarrierrec
  3021  func save(pc, sp uintptr) {
  3022  	_g_ := getg()
  3023  
  3024  	_g_.sched.pc = pc
  3025  	_g_.sched.sp = sp
  3026  	_g_.sched.lr = 0
  3027  	_g_.sched.ret = 0
  3028  	_g_.sched.g = guintptr(unsafe.Pointer(_g_))
  3029  	// We need to ensure ctxt is zero, but can't have a write
  3030  	// barrier here. However, it should always already be zero.
  3031  	// Assert that.
  3032  	if _g_.sched.ctxt != nil {
  3033  		badctxt()
  3034  	}
  3035  }
  3036  
  3037  // The goroutine g is about to enter a system call.
  3038  // Record that it's not using the cpu anymore.
  3039  // This is called only from the go syscall library and cgocall,
  3040  // not from the low-level system calls used by the runtime.
  3041  //
  3042  // Entersyscall cannot split the stack: the gosave must
  3043  // make g->sched refer to the caller's stack segment, because
  3044  // entersyscall is going to return immediately after.
  3045  //
  3046  // Nothing entersyscall calls can split the stack either.
  3047  // We cannot safely move the stack during an active call to syscall,
  3048  // because we do not know which of the uintptr arguments are
  3049  // really pointers (back into the stack).
  3050  // In practice, this means that we make the fast path run through
  3051  // entersyscall doing no-split things, and the slow path has to use systemstack
  3052  // to run bigger things on the system stack.
  3053  //
  3054  // reentersyscall is the entry point used by cgo callbacks, where explicitly
  3055  // saved SP and PC are restored. This is needed when exitsyscall will be called
  3056  // from a function further up in the call stack than the parent, as g->syscallsp
  3057  // must always point to a valid stack frame. entersyscall below is the normal
  3058  // entry point for syscalls, which obtains the SP and PC from the caller.
  3059  //
  3060  // Syscall tracing:
  3061  // At the start of a syscall we emit traceGoSysCall to capture the stack trace.
  3062  // If the syscall does not block, that is it, we do not emit any other events.
  3063  // If the syscall blocks (that is, P is retaken), retaker emits traceGoSysBlock;
  3064  // when syscall returns we emit traceGoSysExit and when the goroutine starts running
  3065  // (potentially instantly, if exitsyscallfast returns true) we emit traceGoStart.
  3066  // To ensure that traceGoSysExit is emitted strictly after traceGoSysBlock,
  3067  // we remember current value of syscalltick in m (_g_.m.syscalltick = _g_.m.p.ptr().syscalltick),
  3068  // whoever emits traceGoSysBlock increments p.syscalltick afterwards;
  3069  // and we wait for the increment before emitting traceGoSysExit.
  3070  // Note that the increment is done even if tracing is not enabled,
  3071  // because tracing can be enabled in the middle of syscall. We don't want the wait to hang.
  3072  //
  3073  //go:nosplit
  3074  func reentersyscall(pc, sp uintptr) {
  3075  	_g_ := getg()
  3076  
  3077  	// Disable preemption because during this function g is in Gsyscall status,
  3078  	// but can have inconsistent g->sched, do not let GC observe it.
  3079  	_g_.m.locks++
  3080  
  3081  	// Entersyscall must not call any function that might split/grow the stack.
  3082  	// (See details in comment above.)
  3083  	// Catch calls that might, by replacing the stack guard with something that
  3084  	// will trip any stack check and leaving a flag to tell newstack to die.
  3085  	_g_.stackguard0 = stackPreempt
  3086  	_g_.throwsplit = true
  3087  
  3088  	// Leave SP around for GC and traceback.
  3089  	save(pc, sp)
  3090  	_g_.syscallsp = sp
  3091  	_g_.syscallpc = pc
  3092  	casgstatus(_g_, _Grunning, _Gsyscall)
  3093  	if _g_.syscallsp < _g_.stack.lo || _g_.stack.hi < _g_.syscallsp {
  3094  		systemstack(func() {
  3095  			print("entersyscall inconsistent ", hex(_g_.syscallsp), " [", hex(_g_.stack.lo), ",", hex(_g_.stack.hi), "]\n")
  3096  			throw("entersyscall")
  3097  		})
  3098  	}
  3099  
  3100  	if trace.enabled {
  3101  		systemstack(traceGoSysCall)
  3102  		// systemstack itself clobbers g.sched.{pc,sp} and we might
  3103  		// need them later when the G is genuinely blocked in a
  3104  		// syscall
  3105  		save(pc, sp)
  3106  	}
  3107  
  3108  	if atomic.Load(&sched.sysmonwait) != 0 {
  3109  		systemstack(entersyscall_sysmon)
  3110  		save(pc, sp)
  3111  	}
  3112  
  3113  	if _g_.m.p.ptr().runSafePointFn != 0 {
  3114  		// runSafePointFn may stack split if run on this stack
  3115  		systemstack(runSafePointFn)
  3116  		save(pc, sp)
  3117  	}
  3118  
  3119  	_g_.m.syscalltick = _g_.m.p.ptr().syscalltick
  3120  	_g_.sysblocktraced = true
  3121  	pp := _g_.m.p.ptr()
  3122  	pp.m = 0
  3123  	_g_.m.oldp.set(pp)
  3124  	_g_.m.p = 0
  3125  	atomic.Store(&pp.status, _Psyscall)
  3126  	if sched.gcwaiting != 0 {
  3127  		systemstack(entersyscall_gcwait)
  3128  		save(pc, sp)
  3129  	}
  3130  
  3131  	_g_.m.locks--
  3132  }
  3133  
  3134  // Standard syscall entry used by the go syscall library and normal cgo calls.
  3135  //
  3136  // This is exported via linkname to assembly in the syscall package.
  3137  //
  3138  //go:nosplit
  3139  //go:linkname entersyscall
  3140  func entersyscall() {
  3141  	reentersyscall(getcallerpc(), getcallersp())
  3142  }
  3143  
  3144  func entersyscall_sysmon() {
  3145  	lock(&sched.lock)
  3146  	if atomic.Load(&sched.sysmonwait) != 0 {
  3147  		atomic.Store(&sched.sysmonwait, 0)
  3148  		notewakeup(&sched.sysmonnote)
  3149  	}
  3150  	unlock(&sched.lock)
  3151  }
  3152  
  3153  func entersyscall_gcwait() {
  3154  	_g_ := getg()
  3155  	_p_ := _g_.m.oldp.ptr()
  3156  
  3157  	lock(&sched.lock)
  3158  	if sched.stopwait > 0 && atomic.Cas(&_p_.status, _Psyscall, _Pgcstop) {
  3159  		if trace.enabled {
  3160  			traceGoSysBlock(_p_)
  3161  			traceProcStop(_p_)
  3162  		}
  3163  		_p_.syscalltick++
  3164  		if sched.stopwait--; sched.stopwait == 0 {
  3165  			notewakeup(&sched.stopnote)
  3166  		}
  3167  	}
  3168  	unlock(&sched.lock)
  3169  }
  3170  
  3171  // The same as entersyscall(), but with a hint that the syscall is blocking.
  3172  //go:nosplit
  3173  func entersyscallblock() {
  3174  	_g_ := getg()
  3175  
  3176  	_g_.m.locks++ // see comment in entersyscall
  3177  	_g_.throwsplit = true
  3178  	_g_.stackguard0 = stackPreempt // see comment in entersyscall
  3179  	_g_.m.syscalltick = _g_.m.p.ptr().syscalltick
  3180  	_g_.sysblocktraced = true
  3181  	_g_.m.p.ptr().syscalltick++
  3182  
  3183  	// Leave SP around for GC and traceback.
  3184  	pc := getcallerpc()
  3185  	sp := getcallersp()
  3186  	save(pc, sp)
  3187  	_g_.syscallsp = _g_.sched.sp
  3188  	_g_.syscallpc = _g_.sched.pc
  3189  	if _g_.syscallsp < _g_.stack.lo || _g_.stack.hi < _g_.syscallsp {
  3190  		sp1 := sp
  3191  		sp2 := _g_.sched.sp
  3192  		sp3 := _g_.syscallsp
  3193  		systemstack(func() {
  3194  			print("entersyscallblock inconsistent ", hex(sp1), " ", hex(sp2), " ", hex(sp3), " [", hex(_g_.stack.lo), ",", hex(_g_.stack.hi), "]\n")
  3195  			throw("entersyscallblock")
  3196  		})
  3197  	}
  3198  	casgstatus(_g_, _Grunning, _Gsyscall)
  3199  	if _g_.syscallsp < _g_.stack.lo || _g_.stack.hi < _g_.syscallsp {
  3200  		systemstack(func() {
  3201  			print("entersyscallblock inconsistent ", hex(sp), " ", hex(_g_.sched.sp), " ", hex(_g_.syscallsp), " [", hex(_g_.stack.lo), ",", hex(_g_.stack.hi), "]\n")
  3202  			throw("entersyscallblock")
  3203  		})
  3204  	}
  3205  
  3206  	systemstack(entersyscallblock_handoff)
  3207  
  3208  	// Resave for traceback during blocked call.
  3209  	save(getcallerpc(), getcallersp())
  3210  
  3211  	_g_.m.locks--
  3212  }
  3213  
  3214  func entersyscallblock_handoff() {
  3215  	if trace.enabled {
  3216  		traceGoSysCall()
  3217  		traceGoSysBlock(getg().m.p.ptr())
  3218  	}
  3219  	handoffp(releasep())
  3220  }
  3221  
  3222  // The goroutine g exited its system call.
  3223  // Arrange for it to run on a cpu again.
  3224  // This is called only from the go syscall library, not
  3225  // from the low-level system calls used by the runtime.
  3226  //
  3227  // Write barriers are not allowed because our P may have been stolen.
  3228  //
  3229  // This is exported via linkname to assembly in the syscall package.
  3230  //
  3231  //go:nosplit
  3232  //go:nowritebarrierrec
  3233  //go:linkname exitsyscall
  3234  func exitsyscall() {
  3235  	_g_ := getg()
  3236  
  3237  	_g_.m.locks++ // see comment in entersyscall
  3238  	if getcallersp() > _g_.syscallsp {
  3239  		throw("exitsyscall: syscall frame is no longer valid")
  3240  	}
  3241  
  3242  	_g_.waitsince = 0
  3243  	oldp := _g_.m.oldp.ptr()
  3244  	_g_.m.oldp = 0
  3245  	if exitsyscallfast(oldp) {
  3246  		if trace.enabled {
  3247  			if oldp != _g_.m.p.ptr() || _g_.m.syscalltick != _g_.m.p.ptr().syscalltick {
  3248  				systemstack(traceGoStart)
  3249  			}
  3250  		}
  3251  		// There's a cpu for us, so we can run.
  3252  		_g_.m.p.ptr().syscalltick++
  3253  		// We need to cas the status and scan before resuming...
  3254  		casgstatus(_g_, _Gsyscall, _Grunning)
  3255  
  3256  		// Garbage collector isn't running (since we are),
  3257  		// so okay to clear syscallsp.
  3258  		_g_.syscallsp = 0
  3259  		_g_.m.locks--
  3260  		if _g_.preempt {
  3261  			// restore the preemption request in case we've cleared it in newstack
  3262  			_g_.stackguard0 = stackPreempt
  3263  		} else {
  3264  			// otherwise restore the real _StackGuard, we've spoiled it in entersyscall/entersyscallblock
  3265  			_g_.stackguard0 = _g_.stack.lo + _StackGuard
  3266  		}
  3267  		_g_.throwsplit = false
  3268  
  3269  		if sched.disable.user && !schedEnabled(_g_) {
  3270  			// Scheduling of this goroutine is disabled.
  3271  			Gosched()
  3272  		}
  3273  
  3274  		return
  3275  	}
  3276  
  3277  	_g_.sysexitticks = 0
  3278  	if trace.enabled {
  3279  		// Wait till traceGoSysBlock event is emitted.
  3280  		// This ensures consistency of the trace (the goroutine is started after it is blocked).
  3281  		for oldp != nil && oldp.syscalltick == _g_.m.syscalltick {
  3282  			osyield()
  3283  		}
  3284  		// We can't trace syscall exit right now because we don't have a P.
  3285  		// Tracing code can invoke write barriers that cannot run without a P.
  3286  		// So instead we remember the syscall exit time and emit the event
  3287  		// in execute when we have a P.
  3288  		_g_.sysexitticks = cputicks()
  3289  	}
  3290  
  3291  	_g_.m.locks--
  3292  
  3293  	// Call the scheduler.
  3294  	mcall(exitsyscall0)
  3295  
  3296  	// Scheduler returned, so we're allowed to run now.
  3297  	// Delete the syscallsp information that we left for
  3298  	// the garbage collector during the system call.
  3299  	// Must wait until now because until gosched returns
  3300  	// we don't know for sure that the garbage collector
  3301  	// is not running.
  3302  	_g_.syscallsp = 0
  3303  	_g_.m.p.ptr().syscalltick++
  3304  	_g_.throwsplit = false
  3305  }
  3306  
  3307  //go:nosplit
  3308  func exitsyscallfast(oldp *p) bool {
  3309  	_g_ := getg()
  3310  
  3311  	// Freezetheworld sets stopwait but does not retake P's.
  3312  	if sched.stopwait == freezeStopWait {
  3313  		return false
  3314  	}
  3315  
  3316  	// Try to re-acquire the last P.
  3317  	if oldp != nil && oldp.status == _Psyscall && atomic.Cas(&oldp.status, _Psyscall, _Pidle) {
  3318  		// There's a cpu for us, so we can run.
  3319  		wirep(oldp)
  3320  		exitsyscallfast_reacquired()
  3321  		return true
  3322  	}
  3323  
  3324  	// Try to get any other idle P.
  3325  	if sched.pidle != 0 {
  3326  		var ok bool
  3327  		systemstack(func() {
  3328  			ok = exitsyscallfast_pidle()
  3329  			if ok && trace.enabled {
  3330  				if oldp != nil {
  3331  					// Wait till traceGoSysBlock event is emitted.
  3332  					// This ensures consistency of the trace (the goroutine is started after it is blocked).
  3333  					for oldp.syscalltick == _g_.m.syscalltick {
  3334  						osyield()
  3335  					}
  3336  				}
  3337  				traceGoSysExit(0)
  3338  			}
  3339  		})
  3340  		if ok {
  3341  			return true
  3342  		}
  3343  	}
  3344  	return false
  3345  }
  3346  
  3347  // exitsyscallfast_reacquired is the exitsyscall path on which this G
  3348  // has successfully reacquired the P it was running on before the
  3349  // syscall.
  3350  //
  3351  //go:nosplit
  3352  func exitsyscallfast_reacquired() {
  3353  	_g_ := getg()
  3354  	if _g_.m.syscalltick != _g_.m.p.ptr().syscalltick {
  3355  		if trace.enabled {
  3356  			// The p was retaken and then enter into syscall again (since _g_.m.syscalltick has changed).
  3357  			// traceGoSysBlock for this syscall was already emitted,
  3358  			// but here we effectively retake the p from the new syscall running on the same p.
  3359  			systemstack(func() {
  3360  				// Denote blocking of the new syscall.
  3361  				traceGoSysBlock(_g_.m.p.ptr())
  3362  				// Denote completion of the current syscall.
  3363  				traceGoSysExit(0)
  3364  			})
  3365  		}
  3366  		_g_.m.p.ptr().syscalltick++
  3367  	}
  3368  }
  3369  
  3370  func exitsyscallfast_pidle() bool {
  3371  	lock(&sched.lock)
  3372  	_p_ := pidleget()
  3373  	if _p_ != nil && atomic.Load(&sched.sysmonwait) != 0 {
  3374  		atomic.Store(&sched.sysmonwait, 0)
  3375  		notewakeup(&sched.sysmonnote)
  3376  	}
  3377  	unlock(&sched.lock)
  3378  	if _p_ != nil {
  3379  		acquirep(_p_)
  3380  		return true
  3381  	}
  3382  	return false
  3383  }
  3384  
  3385  // exitsyscall slow path on g0.
  3386  // Failed to acquire P, enqueue gp as runnable.
  3387  //
  3388  //go:nowritebarrierrec
  3389  func exitsyscall0(gp *g) {
  3390  	_g_ := getg()
  3391  
  3392  	casgstatus(gp, _Gsyscall, _Grunnable)
  3393  	dropg()
  3394  	lock(&sched.lock)
  3395  	var _p_ *p
  3396  	if schedEnabled(_g_) {
  3397  		_p_ = pidleget()
  3398  	}
  3399  	if _p_ == nil {
  3400  		globrunqput(gp)
  3401  	} else if atomic.Load(&sched.sysmonwait) != 0 {
  3402  		atomic.Store(&sched.sysmonwait, 0)
  3403  		notewakeup(&sched.sysmonnote)
  3404  	}
  3405  	unlock(&sched.lock)
  3406  	if _p_ != nil {
  3407  		acquirep(_p_)
  3408  		execute(gp, false) // Never returns.
  3409  	}
  3410  	if _g_.m.lockedg != 0 {
  3411  		// Wait until another thread schedules gp and so m again.
  3412  		stoplockedm()
  3413  		execute(gp, false) // Never returns.
  3414  	}
  3415  	stopm()
  3416  	schedule() // Never returns.
  3417  }
  3418  
  3419  func beforefork() {
  3420  	gp := getg().m.curg
  3421  
  3422  	// Block signals during a fork, so that the child does not run
  3423  	// a signal handler before exec if a signal is sent to the process
  3424  	// group. See issue #18600.
  3425  	gp.m.locks++
  3426  	sigsave(&gp.m.sigmask)
  3427  	sigblock()
  3428  
  3429  	// This function is called before fork in syscall package.
  3430  	// Code between fork and exec must not allocate memory nor even try to grow stack.
  3431  	// Here we spoil g->_StackGuard to reliably detect any attempts to grow stack.
  3432  	// runtime_AfterFork will undo this in parent process, but not in child.
  3433  	gp.stackguard0 = stackFork
  3434  }
  3435  
  3436  // Called from syscall package before fork.
  3437  //go:linkname syscall_runtime_BeforeFork syscall.runtime_BeforeFork
  3438  //go:nosplit
  3439  func syscall_runtime_BeforeFork() {
  3440  	systemstack(beforefork)
  3441  }
  3442  
  3443  func afterfork() {
  3444  	gp := getg().m.curg
  3445  
  3446  	// See the comments in beforefork.
  3447  	gp.stackguard0 = gp.stack.lo + _StackGuard
  3448  
  3449  	msigrestore(gp.m.sigmask)
  3450  
  3451  	gp.m.locks--
  3452  }
  3453  
  3454  // Called from syscall package after fork in parent.
  3455  //go:linkname syscall_runtime_AfterFork syscall.runtime_AfterFork
  3456  //go:nosplit
  3457  func syscall_runtime_AfterFork() {
  3458  	systemstack(afterfork)
  3459  }
  3460  
  3461  // inForkedChild is true while manipulating signals in the child process.
  3462  // This is used to avoid calling libc functions in case we are using vfork.
  3463  var inForkedChild bool
  3464  
  3465  // Called from syscall package after fork in child.
  3466  // It resets non-sigignored signals to the default handler, and
  3467  // restores the signal mask in preparation for the exec.
  3468  //
  3469  // Because this might be called during a vfork, and therefore may be
  3470  // temporarily sharing address space with the parent process, this must
  3471  // not change any global variables or calling into C code that may do so.
  3472  //
  3473  //go:linkname syscall_runtime_AfterForkInChild syscall.runtime_AfterForkInChild
  3474  //go:nosplit
  3475  //go:nowritebarrierrec
  3476  func syscall_runtime_AfterForkInChild() {
  3477  	// It's OK to change the global variable inForkedChild here
  3478  	// because we are going to change it back. There is no race here,
  3479  	// because if we are sharing address space with the parent process,
  3480  	// then the parent process can not be running concurrently.
  3481  	inForkedChild = true
  3482  
  3483  	clearSignalHandlers()
  3484  
  3485  	// When we are the child we are the only thread running,
  3486  	// so we know that nothing else has changed gp.m.sigmask.
  3487  	msigrestore(getg().m.sigmask)
  3488  
  3489  	inForkedChild = false
  3490  }
  3491  
  3492  // pendingPreemptSignals is the number of preemption signals
  3493  // that have been sent but not received. This is only used on Darwin.
  3494  // For #41702.
  3495  var pendingPreemptSignals uint32
  3496  
  3497  // Called from syscall package before Exec.
  3498  //go:linkname syscall_runtime_BeforeExec syscall.runtime_BeforeExec
  3499  func syscall_runtime_BeforeExec() {
  3500  	// Prevent thread creation during exec.
  3501  	execLock.lock()
  3502  
  3503  	// On Darwin, wait for all pending preemption signals to
  3504  	// be received. See issue #41702.
  3505  	if GOOS == "darwin" {
  3506  		for int32(atomic.Load(&pendingPreemptSignals)) > 0 {
  3507  			osyield()
  3508  		}
  3509  	}
  3510  }
  3511  
  3512  // Called from syscall package after Exec.
  3513  //go:linkname syscall_runtime_AfterExec syscall.runtime_AfterExec
  3514  func syscall_runtime_AfterExec() {
  3515  	execLock.unlock()
  3516  }
  3517  
  3518  // Allocate a new g, with a stack big enough for stacksize bytes.
  3519  func malg(stacksize int32) *g {
  3520  	newg := new(g)
  3521  	if stacksize >= 0 {
  3522  		stacksize = round2(_StackSystem + stacksize)
  3523  		systemstack(func() {
  3524  			newg.stack = stackalloc(uint32(stacksize))
  3525  		})
  3526  		newg.stackguard0 = newg.stack.lo + _StackGuard
  3527  		newg.stackguard1 = ^uintptr(0)
  3528  		// Clear the bottom word of the stack. We record g
  3529  		// there on gsignal stack during VDSO on ARM and ARM64.
  3530  		*(*uintptr)(unsafe.Pointer(newg.stack.lo)) = 0
  3531  	}
  3532  	return newg
  3533  }
  3534  
  3535  // Create a new g running fn with siz bytes of arguments.
  3536  // Put it on the queue of g's waiting to run.
  3537  // The compiler turns a go statement into a call to this.
  3538  //
  3539  // The stack layout of this call is unusual: it assumes that the
  3540  // arguments to pass to fn are on the stack sequentially immediately
  3541  // after &fn. Hence, they are logically part of newproc's argument
  3542  // frame, even though they don't appear in its signature (and can't
  3543  // because their types differ between call sites).
  3544  //
  3545  // This must be nosplit because this stack layout means there are
  3546  // untyped arguments in newproc's argument frame. Stack copies won't
  3547  // be able to adjust them and stack splits won't be able to copy them.
  3548  //
  3549  //go:nosplit
  3550  func newproc(siz int32, fn *funcval) {
  3551  	argp := add(unsafe.Pointer(&fn), sys.PtrSize)
  3552  	gp := getg()
  3553  	pc := getcallerpc()
  3554  	systemstack(func() {
  3555  		newg := newproc1(fn, argp, siz, gp, pc)
  3556  
  3557  		_p_ := getg().m.p.ptr()
  3558  		runqput(_p_, newg, true)
  3559  
  3560  		if mainStarted {
  3561  			wakep()
  3562  		}
  3563  	})
  3564  }
  3565  
  3566  // Create a new g in state _Grunnable, starting at fn, with narg bytes
  3567  // of arguments starting at argp. callerpc is the address of the go
  3568  // statement that created this. The caller is responsible for adding
  3569  // the new g to the scheduler.
  3570  //
  3571  // This must run on the system stack because it's the continuation of
  3572  // newproc, which cannot split the stack.
  3573  //
  3574  //go:systemstack
  3575  func newproc1(fn *funcval, argp unsafe.Pointer, narg int32, callergp *g, callerpc uintptr) *g {
  3576  	_g_ := getg()
  3577  
  3578  	if fn == nil {
  3579  		_g_.m.throwing = -1 // do not dump full stacks
  3580  		throw("go of nil func value")
  3581  	}
  3582  	acquirem() // disable preemption because it can be holding p in a local var
  3583  	siz := narg
  3584  	siz = (siz + 7) &^ 7
  3585  
  3586  	// We could allocate a larger initial stack if necessary.
  3587  	// Not worth it: this is almost always an error.
  3588  	// 4*sizeof(uintreg): extra space added below
  3589  	// sizeof(uintreg): caller's LR (arm) or return address (x86, in gostartcall).
  3590  	if siz >= _StackMin-4*sys.RegSize-sys.RegSize {
  3591  		throw("newproc: function arguments too large for new goroutine")
  3592  	}
  3593  
  3594  	_p_ := _g_.m.p.ptr()
  3595  	newg := gfget(_p_)
  3596  	if newg == nil {
  3597  		newg = malg(_StackMin)
  3598  		casgstatus(newg, _Gidle, _Gdead)
  3599  		allgadd(newg) // publishes with a g->status of Gdead so GC scanner doesn't look at uninitialized stack.
  3600  	}
  3601  	if newg.stack.hi == 0 {
  3602  		throw("newproc1: newg missing stack")
  3603  	}
  3604  
  3605  	if readgstatus(newg) != _Gdead {
  3606  		throw("newproc1: new g is not Gdead")
  3607  	}
  3608  
  3609  	totalSize := 4*sys.RegSize + uintptr(siz) + sys.MinFrameSize // extra space in case of reads slightly beyond frame
  3610  	totalSize += -totalSize & (sys.SpAlign - 1)                  // align to spAlign
  3611  	sp := newg.stack.hi - totalSize
  3612  	spArg := sp
  3613  	if usesLR {
  3614  		// caller's LR
  3615  		*(*uintptr)(unsafe.Pointer(sp)) = 0
  3616  		prepGoExitFrame(sp)
  3617  		spArg += sys.MinFrameSize
  3618  	}
  3619  	if narg > 0 {
  3620  		memmove(unsafe.Pointer(spArg), argp, uintptr(narg))
  3621  		// This is a stack-to-stack copy. If write barriers
  3622  		// are enabled and the source stack is grey (the
  3623  		// destination is always black), then perform a
  3624  		// barrier copy. We do this *after* the memmove
  3625  		// because the destination stack may have garbage on
  3626  		// it.
  3627  		if writeBarrier.needed && !_g_.m.curg.gcscandone {
  3628  			f := findfunc(fn.fn)
  3629  			stkmap := (*stackmap)(funcdata(f, _FUNCDATA_ArgsPointerMaps))
  3630  			if stkmap.nbit > 0 {
  3631  				// We're in the prologue, so it's always stack map index 0.
  3632  				bv := stackmapdata(stkmap, 0)
  3633  				bulkBarrierBitmap(spArg, spArg, uintptr(bv.n)*sys.PtrSize, 0, bv.bytedata)
  3634  			}
  3635  		}
  3636  	}
  3637  
  3638  	memclrNoHeapPointers(unsafe.Pointer(&newg.sched), unsafe.Sizeof(newg.sched))
  3639  	newg.sched.sp = sp
  3640  	newg.stktopsp = sp
  3641  	newg.sched.pc = funcPC(goexit) + sys.PCQuantum // +PCQuantum so that previous instruction is in same function
  3642  	newg.sched.g = guintptr(unsafe.Pointer(newg))
  3643  	gostartcallfn(&newg.sched, fn)
  3644  	newg.gopc = callerpc
  3645  	newg.ancestors = saveAncestors(callergp)
  3646  	newg.startpc = fn.fn
  3647  	if _g_.m.curg != nil {
  3648  		newg.labels = _g_.m.curg.labels
  3649  	}
  3650  	if isSystemGoroutine(newg, false) {
  3651  		atomic.Xadd(&sched.ngsys, +1)
  3652  	}
  3653  	casgstatus(newg, _Gdead, _Grunnable)
  3654  
  3655  	if _p_.goidcache == _p_.goidcacheend {
  3656  		// Sched.goidgen is the last allocated id,
  3657  		// this batch must be [sched.goidgen+1, sched.goidgen+GoidCacheBatch].
  3658  		// At startup sched.goidgen=0, so main goroutine receives goid=1.
  3659  		_p_.goidcache = atomic.Xadd64(&sched.goidgen, _GoidCacheBatch)
  3660  		_p_.goidcache -= _GoidCacheBatch - 1
  3661  		_p_.goidcacheend = _p_.goidcache + _GoidCacheBatch
  3662  	}
  3663  	newg.goid = int64(_p_.goidcache)
  3664  	_p_.goidcache++
  3665  	if raceenabled {
  3666  		newg.racectx = racegostart(callerpc)
  3667  	}
  3668  	if trace.enabled {
  3669  		traceGoCreate(newg, newg.startpc)
  3670  	}
  3671  	releasem(_g_.m)
  3672  
  3673  	return newg
  3674  }
  3675  
  3676  // saveAncestors copies previous ancestors of the given caller g and
  3677  // includes infor for the current caller into a new set of tracebacks for
  3678  // a g being created.
  3679  func saveAncestors(callergp *g) *[]ancestorInfo {
  3680  	// Copy all prior info, except for the root goroutine (goid 0).
  3681  	if debug.tracebackancestors <= 0 || callergp.goid == 0 {
  3682  		return nil
  3683  	}
  3684  	var callerAncestors []ancestorInfo
  3685  	if callergp.ancestors != nil {
  3686  		callerAncestors = *callergp.ancestors
  3687  	}
  3688  	n := int32(len(callerAncestors)) + 1
  3689  	if n > debug.tracebackancestors {
  3690  		n = debug.tracebackancestors
  3691  	}
  3692  	ancestors := make([]ancestorInfo, n)
  3693  	copy(ancestors[1:], callerAncestors)
  3694  
  3695  	var pcs [_TracebackMaxFrames]uintptr
  3696  	npcs := gcallers(callergp, 0, pcs[:])
  3697  	ipcs := make([]uintptr, npcs)
  3698  	copy(ipcs, pcs[:])
  3699  	ancestors[0] = ancestorInfo{
  3700  		pcs:  ipcs,
  3701  		goid: callergp.goid,
  3702  		gopc: callergp.gopc,
  3703  	}
  3704  
  3705  	ancestorsp := new([]ancestorInfo)
  3706  	*ancestorsp = ancestors
  3707  	return ancestorsp
  3708  }
  3709  
  3710  // Put on gfree list.
  3711  // If local list is too long, transfer a batch to the global list.
  3712  func gfput(_p_ *p, gp *g) {
  3713  	if readgstatus(gp) != _Gdead {
  3714  		throw("gfput: bad status (not Gdead)")
  3715  	}
  3716  
  3717  	stksize := gp.stack.hi - gp.stack.lo
  3718  
  3719  	if stksize != _FixedStack {
  3720  		// non-standard stack size - free it.
  3721  		stackfree(gp.stack)
  3722  		gp.stack.lo = 0
  3723  		gp.stack.hi = 0
  3724  		gp.stackguard0 = 0
  3725  	}
  3726  
  3727  	_p_.gFree.push(gp)
  3728  	_p_.gFree.n++
  3729  	if _p_.gFree.n >= 64 {
  3730  		lock(&sched.gFree.lock)
  3731  		for _p_.gFree.n >= 32 {
  3732  			_p_.gFree.n--
  3733  			gp = _p_.gFree.pop()
  3734  			if gp.stack.lo == 0 {
  3735  				sched.gFree.noStack.push(gp)
  3736  			} else {
  3737  				sched.gFree.stack.push(gp)
  3738  			}
  3739  			sched.gFree.n++
  3740  		}
  3741  		unlock(&sched.gFree.lock)
  3742  	}
  3743  }
  3744  
  3745  // Get from gfree list.
  3746  // If local list is empty, grab a batch from global list.
  3747  func gfget(_p_ *p) *g {
  3748  retry:
  3749  	if _p_.gFree.empty() && (!sched.gFree.stack.empty() || !sched.gFree.noStack.empty()) {
  3750  		lock(&sched.gFree.lock)
  3751  		// Move a batch of free Gs to the P.
  3752  		for _p_.gFree.n < 32 {
  3753  			// Prefer Gs with stacks.
  3754  			gp := sched.gFree.stack.pop()
  3755  			if gp == nil {
  3756  				gp = sched.gFree.noStack.pop()
  3757  				if gp == nil {
  3758  					break
  3759  				}
  3760  			}
  3761  			sched.gFree.n--
  3762  			_p_.gFree.push(gp)
  3763  			_p_.gFree.n++
  3764  		}
  3765  		unlock(&sched.gFree.lock)
  3766  		goto retry
  3767  	}
  3768  	gp := _p_.gFree.pop()
  3769  	if gp == nil {
  3770  		return nil
  3771  	}
  3772  	_p_.gFree.n--
  3773  	if gp.stack.lo == 0 {
  3774  		// Stack was deallocated in gfput. Allocate a new one.
  3775  		systemstack(func() {
  3776  			gp.stack = stackalloc(_FixedStack)
  3777  		})
  3778  		gp.stackguard0 = gp.stack.lo + _StackGuard
  3779  	} else {
  3780  		if raceenabled {
  3781  			racemalloc(unsafe.Pointer(gp.stack.lo), gp.stack.hi-gp.stack.lo)
  3782  		}
  3783  		if msanenabled {
  3784  			msanmalloc(unsafe.Pointer(gp.stack.lo), gp.stack.hi-gp.stack.lo)
  3785  		}
  3786  	}
  3787  	return gp
  3788  }
  3789  
  3790  // Purge all cached G's from gfree list to the global list.
  3791  func gfpurge(_p_ *p) {
  3792  	lock(&sched.gFree.lock)
  3793  	for !_p_.gFree.empty() {
  3794  		gp := _p_.gFree.pop()
  3795  		_p_.gFree.n--
  3796  		if gp.stack.lo == 0 {
  3797  			sched.gFree.noStack.push(gp)
  3798  		} else {
  3799  			sched.gFree.stack.push(gp)
  3800  		}
  3801  		sched.gFree.n++
  3802  	}
  3803  	unlock(&sched.gFree.lock)
  3804  }
  3805  
  3806  // Breakpoint executes a breakpoint trap.
  3807  func Breakpoint() {
  3808  	breakpoint()
  3809  }
  3810  
  3811  // dolockOSThread is called by LockOSThread and lockOSThread below
  3812  // after they modify m.locked. Do not allow preemption during this call,
  3813  // or else the m might be different in this function than in the caller.
  3814  //go:nosplit
  3815  func dolockOSThread() {
  3816  	if GOARCH == "wasm" {
  3817  		return // no threads on wasm yet
  3818  	}
  3819  	_g_ := getg()
  3820  	_g_.m.lockedg.set(_g_)
  3821  	_g_.lockedm.set(_g_.m)
  3822  }
  3823  
  3824  //go:nosplit
  3825  
  3826  // LockOSThread wires the calling goroutine to its current operating system thread.
  3827  // The calling goroutine will always execute in that thread,
  3828  // and no other goroutine will execute in it,
  3829  // until the calling goroutine has made as many calls to
  3830  // UnlockOSThread as to LockOSThread.
  3831  // If the calling goroutine exits without unlocking the thread,
  3832  // the thread will be terminated.
  3833  //
  3834  // All init functions are run on the startup thread. Calling LockOSThread
  3835  // from an init function will cause the main function to be invoked on
  3836  // that thread.
  3837  //
  3838  // A goroutine should call LockOSThread before calling OS services or
  3839  // non-Go library functions that depend on per-thread state.
  3840  func LockOSThread() {
  3841  	if atomic.Load(&newmHandoff.haveTemplateThread) == 0 && GOOS != "plan9" {
  3842  		// If we need to start a new thread from the locked
  3843  		// thread, we need the template thread. Start it now
  3844  		// while we're in a known-good state.
  3845  		startTemplateThread()
  3846  	}
  3847  	_g_ := getg()
  3848  	_g_.m.lockedExt++
  3849  	if _g_.m.lockedExt == 0 {
  3850  		_g_.m.lockedExt--
  3851  		panic("LockOSThread nesting overflow")
  3852  	}
  3853  	dolockOSThread()
  3854  }
  3855  
  3856  //go:nosplit
  3857  func lockOSThread() {
  3858  	getg().m.lockedInt++
  3859  	dolockOSThread()
  3860  }
  3861  
  3862  // dounlockOSThread is called by UnlockOSThread and unlockOSThread below
  3863  // after they update m->locked. Do not allow preemption during this call,
  3864  // or else the m might be in different in this function than in the caller.
  3865  //go:nosplit
  3866  func dounlockOSThread() {
  3867  	if GOARCH == "wasm" {
  3868  		return // no threads on wasm yet
  3869  	}
  3870  	_g_ := getg()
  3871  	if _g_.m.lockedInt != 0 || _g_.m.lockedExt != 0 {
  3872  		return
  3873  	}
  3874  	_g_.m.lockedg = 0
  3875  	_g_.lockedm = 0
  3876  }
  3877  
  3878  //go:nosplit
  3879  
  3880  // UnlockOSThread undoes an earlier call to LockOSThread.
  3881  // If this drops the number of active LockOSThread calls on the
  3882  // calling goroutine to zero, it unwires the calling goroutine from
  3883  // its fixed operating system thread.
  3884  // If there are no active LockOSThread calls, this is a no-op.
  3885  //
  3886  // Before calling UnlockOSThread, the caller must ensure that the OS
  3887  // thread is suitable for running other goroutines. If the caller made
  3888  // any permanent changes to the state of the thread that would affect
  3889  // other goroutines, it should not call this function and thus leave
  3890  // the goroutine locked to the OS thread until the goroutine (and
  3891  // hence the thread) exits.
  3892  func UnlockOSThread() {
  3893  	_g_ := getg()
  3894  	if _g_.m.lockedExt == 0 {
  3895  		return
  3896  	}
  3897  	_g_.m.lockedExt--
  3898  	dounlockOSThread()
  3899  }
  3900  
  3901  //go:nosplit
  3902  func unlockOSThread() {
  3903  	_g_ := getg()
  3904  	if _g_.m.lockedInt == 0 {
  3905  		systemstack(badunlockosthread)
  3906  	}
  3907  	_g_.m.lockedInt--
  3908  	dounlockOSThread()
  3909  }
  3910  
  3911  func badunlockosthread() {
  3912  	throw("runtime: internal error: misuse of lockOSThread/unlockOSThread")
  3913  }
  3914  
  3915  func gcount() int32 {
  3916  	n := int32(allglen) - sched.gFree.n - int32(atomic.Load(&sched.ngsys))
  3917  	for _, _p_ := range allp {
  3918  		n -= _p_.gFree.n
  3919  	}
  3920  
  3921  	// All these variables can be changed concurrently, so the result can be inconsistent.
  3922  	// But at least the current goroutine is running.
  3923  	if n < 1 {
  3924  		n = 1
  3925  	}
  3926  	return n
  3927  }
  3928  
  3929  func mcount() int32 {
  3930  	return int32(sched.mnext - sched.nmfreed)
  3931  }
  3932  
  3933  var prof struct {
  3934  	signalLock uint32
  3935  	hz         int32
  3936  }
  3937  
  3938  func _System()                    { _System() }
  3939  func _ExternalCode()              { _ExternalCode() }
  3940  func _LostExternalCode()          { _LostExternalCode() }
  3941  func _GC()                        { _GC() }
  3942  func _LostSIGPROFDuringAtomic64() { _LostSIGPROFDuringAtomic64() }
  3943  func _VDSO()                      { _VDSO() }
  3944  
  3945  // Called if we receive a SIGPROF signal.
  3946  // Called by the signal handler, may run during STW.
  3947  //go:nowritebarrierrec
  3948  func sigprof(pc, sp, lr uintptr, gp *g, mp *m) {
  3949  	if prof.hz == 0 {
  3950  		return
  3951  	}
  3952  
  3953  	// On mips{,le}, 64bit atomics are emulated with spinlocks, in
  3954  	// runtime/internal/atomic. If SIGPROF arrives while the program is inside
  3955  	// the critical section, it creates a deadlock (when writing the sample).
  3956  	// As a workaround, create a counter of SIGPROFs while in critical section
  3957  	// to store the count, and pass it to sigprof.add() later when SIGPROF is
  3958  	// received from somewhere else (with _LostSIGPROFDuringAtomic64 as pc).
  3959  	if GOARCH == "mips" || GOARCH == "mipsle" || GOARCH == "arm" {
  3960  		if f := findfunc(pc); f.valid() {
  3961  			if hasPrefix(funcname(f), "runtime/internal/atomic") {
  3962  				cpuprof.lostAtomic++
  3963  				return
  3964  			}
  3965  		}
  3966  	}
  3967  
  3968  	// Profiling runs concurrently with GC, so it must not allocate.
  3969  	// Set a trap in case the code does allocate.
  3970  	// Note that on windows, one thread takes profiles of all the
  3971  	// other threads, so mp is usually not getg().m.
  3972  	// In fact mp may not even be stopped.
  3973  	// See golang.org/issue/17165.
  3974  	getg().m.mallocing++
  3975  
  3976  	// Define that a "user g" is a user-created goroutine, and a "system g"
  3977  	// is one that is m->g0 or m->gsignal.
  3978  	//
  3979  	// We might be interrupted for profiling halfway through a
  3980  	// goroutine switch. The switch involves updating three (or four) values:
  3981  	// g, PC, SP, and (on arm) LR. The PC must be the last to be updated,
  3982  	// because once it gets updated the new g is running.
  3983  	//
  3984  	// When switching from a user g to a system g, LR is not considered live,
  3985  	// so the update only affects g, SP, and PC. Since PC must be last, there
  3986  	// the possible partial transitions in ordinary execution are (1) g alone is updated,
  3987  	// (2) both g and SP are updated, and (3) SP alone is updated.
  3988  	// If SP or g alone is updated, we can detect the partial transition by checking
  3989  	// whether the SP is within g's stack bounds. (We could also require that SP
  3990  	// be changed only after g, but the stack bounds check is needed by other
  3991  	// cases, so there is no need to impose an additional requirement.)
  3992  	//
  3993  	// There is one exceptional transition to a system g, not in ordinary execution.
  3994  	// When a signal arrives, the operating system starts the signal handler running
  3995  	// with an updated PC and SP. The g is updated last, at the beginning of the
  3996  	// handler. There are two reasons this is okay. First, until g is updated the
  3997  	// g and SP do not match, so the stack bounds check detects the partial transition.
  3998  	// Second, signal handlers currently run with signals disabled, so a profiling
  3999  	// signal cannot arrive during the handler.
  4000  	//
  4001  	// When switching from a system g to a user g, there are three possibilities.
  4002  	//
  4003  	// First, it may be that the g switch has no PC update, because the SP
  4004  	// either corresponds to a user g throughout (as in asmcgocall)
  4005  	// or because it has been arranged to look like a user g frame
  4006  	// (as in cgocallback_gofunc). In this case, since the entire
  4007  	// transition is a g+SP update, a partial transition updating just one of
  4008  	// those will be detected by the stack bounds check.
  4009  	//
  4010  	// Second, when returning from a signal handler, the PC and SP updates
  4011  	// are performed by the operating system in an atomic update, so the g
  4012  	// update must be done before them. The stack bounds check detects
  4013  	// the partial transition here, and (again) signal handlers run with signals
  4014  	// disabled, so a profiling signal cannot arrive then anyway.
  4015  	//
  4016  	// Third, the common case: it may be that the switch updates g, SP, and PC
  4017  	// separately. If the PC is within any of the functions that does this,
  4018  	// we don't ask for a traceback. C.F. the function setsSP for more about this.
  4019  	//
  4020  	// There is another apparently viable approach, recorded here in case
  4021  	// the "PC within setsSP function" check turns out not to be usable.
  4022  	// It would be possible to delay the update of either g or SP until immediately
  4023  	// before the PC update instruction. Then, because of the stack bounds check,
  4024  	// the only problematic interrupt point is just before that PC update instruction,
  4025  	// and the sigprof handler can detect that instruction and simulate stepping past
  4026  	// it in order to reach a consistent state. On ARM, the update of g must be made
  4027  	// in two places (in R10 and also in a TLS slot), so the delayed update would
  4028  	// need to be the SP update. The sigprof handler must read the instruction at
  4029  	// the current PC and if it was the known instruction (for example, JMP BX or
  4030  	// MOV R2, PC), use that other register in place of the PC value.
  4031  	// The biggest drawback to this solution is that it requires that we can tell
  4032  	// whether it's safe to read from the memory pointed at by PC.
  4033  	// In a correct program, we can test PC == nil and otherwise read,
  4034  	// but if a profiling signal happens at the instant that a program executes
  4035  	// a bad jump (before the program manages to handle the resulting fault)
  4036  	// the profiling handler could fault trying to read nonexistent memory.
  4037  	//
  4038  	// To recap, there are no constraints on the assembly being used for the
  4039  	// transition. We simply require that g and SP match and that the PC is not
  4040  	// in gogo.
  4041  	traceback := true
  4042  	if gp == nil || sp < gp.stack.lo || gp.stack.hi < sp || setsSP(pc) || (mp != nil && mp.vdsoSP != 0) {
  4043  		traceback = false
  4044  	}
  4045  	var stk [maxCPUProfStack]uintptr
  4046  	n := 0
  4047  	if mp.ncgo > 0 && mp.curg != nil && mp.curg.syscallpc != 0 && mp.curg.syscallsp != 0 {
  4048  		cgoOff := 0
  4049  		// Check cgoCallersUse to make sure that we are not
  4050  		// interrupting other code that is fiddling with
  4051  		// cgoCallers.  We are running in a signal handler
  4052  		// with all signals blocked, so we don't have to worry
  4053  		// about any other code interrupting us.
  4054  		if atomic.Load(&mp.cgoCallersUse) == 0 && mp.cgoCallers != nil && mp.cgoCallers[0] != 0 {
  4055  			for cgoOff < len(mp.cgoCallers) && mp.cgoCallers[cgoOff] != 0 {
  4056  				cgoOff++
  4057  			}
  4058  			copy(stk[:], mp.cgoCallers[:cgoOff])
  4059  			mp.cgoCallers[0] = 0
  4060  		}
  4061  
  4062  		// Collect Go stack that leads to the cgo call.
  4063  		n = gentraceback(mp.curg.syscallpc, mp.curg.syscallsp, 0, mp.curg, 0, &stk[cgoOff], len(stk)-cgoOff, nil, nil, 0)
  4064  		if n > 0 {
  4065  			n += cgoOff
  4066  		}
  4067  	} else if traceback {
  4068  		n = gentraceback(pc, sp, lr, gp, 0, &stk[0], len(stk), nil, nil, _TraceTrap|_TraceJumpStack)
  4069  	}
  4070  
  4071  	if n <= 0 {
  4072  		// Normal traceback is impossible or has failed.
  4073  		// See if it falls into several common cases.
  4074  		n = 0
  4075  		if (GOOS == "windows" || GOOS == "solaris" || GOOS == "illumos" || GOOS == "darwin" || GOOS == "aix") && mp.libcallg != 0 && mp.libcallpc != 0 && mp.libcallsp != 0 {
  4076  			// Libcall, i.e. runtime syscall on windows.
  4077  			// Collect Go stack that leads to the call.
  4078  			n = gentraceback(mp.libcallpc, mp.libcallsp, 0, mp.libcallg.ptr(), 0, &stk[0], len(stk), nil, nil, 0)
  4079  		}
  4080  		if n == 0 && mp != nil && mp.vdsoSP != 0 {
  4081  			n = gentraceback(mp.vdsoPC, mp.vdsoSP, 0, gp, 0, &stk[0], len(stk), nil, nil, _TraceTrap|_TraceJumpStack)
  4082  		}
  4083  		if n == 0 {
  4084  			// If all of the above has failed, account it against abstract "System" or "GC".
  4085  			n = 2
  4086  			if inVDSOPage(pc) {
  4087  				pc = funcPC(_VDSO) + sys.PCQuantum
  4088  			} else if pc > firstmoduledata.etext {
  4089  				// "ExternalCode" is better than "etext".
  4090  				pc = funcPC(_ExternalCode) + sys.PCQuantum
  4091  			}
  4092  			stk[0] = pc
  4093  			if mp.preemptoff != "" {
  4094  				stk[1] = funcPC(_GC) + sys.PCQuantum
  4095  			} else {
  4096  				stk[1] = funcPC(_System) + sys.PCQuantum
  4097  			}
  4098  		}
  4099  	}
  4100  
  4101  	if prof.hz != 0 {
  4102  		cpuprof.add(gp, stk[:n])
  4103  	}
  4104  	getg().m.mallocing--
  4105  }
  4106  
  4107  // If the signal handler receives a SIGPROF signal on a non-Go thread,
  4108  // it tries to collect a traceback into sigprofCallers.
  4109  // sigprofCallersUse is set to non-zero while sigprofCallers holds a traceback.
  4110  var sigprofCallers cgoCallers
  4111  var sigprofCallersUse uint32
  4112  
  4113  // sigprofNonGo is called if we receive a SIGPROF signal on a non-Go thread,
  4114  // and the signal handler collected a stack trace in sigprofCallers.
  4115  // When this is called, sigprofCallersUse will be non-zero.
  4116  // g is nil, and what we can do is very limited.
  4117  //go:nosplit
  4118  //go:nowritebarrierrec
  4119  func sigprofNonGo() {
  4120  	if prof.hz != 0 {
  4121  		n := 0
  4122  		for n < len(sigprofCallers) && sigprofCallers[n] != 0 {
  4123  			n++
  4124  		}
  4125  		cpuprof.addNonGo(sigprofCallers[:n])
  4126  	}
  4127  
  4128  	atomic.Store(&sigprofCallersUse, 0)
  4129  }
  4130  
  4131  // sigprofNonGoPC is called when a profiling signal arrived on a
  4132  // non-Go thread and we have a single PC value, not a stack trace.
  4133  // g is nil, and what we can do is very limited.
  4134  //go:nosplit
  4135  //go:nowritebarrierrec
  4136  func sigprofNonGoPC(pc uintptr) {
  4137  	if prof.hz != 0 {
  4138  		stk := []uintptr{
  4139  			pc,
  4140  			funcPC(_ExternalCode) + sys.PCQuantum,
  4141  		}
  4142  		cpuprof.addNonGo(stk)
  4143  	}
  4144  }
  4145  
  4146  // Reports whether a function will set the SP
  4147  // to an absolute value. Important that
  4148  // we don't traceback when these are at the bottom
  4149  // of the stack since we can't be sure that we will
  4150  // find the caller.
  4151  //
  4152  // If the function is not on the bottom of the stack
  4153  // we assume that it will have set it up so that traceback will be consistent,
  4154  // either by being a traceback terminating function
  4155  // or putting one on the stack at the right offset.
  4156  func setsSP(pc uintptr) bool {
  4157  	f := findfunc(pc)
  4158  	if !f.valid() {
  4159  		// couldn't find the function for this PC,
  4160  		// so assume the worst and stop traceback
  4161  		return true
  4162  	}
  4163  	switch f.funcID {
  4164  	case funcID_gogo, funcID_systemstack, funcID_mcall, funcID_morestack:
  4165  		return true
  4166  	}
  4167  	return false
  4168  }
  4169  
  4170  // setcpuprofilerate sets the CPU profiling rate to hz times per second.
  4171  // If hz <= 0, setcpuprofilerate turns off CPU profiling.
  4172  func setcpuprofilerate(hz int32) {
  4173  	// Force sane arguments.
  4174  	if hz < 0 {
  4175  		hz = 0
  4176  	}
  4177  
  4178  	// Disable preemption, otherwise we can be rescheduled to another thread
  4179  	// that has profiling enabled.
  4180  	_g_ := getg()
  4181  	_g_.m.locks++
  4182  
  4183  	// Stop profiler on this thread so that it is safe to lock prof.
  4184  	// if a profiling signal came in while we had prof locked,
  4185  	// it would deadlock.
  4186  	setThreadCPUProfiler(0)
  4187  
  4188  	for !atomic.Cas(&prof.signalLock, 0, 1) {
  4189  		osyield()
  4190  	}
  4191  	if prof.hz != hz {
  4192  		setProcessCPUProfiler(hz)
  4193  		prof.hz = hz
  4194  	}
  4195  	atomic.Store(&prof.signalLock, 0)
  4196  
  4197  	lock(&sched.lock)
  4198  	sched.profilehz = hz
  4199  	unlock(&sched.lock)
  4200  
  4201  	if hz != 0 {
  4202  		setThreadCPUProfiler(hz)
  4203  	}
  4204  
  4205  	_g_.m.locks--
  4206  }
  4207  
  4208  // init initializes pp, which may be a freshly allocated p or a
  4209  // previously destroyed p, and transitions it to status _Pgcstop.
  4210  func (pp *p) init(id int32) {
  4211  	pp.id = id
  4212  	pp.status = _Pgcstop
  4213  	pp.sudogcache = pp.sudogbuf[:0]
  4214  	for i := range pp.deferpool {
  4215  		pp.deferpool[i] = pp.deferpoolbuf[i][:0]
  4216  	}
  4217  	pp.wbBuf.reset()
  4218  	if pp.mcache == nil {
  4219  		if id == 0 {
  4220  			if mcache0 == nil {
  4221  				throw("missing mcache?")
  4222  			}
  4223  			// Use the bootstrap mcache0. Only one P will get
  4224  			// mcache0: the one with ID 0.
  4225  			pp.mcache = mcache0
  4226  		} else {
  4227  			pp.mcache = allocmcache()
  4228  		}
  4229  	}
  4230  	if raceenabled && pp.raceprocctx == 0 {
  4231  		if id == 0 {
  4232  			pp.raceprocctx = raceprocctx0
  4233  			raceprocctx0 = 0 // bootstrap
  4234  		} else {
  4235  			pp.raceprocctx = raceproccreate()
  4236  		}
  4237  	}
  4238  	lockInit(&pp.timersLock, lockRankTimers)
  4239  }
  4240  
  4241  // destroy releases all of the resources associated with pp and
  4242  // transitions it to status _Pdead.
  4243  //
  4244  // sched.lock must be held and the world must be stopped.
  4245  func (pp *p) destroy() {
  4246  	// Move all runnable goroutines to the global queue
  4247  	for pp.runqhead != pp.runqtail {
  4248  		// Pop from tail of local queue
  4249  		pp.runqtail--
  4250  		gp := pp.runq[pp.runqtail%uint32(len(pp.runq))].ptr()
  4251  		// Push onto head of global queue
  4252  		globrunqputhead(gp)
  4253  	}
  4254  	if pp.runnext != 0 {
  4255  		globrunqputhead(pp.runnext.ptr())
  4256  		pp.runnext = 0
  4257  	}
  4258  	if len(pp.timers) > 0 {
  4259  		plocal := getg().m.p.ptr()
  4260  		// The world is stopped, but we acquire timersLock to
  4261  		// protect against sysmon calling timeSleepUntil.
  4262  		// This is the only case where we hold the timersLock of
  4263  		// more than one P, so there are no deadlock concerns.
  4264  		lock(&plocal.timersLock)
  4265  		lock(&pp.timersLock)
  4266  		moveTimers(plocal, pp.timers)
  4267  		pp.timers = nil
  4268  		pp.numTimers = 0
  4269  		pp.adjustTimers = 0
  4270  		pp.deletedTimers = 0
  4271  		atomic.Store64(&pp.timer0When, 0)
  4272  		unlock(&pp.timersLock)
  4273  		unlock(&plocal.timersLock)
  4274  	}
  4275  	// If there's a background worker, make it runnable and put
  4276  	// it on the global queue so it can clean itself up.
  4277  	if gp := pp.gcBgMarkWorker.ptr(); gp != nil {
  4278  		casgstatus(gp, _Gwaiting, _Grunnable)
  4279  		if trace.enabled {
  4280  			traceGoUnpark(gp, 0)
  4281  		}
  4282  		globrunqput(gp)
  4283  		// This assignment doesn't race because the
  4284  		// world is stopped.
  4285  		pp.gcBgMarkWorker.set(nil)
  4286  	}
  4287  	// Flush p's write barrier buffer.
  4288  	if gcphase != _GCoff {
  4289  		wbBufFlush1(pp)
  4290  		pp.gcw.dispose()
  4291  	}
  4292  	for i := range pp.sudogbuf {
  4293  		pp.sudogbuf[i] = nil
  4294  	}
  4295  	pp.sudogcache = pp.sudogbuf[:0]
  4296  	for i := range pp.deferpool {
  4297  		for j := range pp.deferpoolbuf[i] {
  4298  			pp.deferpoolbuf[i][j] = nil
  4299  		}
  4300  		pp.deferpool[i] = pp.deferpoolbuf[i][:0]
  4301  	}
  4302  	systemstack(func() {
  4303  		for i := 0; i < pp.mspancache.len; i++ {
  4304  			// Safe to call since the world is stopped.
  4305  			mheap_.spanalloc.free(unsafe.Pointer(pp.mspancache.buf[i]))
  4306  		}
  4307  		pp.mspancache.len = 0
  4308  		pp.pcache.flush(&mheap_.pages)
  4309  	})
  4310  	freemcache(pp.mcache)
  4311  	pp.mcache = nil
  4312  	gfpurge(pp)
  4313  	traceProcFree(pp)
  4314  	if raceenabled {
  4315  		if pp.timerRaceCtx != 0 {
  4316  			// The race detector code uses a callback to fetch
  4317  			// the proc context, so arrange for that callback
  4318  			// to see the right thing.
  4319  			// This hack only works because we are the only
  4320  			// thread running.
  4321  			mp := getg().m
  4322  			phold := mp.p.ptr()
  4323  			mp.p.set(pp)
  4324  
  4325  			racectxend(pp.timerRaceCtx)
  4326  			pp.timerRaceCtx = 0
  4327  
  4328  			mp.p.set(phold)
  4329  		}
  4330  		raceprocdestroy(pp.raceprocctx)
  4331  		pp.raceprocctx = 0
  4332  	}
  4333  	pp.gcAssistTime = 0
  4334  	pp.status = _Pdead
  4335  }
  4336  
  4337  // Change number of processors. The world is stopped, sched is locked.
  4338  // gcworkbufs are not being modified by either the GC or
  4339  // the write barrier code.
  4340  // Returns list of Ps with local work, they need to be scheduled by the caller.
  4341  func procresize(nprocs int32) *p {
  4342  	old := gomaxprocs
  4343  	if old < 0 || nprocs <= 0 {
  4344  		throw("procresize: invalid arg")
  4345  	}
  4346  	if trace.enabled {
  4347  		traceGomaxprocs(nprocs)
  4348  	}
  4349  
  4350  	// update statistics
  4351  	now := nanotime()
  4352  	if sched.procresizetime != 0 {
  4353  		sched.totaltime += int64(old) * (now - sched.procresizetime)
  4354  	}
  4355  	sched.procresizetime = now
  4356  
  4357  	// Grow allp if necessary.
  4358  	if nprocs > int32(len(allp)) {
  4359  		// Synchronize with retake, which could be running
  4360  		// concurrently since it doesn't run on a P.
  4361  		lock(&allpLock)
  4362  		if nprocs <= int32(cap(allp)) {
  4363  			allp = allp[:nprocs]
  4364  		} else {
  4365  			nallp := make([]*p, nprocs)
  4366  			// Copy everything up to allp's cap so we
  4367  			// never lose old allocated Ps.
  4368  			copy(nallp, allp[:cap(allp)])
  4369  			allp = nallp
  4370  		}
  4371  		unlock(&allpLock)
  4372  	}
  4373  
  4374  	// initialize new P's
  4375  	for i := old; i < nprocs; i++ {
  4376  		pp := allp[i]
  4377  		if pp == nil {
  4378  			pp = new(p)
  4379  		}
  4380  		pp.init(i)
  4381  		atomicstorep(unsafe.Pointer(&allp[i]), unsafe.Pointer(pp))
  4382  	}
  4383  
  4384  	_g_ := getg()
  4385  	if _g_.m.p != 0 && _g_.m.p.ptr().id < nprocs {
  4386  		// continue to use the current P
  4387  		_g_.m.p.ptr().status = _Prunning
  4388  		_g_.m.p.ptr().mcache.prepareForSweep()
  4389  	} else {
  4390  		// release the current P and acquire allp[0].
  4391  		//
  4392  		// We must do this before destroying our current P
  4393  		// because p.destroy itself has write barriers, so we
  4394  		// need to do that from a valid P.
  4395  		if _g_.m.p != 0 {
  4396  			if trace.enabled {
  4397  				// Pretend that we were descheduled
  4398  				// and then scheduled again to keep
  4399  				// the trace sane.
  4400  				traceGoSched()
  4401  				traceProcStop(_g_.m.p.ptr())
  4402  			}
  4403  			_g_.m.p.ptr().m = 0
  4404  		}
  4405  		_g_.m.p = 0
  4406  		p := allp[0]
  4407  		p.m = 0
  4408  		p.status = _Pidle
  4409  		acquirep(p)
  4410  		if trace.enabled {
  4411  			traceGoStart()
  4412  		}
  4413  	}
  4414  
  4415  	// g.m.p is now set, so we no longer need mcache0 for bootstrapping.
  4416  	mcache0 = nil
  4417  
  4418  	// release resources from unused P's
  4419  	for i := nprocs; i < old; i++ {
  4420  		p := allp[i]
  4421  		p.destroy()
  4422  		// can't free P itself because it can be referenced by an M in syscall
  4423  	}
  4424  
  4425  	// Trim allp.
  4426  	if int32(len(allp)) != nprocs {
  4427  		lock(&allpLock)
  4428  		allp = allp[:nprocs]
  4429  		unlock(&allpLock)
  4430  	}
  4431  
  4432  	var runnablePs *p
  4433  	for i := nprocs - 1; i >= 0; i-- {
  4434  		p := allp[i]
  4435  		if _g_.m.p.ptr() == p {
  4436  			continue
  4437  		}
  4438  		p.status = _Pidle
  4439  		if runqempty(p) {
  4440  			pidleput(p)
  4441  		} else {
  4442  			p.m.set(mget())
  4443  			p.link.set(runnablePs)
  4444  			runnablePs = p
  4445  		}
  4446  	}
  4447  	stealOrder.reset(uint32(nprocs))
  4448  	var int32p *int32 = &gomaxprocs // make compiler check that gomaxprocs is an int32
  4449  	atomic.Store((*uint32)(unsafe.Pointer(int32p)), uint32(nprocs))
  4450  	return runnablePs
  4451  }
  4452  
  4453  // Associate p and the current m.
  4454  //
  4455  // This function is allowed to have write barriers even if the caller
  4456  // isn't because it immediately acquires _p_.
  4457  //
  4458  //go:yeswritebarrierrec
  4459  func acquirep(_p_ *p) {
  4460  	// Do the part that isn't allowed to have write barriers.
  4461  	wirep(_p_)
  4462  
  4463  	// Have p; write barriers now allowed.
  4464  
  4465  	// Perform deferred mcache flush before this P can allocate
  4466  	// from a potentially stale mcache.
  4467  	_p_.mcache.prepareForSweep()
  4468  
  4469  	if trace.enabled {
  4470  		traceProcStart()
  4471  	}
  4472  }
  4473  
  4474  // wirep is the first step of acquirep, which actually associates the
  4475  // current M to _p_. This is broken out so we can disallow write
  4476  // barriers for this part, since we don't yet have a P.
  4477  //
  4478  //go:nowritebarrierrec
  4479  //go:nosplit
  4480  func wirep(_p_ *p) {
  4481  	_g_ := getg()
  4482  
  4483  	if _g_.m.p != 0 {
  4484  		throw("wirep: already in go")
  4485  	}
  4486  	if _p_.m != 0 || _p_.status != _Pidle {
  4487  		id := int64(0)
  4488  		if _p_.m != 0 {
  4489  			id = _p_.m.ptr().id
  4490  		}
  4491  		print("wirep: p->m=", _p_.m, "(", id, ") p->status=", _p_.status, "\n")
  4492  		throw("wirep: invalid p state")
  4493  	}
  4494  	_g_.m.p.set(_p_)
  4495  	_p_.m.set(_g_.m)
  4496  	_p_.status = _Prunning
  4497  }
  4498  
  4499  // Disassociate p and the current m.
  4500  func releasep() *p {
  4501  	_g_ := getg()
  4502  
  4503  	if _g_.m.p == 0 {
  4504  		throw("releasep: invalid arg")
  4505  	}
  4506  	_p_ := _g_.m.p.ptr()
  4507  	if _p_.m.ptr() != _g_.m || _p_.status != _Prunning {
  4508  		print("releasep: m=", _g_.m, " m->p=", _g_.m.p.ptr(), " p->m=", hex(_p_.m), " p->status=", _p_.status, "\n")
  4509  		throw("releasep: invalid p state")
  4510  	}
  4511  	if trace.enabled {
  4512  		traceProcStop(_g_.m.p.ptr())
  4513  	}
  4514  	_g_.m.p = 0
  4515  	_p_.m = 0
  4516  	_p_.status = _Pidle
  4517  	return _p_
  4518  }
  4519  
  4520  func incidlelocked(v int32) {
  4521  	lock(&sched.lock)
  4522  	sched.nmidlelocked += v
  4523  	if v > 0 {
  4524  		checkdead()
  4525  	}
  4526  	unlock(&sched.lock)
  4527  }
  4528  
  4529  // Check for deadlock situation.
  4530  // The check is based on number of running M's, if 0 -> deadlock.
  4531  // sched.lock must be held.
  4532  func checkdead() {
  4533  	// For -buildmode=c-shared or -buildmode=c-archive it's OK if
  4534  	// there are no running goroutines. The calling program is
  4535  	// assumed to be running.
  4536  	if islibrary || isarchive {
  4537  		return
  4538  	}
  4539  
  4540  	// If we are dying because of a signal caught on an already idle thread,
  4541  	// freezetheworld will cause all running threads to block.
  4542  	// And runtime will essentially enter into deadlock state,
  4543  	// except that there is a thread that will call exit soon.
  4544  	if panicking > 0 {
  4545  		return
  4546  	}
  4547  
  4548  	// If we are not running under cgo, but we have an extra M then account
  4549  	// for it. (It is possible to have an extra M on Windows without cgo to
  4550  	// accommodate callbacks created by syscall.NewCallback. See issue #6751
  4551  	// for details.)
  4552  	var run0 int32
  4553  	if !iscgo && cgoHasExtraM {
  4554  		mp := lockextra(true)
  4555  		haveExtraM := extraMCount > 0
  4556  		unlockextra(mp)
  4557  		if haveExtraM {
  4558  			run0 = 1
  4559  		}
  4560  	}
  4561  
  4562  	run := mcount() - sched.nmidle - sched.nmidlelocked - sched.nmsys
  4563  	if run > run0 {
  4564  		return
  4565  	}
  4566  	if run < 0 {
  4567  		print("runtime: checkdead: nmidle=", sched.nmidle, " nmidlelocked=", sched.nmidlelocked, " mcount=", mcount(), " nmsys=", sched.nmsys, "\n")
  4568  		throw("checkdead: inconsistent counts")
  4569  	}
  4570  
  4571  	grunning := 0
  4572  	lock(&allglock)
  4573  	for i := 0; i < len(allgs); i++ {
  4574  		gp := allgs[i]
  4575  		if isSystemGoroutine(gp, false) {
  4576  			continue
  4577  		}
  4578  		s := readgstatus(gp)
  4579  		switch s &^ _Gscan {
  4580  		case _Gwaiting,
  4581  			_Gpreempted:
  4582  			grunning++
  4583  		case _Grunnable,
  4584  			_Grunning,
  4585  			_Gsyscall:
  4586  			unlock(&allglock)
  4587  			print("runtime: checkdead: find g ", gp.goid, " in status ", s, "\n")
  4588  			throw("checkdead: runnable g")
  4589  		}
  4590  	}
  4591  	unlock(&allglock)
  4592  	if grunning == 0 { // possible if main goroutine calls runtime·Goexit()
  4593  		unlock(&sched.lock) // unlock so that GODEBUG=scheddetail=1 doesn't hang
  4594  		throw("no goroutines (main called runtime.Goexit) - deadlock!")
  4595  	}
  4596  
  4597  	// Maybe jump time forward for playground.
  4598  	if faketime != 0 {
  4599  		when, _p_ := timeSleepUntil()
  4600  		if _p_ != nil {
  4601  			faketime = when
  4602  			for pp := &sched.pidle; *pp != 0; pp = &(*pp).ptr().link {
  4603  				if (*pp).ptr() == _p_ {
  4604  					*pp = _p_.link
  4605  					break
  4606  				}
  4607  			}
  4608  			mp := mget()
  4609  			if mp == nil {
  4610  				// There should always be a free M since
  4611  				// nothing is running.
  4612  				throw("checkdead: no m for timer")
  4613  			}
  4614  			mp.nextp.set(_p_)
  4615  			notewakeup(&mp.park)
  4616  			return
  4617  		}
  4618  	}
  4619  
  4620  	// There are no goroutines running, so we can look at the P's.
  4621  	for _, _p_ := range allp {
  4622  		if len(_p_.timers) > 0 {
  4623  			return
  4624  		}
  4625  	}
  4626  
  4627  	getg().m.throwing = -1 // do not dump full stacks
  4628  	unlock(&sched.lock)    // unlock so that GODEBUG=scheddetail=1 doesn't hang
  4629  	throw("all goroutines are asleep - deadlock!")
  4630  }
  4631  
  4632  // forcegcperiod is the maximum time in nanoseconds between garbage
  4633  // collections. If we go this long without a garbage collection, one
  4634  // is forced to run.
  4635  //
  4636  // This is a variable for testing purposes. It normally doesn't change.
  4637  var forcegcperiod int64 = 2 * 60 * 1e9
  4638  
  4639  // Always runs without a P, so write barriers are not allowed.
  4640  //
  4641  //go:nowritebarrierrec
  4642  func sysmon() {
  4643  	lock(&sched.lock)
  4644  	sched.nmsys++
  4645  	checkdead()
  4646  	unlock(&sched.lock)
  4647  
  4648  	lasttrace := int64(0)
  4649  	idle := 0 // how many cycles in succession we had not wokeup somebody
  4650  	delay := uint32(0)
  4651  	for {
  4652  		if idle == 0 { // start with 20us sleep...
  4653  			delay = 20
  4654  		} else if idle > 50 { // start doubling the sleep after 1ms...
  4655  			delay *= 2
  4656  		}
  4657  		if delay > 10*1000 { // up to 10ms
  4658  			delay = 10 * 1000
  4659  		}
  4660  		usleep(delay)
  4661  		now := nanotime()
  4662  		next, _ := timeSleepUntil()
  4663  		if debug.schedtrace <= 0 && (sched.gcwaiting != 0 || atomic.Load(&sched.npidle) == uint32(gomaxprocs)) {
  4664  			lock(&sched.lock)
  4665  			if atomic.Load(&sched.gcwaiting) != 0 || atomic.Load(&sched.npidle) == uint32(gomaxprocs) {
  4666  				if next > now {
  4667  					atomic.Store(&sched.sysmonwait, 1)
  4668  					unlock(&sched.lock)
  4669  					// Make wake-up period small enough
  4670  					// for the sampling to be correct.
  4671  					sleep := forcegcperiod / 2
  4672  					if next-now < sleep {
  4673  						sleep = next - now
  4674  					}
  4675  					shouldRelax := sleep >= osRelaxMinNS
  4676  					if shouldRelax {
  4677  						osRelax(true)
  4678  					}
  4679  					notetsleep(&sched.sysmonnote, sleep)
  4680  					if shouldRelax {
  4681  						osRelax(false)
  4682  					}
  4683  					now = nanotime()
  4684  					next, _ = timeSleepUntil()
  4685  					lock(&sched.lock)
  4686  					atomic.Store(&sched.sysmonwait, 0)
  4687  					noteclear(&sched.sysmonnote)
  4688  				}
  4689  				idle = 0
  4690  				delay = 20
  4691  			}
  4692  			unlock(&sched.lock)
  4693  		}
  4694  		lock(&sched.sysmonlock)
  4695  		{
  4696  			// If we spent a long time blocked on sysmonlock
  4697  			// then we want to update now and next since it's
  4698  			// likely stale.
  4699  			now1 := nanotime()
  4700  			if now1-now > 50*1000 /* 50µs */ {
  4701  				next, _ = timeSleepUntil()
  4702  			}
  4703  			now = now1
  4704  		}
  4705  
  4706  		// trigger libc interceptors if needed
  4707  		if *cgo_yield != nil {
  4708  			asmcgocall(*cgo_yield, nil)
  4709  		}
  4710  		// poll network if not polled for more than 10ms
  4711  		lastpoll := int64(atomic.Load64(&sched.lastpoll))
  4712  		if netpollinited() && lastpoll != 0 && lastpoll+10*1000*1000 < now {
  4713  			atomic.Cas64(&sched.lastpoll, uint64(lastpoll), uint64(now))
  4714  			list := netpoll(0) // non-blocking - returns list of goroutines
  4715  			if !list.empty() {
  4716  				// Need to decrement number of idle locked M's
  4717  				// (pretending that one more is running) before injectglist.
  4718  				// Otherwise it can lead to the following situation:
  4719  				// injectglist grabs all P's but before it starts M's to run the P's,
  4720  				// another M returns from syscall, finishes running its G,
  4721  				// observes that there is no work to do and no other running M's
  4722  				// and reports deadlock.
  4723  				incidlelocked(-1)
  4724  				injectglist(&list)
  4725  				incidlelocked(1)
  4726  			}
  4727  		}
  4728  		if next < now {
  4729  			// There are timers that should have already run,
  4730  			// perhaps because there is an unpreemptible P.
  4731  			// Try to start an M to run them.
  4732  			startm(nil, false)
  4733  		}
  4734  		if atomic.Load(&scavenge.sysmonWake) != 0 {
  4735  			// Kick the scavenger awake if someone requested it.
  4736  			wakeScavenger()
  4737  		}
  4738  		// retake P's blocked in syscalls
  4739  		// and preempt long running G's
  4740  		if retake(now) != 0 {
  4741  			idle = 0
  4742  		} else {
  4743  			idle++
  4744  		}
  4745  		// check if we need to force a GC
  4746  		if t := (gcTrigger{kind: gcTriggerTime, now: now}); t.test() && atomic.Load(&forcegc.idle) != 0 {
  4747  			lock(&forcegc.lock)
  4748  			forcegc.idle = 0
  4749  			var list gList
  4750  			list.push(forcegc.g)
  4751  			injectglist(&list)
  4752  			unlock(&forcegc.lock)
  4753  		}
  4754  		if debug.schedtrace > 0 && lasttrace+int64(debug.schedtrace)*1000000 <= now {
  4755  			lasttrace = now
  4756  			schedtrace(debug.scheddetail > 0)
  4757  		}
  4758  		unlock(&sched.sysmonlock)
  4759  	}
  4760  }
  4761  
  4762  type sysmontick struct {
  4763  	schedtick   uint32
  4764  	schedwhen   int64
  4765  	syscalltick uint32
  4766  	syscallwhen int64
  4767  }
  4768  
  4769  // forcePreemptNS is the time slice given to a G before it is
  4770  // preempted.
  4771  const forcePreemptNS = 10 * 1000 * 1000 // 10ms
  4772  
  4773  func retake(now int64) uint32 {
  4774  	n := 0
  4775  	// Prevent allp slice changes. This lock will be completely
  4776  	// uncontended unless we're already stopping the world.
  4777  	lock(&allpLock)
  4778  	// We can't use a range loop over allp because we may
  4779  	// temporarily drop the allpLock. Hence, we need to re-fetch
  4780  	// allp each time around the loop.
  4781  	for i := 0; i < len(allp); i++ {
  4782  		_p_ := allp[i]
  4783  		if _p_ == nil {
  4784  			// This can happen if procresize has grown
  4785  			// allp but not yet created new Ps.
  4786  			continue
  4787  		}
  4788  		pd := &_p_.sysmontick
  4789  		s := _p_.status
  4790  		sysretake := false
  4791  		if s == _Prunning || s == _Psyscall {
  4792  			// Preempt G if it's running for too long.
  4793  			t := int64(_p_.schedtick)
  4794  			if int64(pd.schedtick) != t {
  4795  				pd.schedtick = uint32(t)
  4796  				pd.schedwhen = now
  4797  			} else if pd.schedwhen+forcePreemptNS <= now {
  4798  				preemptone(_p_)
  4799  				// In case of syscall, preemptone() doesn't
  4800  				// work, because there is no M wired to P.
  4801  				sysretake = true
  4802  			}
  4803  		}
  4804  		if s == _Psyscall {
  4805  			// Retake P from syscall if it's there for more than 1 sysmon tick (at least 20us).
  4806  			t := int64(_p_.syscalltick)
  4807  			if !sysretake && int64(pd.syscalltick) != t {
  4808  				pd.syscalltick = uint32(t)
  4809  				pd.syscallwhen = now
  4810  				continue
  4811  			}
  4812  			// On the one hand we don't want to retake Ps if there is no other work to do,
  4813  			// but on the other hand we want to retake them eventually
  4814  			// because they can prevent the sysmon thread from deep sleep.
  4815  			if runqempty(_p_) && atomic.Load(&sched.nmspinning)+atomic.Load(&sched.npidle) > 0 && pd.syscallwhen+10*1000*1000 > now {
  4816  				continue
  4817  			}
  4818  			// Drop allpLock so we can take sched.lock.
  4819  			unlock(&allpLock)
  4820  			// Need to decrement number of idle locked M's
  4821  			// (pretending that one more is running) before the CAS.
  4822  			// Otherwise the M from which we retake can exit the syscall,
  4823  			// increment nmidle and report deadlock.
  4824  			incidlelocked(-1)
  4825  			if atomic.Cas(&_p_.status, s, _Pidle) {
  4826  				if trace.enabled {
  4827  					traceGoSysBlock(_p_)
  4828  					traceProcStop(_p_)
  4829  				}
  4830  				n++
  4831  				_p_.syscalltick++
  4832  				handoffp(_p_)
  4833  			}
  4834  			incidlelocked(1)
  4835  			lock(&allpLock)
  4836  		}
  4837  	}
  4838  	unlock(&allpLock)
  4839  	return uint32(n)
  4840  }
  4841  
  4842  // Tell all goroutines that they have been preempted and they should stop.
  4843  // This function is purely best-effort. It can fail to inform a goroutine if a
  4844  // processor just started running it.
  4845  // No locks need to be held.
  4846  // Returns true if preemption request was issued to at least one goroutine.
  4847  func preemptall() bool {
  4848  	res := false
  4849  	for _, _p_ := range allp {
  4850  		if _p_.status != _Prunning {
  4851  			continue
  4852  		}
  4853  		if preemptone(_p_) {
  4854  			res = true
  4855  		}
  4856  	}
  4857  	return res
  4858  }
  4859  
  4860  // Tell the goroutine running on processor P to stop.
  4861  // This function is purely best-effort. It can incorrectly fail to inform the
  4862  // goroutine. It can send inform the wrong goroutine. Even if it informs the
  4863  // correct goroutine, that goroutine might ignore the request if it is
  4864  // simultaneously executing newstack.
  4865  // No lock needs to be held.
  4866  // Returns true if preemption request was issued.
  4867  // The actual preemption will happen at some point in the future
  4868  // and will be indicated by the gp->status no longer being
  4869  // Grunning
  4870  func preemptone(_p_ *p) bool {
  4871  	mp := _p_.m.ptr()
  4872  	if mp == nil || mp == getg().m {
  4873  		return false
  4874  	}
  4875  	gp := mp.curg
  4876  	if gp == nil || gp == mp.g0 {
  4877  		return false
  4878  	}
  4879  
  4880  	gp.preempt = true
  4881  
  4882  	// Every call in a go routine checks for stack overflow by
  4883  	// comparing the current stack pointer to gp->stackguard0.
  4884  	// Setting gp->stackguard0 to StackPreempt folds
  4885  	// preemption into the normal stack overflow check.
  4886  	gp.stackguard0 = stackPreempt
  4887  
  4888  	// Request an async preemption of this P.
  4889  	if preemptMSupported && debug.asyncpreemptoff == 0 {
  4890  		_p_.preempt = true
  4891  		preemptM(mp)
  4892  	}
  4893  
  4894  	return true
  4895  }
  4896  
  4897  var starttime int64
  4898  
  4899  func schedtrace(detailed bool) {
  4900  	now := nanotime()
  4901  	if starttime == 0 {
  4902  		starttime = now
  4903  	}
  4904  
  4905  	lock(&sched.lock)
  4906  	print("SCHED ", (now-starttime)/1e6, "ms: gomaxprocs=", gomaxprocs, " idleprocs=", sched.npidle, " threads=", mcount(), " spinningthreads=", sched.nmspinning, " idlethreads=", sched.nmidle, " runqueue=", sched.runqsize)
  4907  	if detailed {
  4908  		print(" gcwaiting=", sched.gcwaiting, " nmidlelocked=", sched.nmidlelocked, " stopwait=", sched.stopwait, " sysmonwait=", sched.sysmonwait, "\n")
  4909  	}
  4910  	// We must be careful while reading data from P's, M's and G's.
  4911  	// Even if we hold schedlock, most data can be changed concurrently.
  4912  	// E.g. (p->m ? p->m->id : -1) can crash if p->m changes from non-nil to nil.
  4913  	for i, _p_ := range allp {
  4914  		mp := _p_.m.ptr()
  4915  		h := atomic.Load(&_p_.runqhead)
  4916  		t := atomic.Load(&_p_.runqtail)
  4917  		if detailed {
  4918  			id := int64(-1)
  4919  			if mp != nil {
  4920  				id = mp.id
  4921  			}
  4922  			print("  P", i, ": status=", _p_.status, " schedtick=", _p_.schedtick, " syscalltick=", _p_.syscalltick, " m=", id, " runqsize=", t-h, " gfreecnt=", _p_.gFree.n, " timerslen=", len(_p_.timers), "\n")
  4923  		} else {
  4924  			// In non-detailed mode format lengths of per-P run queues as:
  4925  			// [len1 len2 len3 len4]
  4926  			print(" ")
  4927  			if i == 0 {
  4928  				print("[")
  4929  			}
  4930  			print(t - h)
  4931  			if i == len(allp)-1 {
  4932  				print("]\n")
  4933  			}
  4934  		}
  4935  	}
  4936  
  4937  	if !detailed {
  4938  		unlock(&sched.lock)
  4939  		return
  4940  	}
  4941  
  4942  	for mp := allm; mp != nil; mp = mp.alllink {
  4943  		_p_ := mp.p.ptr()
  4944  		gp := mp.curg
  4945  		lockedg := mp.lockedg.ptr()
  4946  		id1 := int32(-1)
  4947  		if _p_ != nil {
  4948  			id1 = _p_.id
  4949  		}
  4950  		id2 := int64(-1)
  4951  		if gp != nil {
  4952  			id2 = gp.goid
  4953  		}
  4954  		id3 := int64(-1)
  4955  		if lockedg != nil {
  4956  			id3 = lockedg.goid
  4957  		}
  4958  		print("  M", mp.id, ": p=", id1, " curg=", id2, " mallocing=", mp.mallocing, " throwing=", mp.throwing, " preemptoff=", mp.preemptoff, ""+" locks=", mp.locks, " dying=", mp.dying, " spinning=", mp.spinning, " blocked=", mp.blocked, " lockedg=", id3, "\n")
  4959  	}
  4960  
  4961  	lock(&allglock)
  4962  	for gi := 0; gi < len(allgs); gi++ {
  4963  		gp := allgs[gi]
  4964  		mp := gp.m
  4965  		lockedm := gp.lockedm.ptr()
  4966  		id1 := int64(-1)
  4967  		if mp != nil {
  4968  			id1 = mp.id
  4969  		}
  4970  		id2 := int64(-1)
  4971  		if lockedm != nil {
  4972  			id2 = lockedm.id
  4973  		}
  4974  		print("  G", gp.goid, ": status=", readgstatus(gp), "(", gp.waitreason.String(), ") m=", id1, " lockedm=", id2, "\n")
  4975  	}
  4976  	unlock(&allglock)
  4977  	unlock(&sched.lock)
  4978  }
  4979  
  4980  // schedEnableUser enables or disables the scheduling of user
  4981  // goroutines.
  4982  //
  4983  // This does not stop already running user goroutines, so the caller
  4984  // should first stop the world when disabling user goroutines.
  4985  func schedEnableUser(enable bool) {
  4986  	lock(&sched.lock)
  4987  	if sched.disable.user == !enable {
  4988  		unlock(&sched.lock)
  4989  		return
  4990  	}
  4991  	sched.disable.user = !enable
  4992  	if enable {
  4993  		n := sched.disable.n
  4994  		sched.disable.n = 0
  4995  		globrunqputbatch(&sched.disable.runnable, n)
  4996  		unlock(&sched.lock)
  4997  		for ; n != 0 && sched.npidle != 0; n-- {
  4998  			startm(nil, false)
  4999  		}
  5000  	} else {
  5001  		unlock(&sched.lock)
  5002  	}
  5003  }
  5004  
  5005  // schedEnabled reports whether gp should be scheduled. It returns
  5006  // false is scheduling of gp is disabled.
  5007  func schedEnabled(gp *g) bool {
  5008  	if sched.disable.user {
  5009  		return isSystemGoroutine(gp, true)
  5010  	}
  5011  	return true
  5012  }
  5013  
  5014  // Put mp on midle list.
  5015  // Sched must be locked.
  5016  // May run during STW, so write barriers are not allowed.
  5017  //go:nowritebarrierrec
  5018  func mput(mp *m) {
  5019  	mp.schedlink = sched.midle
  5020  	sched.midle.set(mp)
  5021  	sched.nmidle++
  5022  	checkdead()
  5023  }
  5024  
  5025  // Try to get an m from midle list.
  5026  // Sched must be locked.
  5027  // May run during STW, so write barriers are not allowed.
  5028  //go:nowritebarrierrec
  5029  func mget() *m {
  5030  	mp := sched.midle.ptr()
  5031  	if mp != nil {
  5032  		sched.midle = mp.schedlink
  5033  		sched.nmidle--
  5034  	}
  5035  	return mp
  5036  }
  5037  
  5038  // Put gp on the global runnable queue.
  5039  // Sched must be locked.
  5040  // May run during STW, so write barriers are not allowed.
  5041  //go:nowritebarrierrec
  5042  func globrunqput(gp *g) {
  5043  	sched.runq.pushBack(gp)
  5044  	sched.runqsize++
  5045  }
  5046  
  5047  // Put gp at the head of the global runnable queue.
  5048  // Sched must be locked.
  5049  // May run during STW, so write barriers are not allowed.
  5050  //go:nowritebarrierrec
  5051  func globrunqputhead(gp *g) {
  5052  	sched.runq.push(gp)
  5053  	sched.runqsize++
  5054  }
  5055  
  5056  // Put a batch of runnable goroutines on the global runnable queue.
  5057  // This clears *batch.
  5058  // Sched must be locked.
  5059  func globrunqputbatch(batch *gQueue, n int32) {
  5060  	sched.runq.pushBackAll(*batch)
  5061  	sched.runqsize += n
  5062  	*batch = gQueue{}
  5063  }
  5064  
  5065  // Try get a batch of G's from the global runnable queue.
  5066  // Sched must be locked.
  5067  func globrunqget(_p_ *p, max int32) *g {
  5068  	if sched.runqsize == 0 {
  5069  		return nil
  5070  	}
  5071  
  5072  	n := sched.runqsize/gomaxprocs + 1
  5073  	if n > sched.runqsize {
  5074  		n = sched.runqsize
  5075  	}
  5076  	if max > 0 && n > max {
  5077  		n = max
  5078  	}
  5079  	if n > int32(len(_p_.runq))/2 {
  5080  		n = int32(len(_p_.runq)) / 2
  5081  	}
  5082  
  5083  	sched.runqsize -= n
  5084  
  5085  	gp := sched.runq.pop()
  5086  	n--
  5087  	for ; n > 0; n-- {
  5088  		gp1 := sched.runq.pop()
  5089  		runqput(_p_, gp1, false)
  5090  	}
  5091  	return gp
  5092  }
  5093  
  5094  // Put p to on _Pidle list.
  5095  // Sched must be locked.
  5096  // May run during STW, so write barriers are not allowed.
  5097  //go:nowritebarrierrec
  5098  func pidleput(_p_ *p) {
  5099  	if !runqempty(_p_) {
  5100  		throw("pidleput: P has non-empty run queue")
  5101  	}
  5102  	_p_.link = sched.pidle
  5103  	sched.pidle.set(_p_)
  5104  	atomic.Xadd(&sched.npidle, 1) // TODO: fast atomic
  5105  }
  5106  
  5107  // Try get a p from _Pidle list.
  5108  // Sched must be locked.
  5109  // May run during STW, so write barriers are not allowed.
  5110  //go:nowritebarrierrec
  5111  func pidleget() *p {
  5112  	_p_ := sched.pidle.ptr()
  5113  	if _p_ != nil {
  5114  		sched.pidle = _p_.link
  5115  		atomic.Xadd(&sched.npidle, -1) // TODO: fast atomic
  5116  	}
  5117  	return _p_
  5118  }
  5119  
  5120  // runqempty reports whether _p_ has no Gs on its local run queue.
  5121  // It never returns true spuriously.
  5122  func runqempty(_p_ *p) bool {
  5123  	// Defend against a race where 1) _p_ has G1 in runqnext but runqhead == runqtail,
  5124  	// 2) runqput on _p_ kicks G1 to the runq, 3) runqget on _p_ empties runqnext.
  5125  	// Simply observing that runqhead == runqtail and then observing that runqnext == nil
  5126  	// does not mean the queue is empty.
  5127  	for {
  5128  		head := atomic.Load(&_p_.runqhead)
  5129  		tail := atomic.Load(&_p_.runqtail)
  5130  		runnext := atomic.Loaduintptr((*uintptr)(unsafe.Pointer(&_p_.runnext)))
  5131  		if tail == atomic.Load(&_p_.runqtail) {
  5132  			return head == tail && runnext == 0
  5133  		}
  5134  	}
  5135  }
  5136  
  5137  // To shake out latent assumptions about scheduling order,
  5138  // we introduce some randomness into scheduling decisions
  5139  // when running with the race detector.
  5140  // The need for this was made obvious by changing the
  5141  // (deterministic) scheduling order in Go 1.5 and breaking
  5142  // many poorly-written tests.
  5143  // With the randomness here, as long as the tests pass
  5144  // consistently with -race, they shouldn't have latent scheduling
  5145  // assumptions.
  5146  const randomizeScheduler = raceenabled
  5147  
  5148  // runqput tries to put g on the local runnable queue.
  5149  // If next is false, runqput adds g to the tail of the runnable queue.
  5150  // If next is true, runqput puts g in the _p_.runnext slot.
  5151  // If the run queue is full, runnext puts g on the global queue.
  5152  // Executed only by the owner P.
  5153  func runqput(_p_ *p, gp *g, next bool) {
  5154  	if randomizeScheduler && next && fastrand()%2 == 0 {
  5155  		next = false
  5156  	}
  5157  
  5158  	if next {
  5159  	retryNext:
  5160  		oldnext := _p_.runnext
  5161  		if !_p_.runnext.cas(oldnext, guintptr(unsafe.Pointer(gp))) {
  5162  			goto retryNext
  5163  		}
  5164  		if oldnext == 0 {
  5165  			return
  5166  		}
  5167  		// Kick the old runnext out to the regular run queue.
  5168  		gp = oldnext.ptr()
  5169  	}
  5170  
  5171  retry:
  5172  	h := atomic.LoadAcq(&_p_.runqhead) // load-acquire, synchronize with consumers
  5173  	t := _p_.runqtail
  5174  	if t-h < uint32(len(_p_.runq)) {
  5175  		_p_.runq[t%uint32(len(_p_.runq))].set(gp)
  5176  		atomic.StoreRel(&_p_.runqtail, t+1) // store-release, makes the item available for consumption
  5177  		return
  5178  	}
  5179  	if runqputslow(_p_, gp, h, t) {
  5180  		return
  5181  	}
  5182  	// the queue is not full, now the put above must succeed
  5183  	goto retry
  5184  }
  5185  
  5186  // Put g and a batch of work from local runnable queue on global queue.
  5187  // Executed only by the owner P.
  5188  func runqputslow(_p_ *p, gp *g, h, t uint32) bool {
  5189  	var batch [len(_p_.runq)/2 + 1]*g
  5190  
  5191  	// First, grab a batch from local queue.
  5192  	n := t - h
  5193  	n = n / 2
  5194  	if n != uint32(len(_p_.runq)/2) {
  5195  		throw("runqputslow: queue is not full")
  5196  	}
  5197  	for i := uint32(0); i < n; i++ {
  5198  		batch[i] = _p_.runq[(h+i)%uint32(len(_p_.runq))].ptr()
  5199  	}
  5200  	if !atomic.CasRel(&_p_.runqhead, h, h+n) { // cas-release, commits consume
  5201  		return false
  5202  	}
  5203  	batch[n] = gp
  5204  
  5205  	if randomizeScheduler {
  5206  		for i := uint32(1); i <= n; i++ {
  5207  			j := fastrandn(i + 1)
  5208  			batch[i], batch[j] = batch[j], batch[i]
  5209  		}
  5210  	}
  5211  
  5212  	// Link the goroutines.
  5213  	for i := uint32(0); i < n; i++ {
  5214  		batch[i].schedlink.set(batch[i+1])
  5215  	}
  5216  	var q gQueue
  5217  	q.head.set(batch[0])
  5218  	q.tail.set(batch[n])
  5219  
  5220  	// Now put the batch on global queue.
  5221  	lock(&sched.lock)
  5222  	globrunqputbatch(&q, int32(n+1))
  5223  	unlock(&sched.lock)
  5224  	return true
  5225  }
  5226  
  5227  // runqputbatch tries to put all the G's on q on the local runnable queue.
  5228  // If the queue is full, they are put on the global queue; in that case
  5229  // this will temporarily acquire the scheduler lock.
  5230  // Executed only by the owner P.
  5231  func runqputbatch(pp *p, q *gQueue, qsize int) {
  5232  	h := atomic.LoadAcq(&pp.runqhead)
  5233  	t := pp.runqtail
  5234  	n := uint32(0)
  5235  	for !q.empty() && t-h < uint32(len(pp.runq)) {
  5236  		gp := q.pop()
  5237  		pp.runq[t%uint32(len(pp.runq))].set(gp)
  5238  		t++
  5239  		n++
  5240  	}
  5241  	qsize -= int(n)
  5242  
  5243  	if randomizeScheduler {
  5244  		off := func(o uint32) uint32 {
  5245  			return (pp.runqtail + o) % uint32(len(pp.runq))
  5246  		}
  5247  		for i := uint32(1); i < n; i++ {
  5248  			j := fastrandn(i + 1)
  5249  			pp.runq[off(i)], pp.runq[off(j)] = pp.runq[off(j)], pp.runq[off(i)]
  5250  		}
  5251  	}
  5252  
  5253  	atomic.StoreRel(&pp.runqtail, t)
  5254  	if !q.empty() {
  5255  		lock(&sched.lock)
  5256  		globrunqputbatch(q, int32(qsize))
  5257  		unlock(&sched.lock)
  5258  	}
  5259  }
  5260  
  5261  // Get g from local runnable queue.
  5262  // If inheritTime is true, gp should inherit the remaining time in the
  5263  // current time slice. Otherwise, it should start a new time slice.
  5264  // Executed only by the owner P.
  5265  func runqget(_p_ *p) (gp *g, inheritTime bool) {
  5266  	// If there's a runnext, it's the next G to run.
  5267  	for {
  5268  		next := _p_.runnext
  5269  		if next == 0 {
  5270  			break
  5271  		}
  5272  		if _p_.runnext.cas(next, 0) {
  5273  			return next.ptr(), true
  5274  		}
  5275  	}
  5276  
  5277  	for {
  5278  		h := atomic.LoadAcq(&_p_.runqhead) // load-acquire, synchronize with other consumers
  5279  		t := _p_.runqtail
  5280  		if t == h {
  5281  			return nil, false
  5282  		}
  5283  		gp := _p_.runq[h%uint32(len(_p_.runq))].ptr()
  5284  		if atomic.CasRel(&_p_.runqhead, h, h+1) { // cas-release, commits consume
  5285  			return gp, false
  5286  		}
  5287  	}
  5288  }
  5289  
  5290  // Grabs a batch of goroutines from _p_'s runnable queue into batch.
  5291  // Batch is a ring buffer starting at batchHead.
  5292  // Returns number of grabbed goroutines.
  5293  // Can be executed by any P.
  5294  func runqgrab(_p_ *p, batch *[256]guintptr, batchHead uint32, stealRunNextG bool) uint32 {
  5295  	for {
  5296  		h := atomic.LoadAcq(&_p_.runqhead) // load-acquire, synchronize with other consumers
  5297  		t := atomic.LoadAcq(&_p_.runqtail) // load-acquire, synchronize with the producer
  5298  		n := t - h
  5299  		n = n - n/2
  5300  		if n == 0 {
  5301  			if stealRunNextG {
  5302  				// Try to steal from _p_.runnext.
  5303  				if next := _p_.runnext; next != 0 {
  5304  					if _p_.status == _Prunning {
  5305  						// Sleep to ensure that _p_ isn't about to run the g
  5306  						// we are about to steal.
  5307  						// The important use case here is when the g running
  5308  						// on _p_ ready()s another g and then almost
  5309  						// immediately blocks. Instead of stealing runnext
  5310  						// in this window, back off to give _p_ a chance to
  5311  						// schedule runnext. This will avoid thrashing gs
  5312  						// between different Ps.
  5313  						// A sync chan send/recv takes ~50ns as of time of
  5314  						// writing, so 3us gives ~50x overshoot.
  5315  						if GOOS != "windows" {
  5316  							usleep(3)
  5317  						} else {
  5318  							// On windows system timer granularity is
  5319  							// 1-15ms, which is way too much for this
  5320  							// optimization. So just yield.
  5321  							osyield()
  5322  						}
  5323  					}
  5324  					if !_p_.runnext.cas(next, 0) {
  5325  						continue
  5326  					}
  5327  					batch[batchHead%uint32(len(batch))] = next
  5328  					return 1
  5329  				}
  5330  			}
  5331  			return 0
  5332  		}
  5333  		if n > uint32(len(_p_.runq)/2) { // read inconsistent h and t
  5334  			continue
  5335  		}
  5336  		for i := uint32(0); i < n; i++ {
  5337  			g := _p_.runq[(h+i)%uint32(len(_p_.runq))]
  5338  			batch[(batchHead+i)%uint32(len(batch))] = g
  5339  		}
  5340  		if atomic.CasRel(&_p_.runqhead, h, h+n) { // cas-release, commits consume
  5341  			return n
  5342  		}
  5343  	}
  5344  }
  5345  
  5346  // Steal half of elements from local runnable queue of p2
  5347  // and put onto local runnable queue of p.
  5348  // Returns one of the stolen elements (or nil if failed).
  5349  func runqsteal(_p_, p2 *p, stealRunNextG bool) *g {
  5350  	t := _p_.runqtail
  5351  	n := runqgrab(p2, &_p_.runq, t, stealRunNextG)
  5352  	if n == 0 {
  5353  		return nil
  5354  	}
  5355  	n--
  5356  	gp := _p_.runq[(t+n)%uint32(len(_p_.runq))].ptr()
  5357  	if n == 0 {
  5358  		return gp
  5359  	}
  5360  	h := atomic.LoadAcq(&_p_.runqhead) // load-acquire, synchronize with consumers
  5361  	if t-h+n >= uint32(len(_p_.runq)) {
  5362  		throw("runqsteal: runq overflow")
  5363  	}
  5364  	atomic.StoreRel(&_p_.runqtail, t+n) // store-release, makes the item available for consumption
  5365  	return gp
  5366  }
  5367  
  5368  // A gQueue is a dequeue of Gs linked through g.schedlink. A G can only
  5369  // be on one gQueue or gList at a time.
  5370  type gQueue struct {
  5371  	head guintptr
  5372  	tail guintptr
  5373  }
  5374  
  5375  // empty reports whether q is empty.
  5376  func (q *gQueue) empty() bool {
  5377  	return q.head == 0
  5378  }
  5379  
  5380  // push adds gp to the head of q.
  5381  func (q *gQueue) push(gp *g) {
  5382  	gp.schedlink = q.head
  5383  	q.head.set(gp)
  5384  	if q.tail == 0 {
  5385  		q.tail.set(gp)
  5386  	}
  5387  }
  5388  
  5389  // pushBack adds gp to the tail of q.
  5390  func (q *gQueue) pushBack(gp *g) {
  5391  	gp.schedlink = 0
  5392  	if q.tail != 0 {
  5393  		q.tail.ptr().schedlink.set(gp)
  5394  	} else {
  5395  		q.head.set(gp)
  5396  	}
  5397  	q.tail.set(gp)
  5398  }
  5399  
  5400  // pushBackAll adds all Gs in l2 to the tail of q. After this q2 must
  5401  // not be used.
  5402  func (q *gQueue) pushBackAll(q2 gQueue) {
  5403  	if q2.tail == 0 {
  5404  		return
  5405  	}
  5406  	q2.tail.ptr().schedlink = 0
  5407  	if q.tail != 0 {
  5408  		q.tail.ptr().schedlink = q2.head
  5409  	} else {
  5410  		q.head = q2.head
  5411  	}
  5412  	q.tail = q2.tail
  5413  }
  5414  
  5415  // pop removes and returns the head of queue q. It returns nil if
  5416  // q is empty.
  5417  func (q *gQueue) pop() *g {
  5418  	gp := q.head.ptr()
  5419  	if gp != nil {
  5420  		q.head = gp.schedlink
  5421  		if q.head == 0 {
  5422  			q.tail = 0
  5423  		}
  5424  	}
  5425  	return gp
  5426  }
  5427  
  5428  // popList takes all Gs in q and returns them as a gList.
  5429  func (q *gQueue) popList() gList {
  5430  	stack := gList{q.head}
  5431  	*q = gQueue{}
  5432  	return stack
  5433  }
  5434  
  5435  // A gList is a list of Gs linked through g.schedlink. A G can only be
  5436  // on one gQueue or gList at a time.
  5437  type gList struct {
  5438  	head guintptr
  5439  }
  5440  
  5441  // empty reports whether l is empty.
  5442  func (l *gList) empty() bool {
  5443  	return l.head == 0
  5444  }
  5445  
  5446  // push adds gp to the head of l.
  5447  func (l *gList) push(gp *g) {
  5448  	gp.schedlink = l.head
  5449  	l.head.set(gp)
  5450  }
  5451  
  5452  // pushAll prepends all Gs in q to l.
  5453  func (l *gList) pushAll(q gQueue) {
  5454  	if !q.empty() {
  5455  		q.tail.ptr().schedlink = l.head
  5456  		l.head = q.head
  5457  	}
  5458  }
  5459  
  5460  // pop removes and returns the head of l. If l is empty, it returns nil.
  5461  func (l *gList) pop() *g {
  5462  	gp := l.head.ptr()
  5463  	if gp != nil {
  5464  		l.head = gp.schedlink
  5465  	}
  5466  	return gp
  5467  }
  5468  
  5469  //go:linkname setMaxThreads runtime/debug.setMaxThreads
  5470  func setMaxThreads(in int) (out int) {
  5471  	lock(&sched.lock)
  5472  	out = int(sched.maxmcount)
  5473  	if in > 0x7fffffff { // MaxInt32
  5474  		sched.maxmcount = 0x7fffffff
  5475  	} else {
  5476  		sched.maxmcount = int32(in)
  5477  	}
  5478  	checkmcount()
  5479  	unlock(&sched.lock)
  5480  	return
  5481  }
  5482  
  5483  func haveexperiment(name string) bool {
  5484  	if name == "framepointer" {
  5485  		return framepointer_enabled // set by linker
  5486  	}
  5487  	x := sys.Goexperiment
  5488  	for x != "" {
  5489  		xname := ""
  5490  		i := index(x, ",")
  5491  		if i < 0 {
  5492  			xname, x = x, ""
  5493  		} else {
  5494  			xname, x = x[:i], x[i+1:]
  5495  		}
  5496  		if xname == name {
  5497  			return true
  5498  		}
  5499  		if len(xname) > 2 && xname[:2] == "no" && xname[2:] == name {
  5500  			return false
  5501  		}
  5502  	}
  5503  	return false
  5504  }
  5505  
  5506  //go:nosplit
  5507  func procPin() int {
  5508  	_g_ := getg()
  5509  	mp := _g_.m
  5510  
  5511  	mp.locks++
  5512  	return int(mp.p.ptr().id)
  5513  }
  5514  
  5515  //go:nosplit
  5516  func procUnpin() {
  5517  	_g_ := getg()
  5518  	_g_.m.locks--
  5519  }
  5520  
  5521  //go:linkname sync_runtime_procPin sync.runtime_procPin
  5522  //go:nosplit
  5523  func sync_runtime_procPin() int {
  5524  	return procPin()
  5525  }
  5526  
  5527  //go:linkname sync_runtime_procUnpin sync.runtime_procUnpin
  5528  //go:nosplit
  5529  func sync_runtime_procUnpin() {
  5530  	procUnpin()
  5531  }
  5532  
  5533  //go:linkname sync_atomic_runtime_procPin sync/atomic.runtime_procPin
  5534  //go:nosplit
  5535  func sync_atomic_runtime_procPin() int {
  5536  	return procPin()
  5537  }
  5538  
  5539  //go:linkname sync_atomic_runtime_procUnpin sync/atomic.runtime_procUnpin
  5540  //go:nosplit
  5541  func sync_atomic_runtime_procUnpin() {
  5542  	procUnpin()
  5543  }
  5544  
  5545  // Active spinning for sync.Mutex.
  5546  //go:linkname sync_runtime_canSpin sync.runtime_canSpin
  5547  //go:nosplit
  5548  func sync_runtime_canSpin(i int) bool {
  5549  	// sync.Mutex is cooperative, so we are conservative with spinning.
  5550  	// Spin only few times and only if running on a multicore machine and
  5551  	// GOMAXPROCS>1 and there is at least one other running P and local runq is empty.
  5552  	// As opposed to runtime mutex we don't do passive spinning here,
  5553  	// because there can be work on global runq or on other Ps.
  5554  	if i >= active_spin || ncpu <= 1 || gomaxprocs <= int32(sched.npidle+sched.nmspinning)+1 {
  5555  		return false
  5556  	}
  5557  	if p := getg().m.p.ptr(); !runqempty(p) {
  5558  		return false
  5559  	}
  5560  	return true
  5561  }
  5562  
  5563  //go:linkname sync_runtime_doSpin sync.runtime_doSpin
  5564  //go:nosplit
  5565  func sync_runtime_doSpin() {
  5566  	procyield(active_spin_cnt)
  5567  }
  5568  
  5569  var stealOrder randomOrder
  5570  
  5571  // randomOrder/randomEnum are helper types for randomized work stealing.
  5572  // They allow to enumerate all Ps in different pseudo-random orders without repetitions.
  5573  // The algorithm is based on the fact that if we have X such that X and GOMAXPROCS
  5574  // are coprime, then a sequences of (i + X) % GOMAXPROCS gives the required enumeration.
  5575  type randomOrder struct {
  5576  	count    uint32
  5577  	coprimes []uint32
  5578  }
  5579  
  5580  type randomEnum struct {
  5581  	i     uint32
  5582  	count uint32
  5583  	pos   uint32
  5584  	inc   uint32
  5585  }
  5586  
  5587  func (ord *randomOrder) reset(count uint32) {
  5588  	ord.count = count
  5589  	ord.coprimes = ord.coprimes[:0]
  5590  	for i := uint32(1); i <= count; i++ {
  5591  		if gcd(i, count) == 1 {
  5592  			ord.coprimes = append(ord.coprimes, i)
  5593  		}
  5594  	}
  5595  }
  5596  
  5597  func (ord *randomOrder) start(i uint32) randomEnum {
  5598  	return randomEnum{
  5599  		count: ord.count,
  5600  		pos:   i % ord.count,
  5601  		inc:   ord.coprimes[i%uint32(len(ord.coprimes))],
  5602  	}
  5603  }
  5604  
  5605  func (enum *randomEnum) done() bool {
  5606  	return enum.i == enum.count
  5607  }
  5608  
  5609  func (enum *randomEnum) next() {
  5610  	enum.i++
  5611  	enum.pos = (enum.pos + enum.inc) % enum.count
  5612  }
  5613  
  5614  func (enum *randomEnum) position() uint32 {
  5615  	return enum.pos
  5616  }
  5617  
  5618  func gcd(a, b uint32) uint32 {
  5619  	for b != 0 {
  5620  		a, b = b, a%b
  5621  	}
  5622  	return a
  5623  }
  5624  
  5625  // An initTask represents the set of initializations that need to be done for a package.
  5626  // Keep in sync with ../../test/initempty.go:initTask
  5627  type initTask struct {
  5628  	// TODO: pack the first 3 fields more tightly?
  5629  	state uintptr // 0 = uninitialized, 1 = in progress, 2 = done
  5630  	ndeps uintptr
  5631  	nfns  uintptr
  5632  	// followed by ndeps instances of an *initTask, one per package depended on
  5633  	// followed by nfns pcs, one per init function to run
  5634  }
  5635  
  5636  func doInit(t *initTask) {
  5637  	switch t.state {
  5638  	case 2: // fully initialized
  5639  		return
  5640  	case 1: // initialization in progress
  5641  		throw("recursive call during initialization - linker skew")
  5642  	default: // not initialized yet
  5643  		t.state = 1 // initialization in progress
  5644  		for i := uintptr(0); i < t.ndeps; i++ {
  5645  			p := add(unsafe.Pointer(t), (3+i)*sys.PtrSize)
  5646  			t2 := *(**initTask)(p)
  5647  			doInit(t2)
  5648  		}
  5649  		for i := uintptr(0); i < t.nfns; i++ {
  5650  			p := add(unsafe.Pointer(t), (3+t.ndeps+i)*sys.PtrSize)
  5651  			f := *(*func())(unsafe.Pointer(&p))
  5652  			f()
  5653  		}
  5654  		t.state = 2 // initialization done
  5655  	}
  5656  }
  5657  

View as plain text