Skip to content

Commit 44e4c4a

Browse files
HHHartmannmarcelstoer
authored andcommitted
Fifo fixes (#3638)
1 parent 2e6f0c2 commit 44e4c4a

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

lua_modules/fifo/fifo.lua

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- A generic fifo module. See docs/lua-modules/fifo.md for use examples.
22

3-
local tr, ti = table.remove, table.insert
3+
local tableRemove, tableInsert = table.remove, table.insert
44

55
-- Remove an element and pass it to k, together with a boolean indicating that
66
-- this is the last element in the queue; if that returns a value, leave that
@@ -17,29 +17,33 @@ local tr, ti = table.remove, table.insert
1717
--
1818
-- Returns 'true' if the queue contained at least one non-phantom entry,
1919
-- 'false' otherwise.
20-
local function dequeue(q,k)
21-
if #q > 0
22-
then
23-
local new, again = k(q[1], #q == 1)
24-
if new == nil
25-
then tr(q,1)
26-
if again then return dequeue(q, k) end -- note tail call
27-
else q[1] = new
28-
end
29-
return true
30-
else q._go = true ; return false
20+
local function dequeue(q, k)
21+
if #q > 0 then
22+
local new, again = k(q[1], #q == 1)
23+
if new == nil then
24+
tableRemove(q, 1)
25+
else
26+
q[1] = new
27+
end
28+
if again then
29+
return dequeue(q, k)
30+
end -- note tail call
31+
return true
32+
else
33+
q._go = true
34+
return false
3135
end
3236
end
3337

3438
-- Queue a on queue q and dequeue with `k` if the fifo had previously emptied.
3539
local function queue(q,a,k)
36-
ti(q,a)
40+
tableInsert(q,a)
3741
if k ~= nil and q._go then q._go = false; dequeue(q, k) end
3842
end
3943

4044
-- return a table containing just the FIFO constructor
4145
return {
4246
['new'] = function()
43-
return { ['_go'] = true ; ['queue'] = queue ; ['dequeue'] = dequeue }
44-
end
47+
return { ['_go'] = true ; ['queue'] = queue ; ['dequeue'] = dequeue }
48+
end
4549
}

lua_modules/fifo/fifosock.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ local function wrap(sock)
5858
-- Either that was a function or we've hit our coalescing limit or
5959
-- we didn't ship above. Ship now, if there's something to ship.
6060
if s ~= nil then
61-
if sslan == 0 then sock:send(s) else sock:send(ssla .. s) end
61+
if sslan == 0 then
62+
if #s > 0 then
63+
sock:send(s)
64+
else
65+
return ns or nil, true
66+
end
67+
else
68+
sock:send(ssla .. s)
69+
end
6270
ssla, sslan = nil, 0; gc()
6371
return ns or nil, false
6472
elseif sslan ~= 0 then

0 commit comments

Comments
 (0)