-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcqueue_fifo.lua
More file actions
59 lines (43 loc) · 952 Bytes
/
cqueue_fifo.lua
File metadata and controls
59 lines (43 loc) · 952 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--
-- simple fifo module for cqueues
--
-- ------------------------------------------------------------------------
local condition = require "cqueues.condition"
local fifo = {}
function fifo:new()
local o = { condvar = condition.new(), count = 0 }
setmetatable(o, { __index = self })
return o
end -- fifo.new
function fifo:put(msg)
local tail = { data = msg }
if self.tail then
self.tail.next = tail
self.tail = tail
else
self.head = tail
self.tail = tail
end
self.count = self.count + 1
self:signal()
end -- fifo:put
function fifo:get()
if self.head then
local head = self.head
self.head = head.next
if not self.head then
self.tail = nil
end
assert(self.count > 0)
self.count = self.count - 1
return head.data
end
assert(self.count == 0)
end -- fifo:get
function fifo:signal()
self.condvar:signal()
end -- fifo:signal
function fifo:getcv()
return self.condvar
end -- fifo:getcv
return fifo