#108 resolved TCD race condition

This commit is contained in:
Mikayla Fischler 2022-10-21 15:15:56 -04:00
parent 93286174d4
commit d202a49011
4 changed files with 28 additions and 4 deletions

View File

@ -16,7 +16,7 @@ local config = require("coordinator.config")
local coordinator = require("coordinator.coordinator")
local renderer = require("coordinator.renderer")
local COORDINATOR_VERSION = "alpha-v0.5.6"
local COORDINATOR_VERSION = "alpha-v0.5.7"
local print = util.print
local println = util.println
@ -146,7 +146,6 @@ end
---@return boolean ui_ok started ok
local function init_start_ui()
log_graphics("starting UI...")
-- util.psleep(3)
local draw_start = util.time_ms()

View File

@ -85,7 +85,7 @@ local function waiting(args)
if state >= 12 then state = 0 end
if run_animation then
tcd.dispatch(0.5, animate)
tcd.dispatch_unique(0.5, animate)
end
end

View File

@ -38,13 +38,14 @@ local function callback_250ms()
callback_counter = callback_counter + 1
tcd.dispatch(0.25, callback_250ms)
tcd.dispatch_unique(0.25, callback_250ms)
end
end
-- start the flasher periodic
function flasher.init()
active = true
callback_counter = 0
registry = { {}, {}, {} }
callback_250ms()
end

View File

@ -2,6 +2,9 @@
-- Timer Callback Dispatcher
--
local log = require("scada-common.log")
local util = require("scada-common.util")
local tcallbackdsp = {}
local registry = {}
@ -14,6 +17,27 @@ function tcallbackdsp.dispatch(time, f)
registry[os.startTimer(time)] = { callback = f }
end
-- request a function to be called after the specified time, aborting any registered instances of that function reference
---@param time number seconds
---@param f function callback function
function tcallbackdsp.dispatch_unique(time, f)
-- ignore if already registered
for timer, entry in pairs(registry) do
if entry.callback == f then
-- found an instance of this function reference, abort it
log.debug(util.c("TCD: aborting duplicate timer callback (timer: ", timer, ", function: ", f, ")"))
-- cancel event and remove from registry (even if it fires it won't call)
---@diagnostic disable-next-line: undefined-field
os.cancelTimer(timer)
registry[timer] = nil
end
end
---@diagnostic disable-next-line: undefined-field
registry[os.startTimer(time)] = { callback = f }
end
-- lookup a timer event and execute the callback if found
---@param event integer timer event timer ID
function tcallbackdsp.handle(event)