🔧 build: use to-be-close syntax add RAII style queue lock
parent
90b0695224
commit
e5f704c825
@ -0,0 +1,39 @@
|
||||
local skynet = require "skynet"
|
||||
local coroutine = coroutine
|
||||
local assert = assert
|
||||
local tinsert = table.insert
|
||||
local tremove = table.remove
|
||||
local setmetatable = setmetatable
|
||||
|
||||
function skynet.queue2()
|
||||
local current_thread
|
||||
local ref = 0
|
||||
local thread_queue = {}
|
||||
|
||||
local scope_lock = setmetatable({}, {
|
||||
__close = function()
|
||||
ref = ref - 1
|
||||
if ref == 0 then
|
||||
current_thread = tremove(thread_queue, 1)
|
||||
if current_thread then
|
||||
skynet.wakeup(current_thread)
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
return function()
|
||||
local thread = coroutine.running()
|
||||
if current_thread and current_thread ~= thread then
|
||||
tinsert(thread_queue, thread)
|
||||
skynet.wait()
|
||||
assert(ref == 0) -- current_thread == thread
|
||||
end
|
||||
current_thread = thread
|
||||
|
||||
ref = ref + 1
|
||||
return scope_lock
|
||||
end
|
||||
end
|
||||
|
||||
return skynet.queue2
|
||||
Loading…
Reference in New Issue