summaryrefslogtreecommitdiff
path: root/queue.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2025-02-27 23:23:57 +0000
committerubq323 <ubq323@ubq323.website>2025-02-27 23:23:57 +0000
commit5dea96ad915692e9abbc0620930756c51c256179 (patch)
tree881d7680a521474f262bac262f382e778c1fc191 /queue.lua
parentec4053dea479c3c85d57c447e262a90addcce5c3 (diff)
restructure, refactor, a bit
Diffstat (limited to 'queue.lua')
-rw-r--r--queue.lua43
1 files changed, 43 insertions, 0 deletions
diff --git a/queue.lua b/queue.lua
new file mode 100644
index 0000000..5a30646
--- /dev/null
+++ b/queue.lua
@@ -0,0 +1,43 @@
+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(...)
+ print('q',...)
+ 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
+ (function(...)
+ print('p',...)
+ coroutine.yield(...)
+ end)(table.unpack(item, 1, item.n))
+ end
+ end
+ self.cv:wait()
+ end
+ end)
+end
+
+
+return Queue