关键字: lua coroutine
一直对coroutine的运作原理不是很明白, 这几天琢磨了下终于搞明白了:
root@yfnix:~/lua-5.1.3/src# cat t.lua
a= coroutine.create(function ()
b= coroutine.create(function ()
coroutine.yield(1)
end)
coroutine.resume(b)
end)
coroutine.resume(a,1)
root@yfnix:~/lua-5.1.3/src# gdb --args lua t.lua
b lua_yield
r
bt
得到以下的调用stack
#0 lua_yield (L=0x80770b0, nresults=1) at ldo.c:446
#1 0x08064f7d in luaB_yield (L=0x80770b0) at lbaselib.c:592
#2 0x08050ac2 in luaD_precall (L=0x80770b0, func=0x8077b00, nresults=0) at ldo.c:319
#3 0x0805d2c8 in
luaV_execute (L=0x80770b0, nexeccalls=1) at lvm.c:587
#4 0x08050e11 in resume (L=0x80770b0, ud=0x8077b00) at ldo.c:404
#5 0x0804fec5 in luaD_rawrunprotected (L=0x80770b0, f=0x8050d56
, ud=0x8077b00) at ldo.c:116
#6 0x08050f61 in lua_resume (L=0x80770b0, nargs=0) at ldo.c:427
#7 0x08064c97 in auxresume (L=0x8076e90, co=0x80770b0, narg=0) at lbaselib.c:526
#8 0x08064d85 in luaB_coresume (L=0x8076e90) at lbaselib.c:545
#9 0x08050ac2 in luaD_precall (L=0x8076e90, func=0x8077818, nresults=0) at ldo.c:319
#10 0x0805d2c8 in luaV_execute (L=0x8076e90, nexeccalls=1) at lvm.c:587
#11 0x08050e11 in resume (L=0x8076e90, ud=0x8077818) at ldo.c:404
#12 0x0804fec5 in luaD_rawrunprotected (L=0x8076e90, f=0x8050d56 , ud=0x8077818) at ldo.c:116
#13 0x08050f61 in lua_resume (L=0x8076e90, nargs=1) at ldo.c:427
#14 0x08064c97 in auxresume (L=0x8072008, co=0x8076e90, narg=1) at lbaselib.c:526
#15 0x08064d85 in luaB_coresume (L=0x8072008) at lbaselib.c:545
#16 0x08050ac2 in luaD_precall (L=0x8072008, func=0x807226c, nresults=0) at ldo.c:319
#17 0x0805d2c8 in luaV_execute (L=0x8072008, nexeccalls=1) at lvm.c:587
#18 0x08050d22 in luaD_call (L=0x8072008, func=0x8072260, nResults=-1) at ldo.c:377
#19 0x0804dbe7 in f_call (L=0x8072008, ud=0xbfaf3574) at lapi.c:801
#20 0x0804fec5 in luaD_rawrunprotected (L=0x8072008, f=0x804dbbd , ud=0xbfaf3574) at ldo.c:116
#21 0x0805107e in luaD_pcall (L=0x8072008, func=0x804dbbd , u=0xbfaf3574, old_top=48, ef=36) at ldo.c:463
#22 0x0804dc7b in lua_pcall (L=0x8072008, nargs=0, nresults=-1, errfunc=2) at lapi.c:822
#23 0x0804b3b9 in docall (L=0x8072008, narg=0, clear=0) at lua.c:102
#24 0x0804bc16 in handle_script (L=0x8072008, argv=0xbfaf38e4, n=1) at lua.c:250
#25 0x0804c0e5 in pmain (L=0x8072008) at lua.c:362
#26 0x08050ac2 in luaD_precall (L=0x8072008, func=0x807223c, nresults=0) at ldo.c:319
#27 0x08050d0b in luaD_call (L=0x8072008, func=0x807223c, nResults=0) at ldo.c:376
#28 0x0804dd5e in f_Ccall (L=0x8072008, ud=0xbfaf37ec) at lapi.c:847
#29 0x0804fec5 in luaD_rawrunprotected (L=0x8072008, f=0x804dcab , ud=0xbfaf37ec) at ldo.c:116
#30 0x0805107e in luaD_pcall (L=0x8072008, func=0x804dcab , u=0xbfaf37ec, old_top=12, ef=0) at ldo.c:463
#31 0x0804ddac in lua_cpcall (L=0x8072008, func=0x804bf61 , ud=0xbfaf3830) at lapi.c:857
#32 0x0804c1b2 in main (argc=2, argv=0xbfaf38e4) at lua.c:387
lua的coroutine是通过c的堆栈来保存调用上下文的 多少个coroutine嵌套 就有多少个luaV_execute, 而lua的state保存存在lua_State,也就是thread对象中。
顺着调用链 再看下源代码很容易就明白了。
阅读(3393) | 评论(0) | 转发(0) |