]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_lamp/commitdiff
lamp/linux-draw: properly emulate timer callbacks
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Tue, 17 Jan 2017 06:42:11 +0000 (01:42 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 18 Jan 2017 02:18:02 +0000 (21:18 -0500)
linux-draw.lua

index 24fcabee0d49158146c8a9b5d5e20a2e01196170..4b6e45292b55dab317e1ddffbef48f41e32e568a 100644 (file)
@@ -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.