summaryrefslogtreecommitdiff
path: root/queue.lua
diff options
context:
space:
mode:
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