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

View as plain text