diff options
author | equa <equaa@protonmail.com> | 2022-03-07 19:14:15 +0000 |
---|---|---|
committer | equa <equaa@protonmail.com> | 2022-03-07 19:14:15 +0000 |
commit | 224e8f43c50a513bae78af2152c12c0a5f9564f9 (patch) | |
tree | 7748d6ffeba50a43d3a7f55a3c85983d07969776 /fifo.lua |
initial commit
Diffstat (limited to 'fifo.lua')
-rw-r--r-- | fifo.lua | 32 |
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 |