about summary refs log tree commit diff
path: root/fifo.lua
diff options
context:
space:
mode:
authorequa <equaa@protonmail.com>2022-03-07 19:14:15 +0000
committerequa <equaa@protonmail.com>2022-03-07 19:14:15 +0000
commit224e8f43c50a513bae78af2152c12c0a5f9564f9 (patch)
tree7748d6ffeba50a43d3a7f55a3c85983d07969776 /fifo.lua
initial commit
Diffstat (limited to 'fifo.lua')
-rw-r--r--fifo.lua32
1 files changed, 32 insertions, 0 deletions
diff --git a/fifo.lua b/fifo.lua
new file mode 100644
index 0000000..03a7af7
--- /dev/null
+++ b/fifo.lua
@@ -0,0 +1,32 @@
+local condition = require("cqueues.condition")
+
+local fifo = {}
+
+function fifo.new()
+	return setmetatable({ cond = condition.new() }, { __index = fifo })
+end
+
+function fifo.signal(f)
+	f.cond:signal()
+end
+
+function fifo.get(f)
+	if not f.head then
+		f.cond:wait()
+	end
+
+	local data = f.head.data
+	f.head = f.head.tail
+	return data
+end
+
+function fifo.put(f, data)
+	f.head = { data = data, tail = f.head }
+	fifo.signal(f)
+end
+
+function fifo.iter(f)
+	return function () return fifo.get(f) end
+end
+
+return fifo