加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sync.lua 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
赵建辉 提交于 2023-10-08 11:59 . perf(sync): reduce memory usage
-- 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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化