Skip to content

Commit

Permalink
balance packets
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneia committed Jul 25, 2017
1 parent 3ddb3f3 commit 86a6ee9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/apps/inter/mcp_ring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ end
function create (size, name)
assert(band(size, size-1) == 0, "size is not a power of two")
local r = shm.create(name, mcp_t(size))
r.max = size-1
r.max = size - 1
r.nwrite = r.max -- “full” until initlaized
return r
end

function init (r) -- initialization must be performed by consumer
assert(full(r) and empty(r)) -- only satisfied if uninitialized
repeat
r.packets[r.nwrite] = packet.allocate()
r.nwrite = r.nwrite - 1
until r.nwrite == 0
r.packets[r.nwrite] = packet.allocate()
end

local function NEXT (r, i)
return band(i + 1, r.max)
end
Expand All @@ -45,6 +55,7 @@ function full (r)
end

function insert (r, p)
packet.free(r.packets[r.nwrite])
r.packets[r.nwrite] = p
r.nwrite = NEXT(r, r.nwrite)
end
Expand All @@ -64,6 +75,7 @@ end

function extract (r)
local p = r.packets[r.nread]
r.packets[r.nread] = packet.allocate()
r.nread = NEXT(r, r.nread)
return p
end
Expand Down

0 comments on commit 86a6ee9

Please sign in to comment.