add coroutine close for skynetco (#1392)

Co-authored-by: jietao.cjt <jietao.cjt@alibaba-inc.com>
This commit is contained in:
t0350
2021-04-29 14:40:37 +08:00
committed by GitHub
parent f61a27ac6b
commit 1e6781bd41
2 changed files with 11 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ local coroutine_resume = coroutine.resume
local coroutine_yield = coroutine.yield local coroutine_yield = coroutine.yield
local coroutine_status = coroutine.status local coroutine_status = coroutine.status
local coroutine_running = coroutine.running local coroutine_running = coroutine.running
local coroutine_close = coroutine.close
local select = select local select = select
local skynetco = {} local skynetco = {}
@@ -15,6 +16,9 @@ skynetco.running = coroutine.running
skynetco.status = coroutine.status skynetco.status = coroutine.status
local skynet_coroutines = setmetatable({}, { __mode = "kv" }) local skynet_coroutines = setmetatable({}, { __mode = "kv" })
-- true : skynet coroutine
-- false : skynet suspend
-- nil : exit
function skynetco.create(f) function skynetco.create(f)
local co = coroutine.create(f) local co = coroutine.create(f)
@@ -84,7 +88,7 @@ do -- begin skynetco.resume
end -- end of skynetco.resume end -- end of skynetco.resume
function skynetco.status(co) function skynetco.status(co)
local status = coroutine.status(co) local status = coroutine_status(co)
if status == "suspended" then if status == "suspended" then
if skynet_coroutines[co] == false then if skynet_coroutines[co] == false then
return "blocked" return "blocked"
@@ -121,4 +125,9 @@ end
end -- end of skynetco.wrap end -- end of skynetco.wrap
function skynetco.close(co)
skynet_coroutines[co] = nil
return coroutine_close(co)
end
return skynetco return skynetco

View File

@@ -50,4 +50,5 @@ skynet.start(function()
print("main step", f()) print("main step", f())
print("main step", f()) print("main step", f())
-- print("main thread time:", profile.stop()) -- print("main thread time:", profile.stop())
print("close", coroutine.close(coroutine.create(main)))
end) end)