用 Lua 并发功能模拟 Golang 的 go defer 编程模式 - 封装 defer
最编程
2024-05-02 17:50:42
...
defer 的特点有以下:
- 协程正常退出能被执行
- 协程异常退出能被执行
- 同个协程内可以多次调用 defer
- defer 被执行时,按出栈顺序被执行
defer 多次执行
首先定义 defer 函数,让它具备能多次被调用:
function defer(_co_wrap, h)
table.insert(_co_wrap.defer_handlers, h)
end
因为要对 defer 的函数句柄做保持,以便退出时执行。包裹了下 co 对象:
---@class co_wrap
---@field co thread
---@field defer_handlers fun(_co_error:co_error)[]
同时定义下让 defer 的函数知道是否有错误的对象:
---@class co_error
---@field ok boolean
defer 被执行时,按出栈顺序被执行
function invoke_defer_handlers(_co_wrap, _co_error)
for i=#_co_wrap.defer_handlers, 1, -1 do
local h = _co_wrap.defer_handlers[i]
xpcall(h, function(err) print(err) end, _co_error)
end
end
协程异常时,能被执行
Lua 协程异常,通过 coroutine.resume
捕获,并返回错误信息
因此主要封装下 coroutine.resume
:
function coroutine_resume(_co_wrap, ...)
local ok, errmsg = coroutine.resume(_co_wrap.co, ...)
if not ok then
invoke_defer_handlers(_co_wrap, {ok=false}) -- 异常退出
end
end
协程正常退出时,能被执行
function go(_co_task)
local co = coroutine.create(function(_co_wrap)
_co_task(_co_wrap)
invoke_defer_handlers(_co_wrap, {ok=true}) -- 正常退出
end)
local cowrap = { co = co, defer_handlers = {} } ---@type co_wrap
coroutine_resume(cowrap, cowrap) -- 初创建的协程是暂停的,手动触发执行
end
以上就可以在 Lua 中完全 Golang 的方式编写协程代码了