about summary refs log tree commit diff
path: root/fifo.lua
diff options
context:
space:
mode:
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