代码拉取完成,页面将自动刷新
同步操作将从 赵建辉/lua-eco 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <zhaojh329@gmail.com>
local M = {}
local cond_methods = {}
-- waiting to be awakened
function cond_methods:wait(timeout)
local watchers = self.watchers
local w = eco.watcher(eco.ASYNC)
watchers[#watchers + 1] = w
return w:wait(timeout)
end
-- wakes one coroutine waiting on the cond, if there is any.
function cond_methods:signal()
local watchers = self.watchers
if #watchers > 0 then
watchers[1]:send()
table.remove(watchers, 1)
end
end
-- wakes all coroutines waiting on the cond
function cond_methods:broadcast()
for _, w in ipairs(self.watchers) do
w:send()
end
self.watchers = {}
end
local cond_mt = { __index = cond_methods }
-- implements a condition variable, a rendezvous point for coroutines waiting for or announcing the occurrence of an event.
function M.cond()
return setmetatable({ watchers = {} }, cond_mt)
end
local waitgroup_methods = {}
function waitgroup_methods:add(delta)
self.counter = self.counter + delta
end
function waitgroup_methods:done()
local counter = self.counter
counter = counter - 1
if counter < 0 then
error('negative wait group counter')
end
self.counter = counter
if counter == 0 then
self.cond:broadcast()
end
end
function waitgroup_methods:wait(timeout)
if self.counter == 0 then
return true
end
return self.cond:wait(timeout)
end
local waitgroup_mt = { __index = waitgroup_methods }
--[[
A waitgroup waits for a collection of coroutines to finish.
One coroutine calls add to set the number of coroutines to wait for.
Then each of the coroutines runs and calls done when finished.
At the same time, wait can be used to block until all coroutines have finished.
--]]
function M.waitgroup()
return setmetatable({
counter = 0,
cond = M.cond()
}, waitgroup_mt)
end
return M
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。