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

View File

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

View File

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

View File

@ -2,6 +2,9 @@
-- Timer Callback Dispatcher -- Timer Callback Dispatcher
-- --
local log = require("scada-common.log")
local util = require("scada-common.util")
local tcallbackdsp = {} local tcallbackdsp = {}
local registry = {} local registry = {}
@ -14,6 +17,27 @@ function tcallbackdsp.dispatch(time, f)
registry[os.startTimer(time)] = { callback = f } registry[os.startTimer(time)] = { callback = f }
end 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 -- lookup a timer event and execute the callback if found
---@param event integer timer event timer ID ---@param event integer timer event timer ID
function tcallbackdsp.handle(event) function tcallbackdsp.handle(event)