Git Product home page Git Product logo

Comments (5)

RainbowMango avatar RainbowMango commented on August 21, 2024

你过誉了:)

查看了部分源码,现在更倾向于Once a runnable G is found, it is executed until it is blocked.这个说法。

https://github.com/golang/go/blob/2197321db1dd997165c0091ba2bcb3b6be7633d0/src/runtime/proc.go#L2165-L2174

这里有协程执行的函数

// Schedules gp to run on the current M.
// If inheritTime is true, gp inherits the remaining time in the
// current time slice. Otherwise, it starts a new time slice.
// Never returns.
//
// Write barriers are allowed because this is called immediately after
// acquiring a P in several places.
//
//go:yeswritebarrierrec
func execute(gp *g, inheritTime bool) {

协程一旦被调度,没有主动弹出的操作。

协程这部分代码有点复杂,还没仔细看,这个章节也是不完整的。
先暂时搁置,待更详细的分析源码后再修改。

PS:如果你有兴趣,可以深入研究下:)

from goexpertprogramming.

liyonglion avatar liyonglion commented on August 21, 2024

貌似是在函数调用的时候,在栈上做了特殊处理。当发生函数调用,会检查某些条件(具体啥条件我也不知道),如果触发了该条件,则会调度另外一个G运行。

from goexpertprogramming.

RainbowMango avatar RainbowMango commented on August 21, 2024

貌似是在函数调用的时候,在栈上做了特殊处理。当发生函数调用,会检查某些条件(具体啥条件我也不知道),如果触发了该条件,则会调度另外一个G运行。

对的。正是因为依赖函数调用才能实现G的切换,所以如果函数没有发生函数调用(死循环)时,G就无法被抢占,Go在1.14版本实现的抢占式调度正是解决的这个问题。

from goexpertprogramming.

liyonglion avatar liyonglion commented on August 21, 2024

貌似是在函数调用的时候,在栈上做了特殊处理。当发生函数调用,会检查某些条件(具体啥条件我也不知道),如果触发了该条件,则会调度另外一个G运行。

对的。正是因为依赖函数调用才能实现G的切换,所以如果函数没有发生函数调用(死循环)时,G就无法被抢占,Go在1.14版本实现的抢占式调度正是解决的这个问题。

感谢 @RainbowMango 贡献如此好文,结合你的文章,再看源码如虎添翼

from goexpertprogramming.

RainbowMango avatar RainbowMango commented on August 21, 2024

哈哈,你这么说我很高兴,能帮助别人看源码也算没白费力。

from goexpertprogramming.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.