about summary refs log tree commit diff
path: root/fifo.lua
blob: 03a7af7b09e391753bc0eccadaacc90acf82cf3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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