Setjmp/longjmp in Ruby’s Continuation

左心房为你撑大大i 提交于 2019-12-11 07:28:32

问题


I was wondering about this while digging through the code of cont.c in Ruby’s current version. The documentation of setjmp says that calling longjmp on the jmp_buf structure after the caller of setjmp returned is an error. But, it seems that Ruby does this happily and does not crash:

https://github.com/ruby/ruby/blob/05f087c844f0e1e7cfe323edcf591de64069a289/cont.c#L522 https://github.com/ruby/ruby/blob/05f087c844f0e1e7cfe323edcf591de64069a289/cont.c#L775 (+ a couple more usages with fibers)

And I ask: how is that possible?

I did notice that the same source code uses also set/getcontext, but only with fibers or so it seems. It would seem more reasonable to me to use these functions rather than set/longjmp.

来源:https://stackoverflow.com/questions/28569337/setjmp-longjmp-in-ruby-s-continuation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!