From 4dec20e4ff11b61be57a6cfbdb289327d9e1eb7d Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 25 Feb 2025 23:43:41 +0000 Subject: the Restructuring --- util/queue.lua | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 util/queue.lua (limited to 'util/queue.lua') diff --git a/util/queue.lua b/util/queue.lua new file mode 100644 index 0000000..8c9c373 --- /dev/null +++ b/util/queue.lua @@ -0,0 +1,39 @@ +local cqueues = require'cqueues' +local condition = require'cqueues.condition' +local cqaux = require'cqueues.auxlib' + +local Queue = {} + +function Queue.make() + return setmetatable({ + items = {}, + cv = condition.new(), + }, {__index=Queue}) +end + +function Queue.enqueue(self, ...) + local item = table.pack(...) + table.insert(self.items, item) + if #self.items > 128 then + print('warning: queue is quite big') + end + self.cv:signal() +end + +function Queue.iter(self) + return cqaux.wrap(function() + while true do + while #self.items > 0 do + local items = self.items + self.items = {} -- the old switcheroo + for _, item in ipairs(items) do + coroutine.yield(table.unpack(item, 1, item.n)) + end + end + self.cv:wait() + end + end) +end + + +return Queue -- cgit v1.2.3