Skip to content

Instantly share code, notes, and snippets.

@xyun1996

xyun1996/proc.go Secret

Last active May 10, 2022 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xyun1996/b2c181a0f327478d968621a6063494ef to your computer and use it in GitHub Desktop.
Save xyun1996/b2c181a0f327478d968621a6063494ef to your computer and use it in GitHub Desktop.
newproc
func newproc(siz int32, fn *funcval) {
argp := add(unsafe.Pointer(&fn), sys.PtrSize)
gp := getg()
pc := getcallerpc()
systemstack(func() {
newg := newproc1(fn, argp, siz, gp, pc)
_p_ := getg().m.p.ptr()
runqput(_p_, newg, true) //true 始终把p的runnext设置为newg
if mainStarted {
wakep()
}
})
}
func runqput(_p_ *p, gp *g, next bool) {
if randomizeScheduler && next && fastrand()%2 == 0 {
next = false
}
if next {
retryNext:
oldnext := _p_.runnext
if !_p_.runnext.cas(oldnext, guintptr(unsafe.Pointer(gp))) {
goto retryNext
}
if oldnext == 0 {
return
}
// Kick the old runnext out to the regular run queue.
gp = oldnext.ptr()
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment