From 0d8767c62f75e6364800102a8ebf367c8d87f495 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Tue, 17 Jan 2017 01:42:11 -0500 Subject: [PATCH] lamp/linux-draw: properly emulate timer callbacks --- linux-draw.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/linux-draw.lua b/linux-draw.lua index 24fcabe..4b6e452 100644 --- a/linux-draw.lua +++ b/linux-draw.lua @@ -19,10 +19,21 @@ function printerr(...) io.stderr:write(s) end --- Emulate tq with facilities available thanks to cqueues +-- Emulate tq with facilities available thanks to cqueues. +-- +-- It might be OK that we don't (and can't) cancel the coroutines spawned +-- by arm because the callback is observably idempotent, but it keeps extras +-- laying around, and they might accumulate without bound. So we have a +-- notion of which callback was registered last and the others turn into +-- NOPs by comparing the current callback against the value we closed over. tq = dofile("tq/tq.lua")(nil) +tq.__emu_lastcb = 0 tq.now = function() return cq.monotime() * 1000000 end -tq.arm = function(self,fn,t) cqc:wrap(function() cq.poll(t/1000) ; fn() end) end +tq.arm = function(self,fn,t) + local cbix = tq.__emu_lastcb + 1 + tq.__emu_lastcb = cbix + cqc:wrap(function() cq.poll(t/1000) ; if tq.__emu_lastcb == cbix then fn() end end) +end -- how backwards is this!? We are using tq as faked above for tmr support -- since it's not obvious to me how to remove pending events in cqueues. -- 2.50.1