From d202a490113a968b50669912bd5a871dda94c268 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Fri, 21 Oct 2022 15:15:56 -0400 Subject: [PATCH] #108 resolved TCD race condition --- coordinator/startup.lua | 3 +-- graphics/elements/animations/waiting.lua | 2 +- graphics/flasher.lua | 3 ++- scada-common/tcallbackdsp.lua | 24 ++++++++++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/coordinator/startup.lua b/coordinator/startup.lua index fac7ab8..0c4e579 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -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() diff --git a/graphics/elements/animations/waiting.lua b/graphics/elements/animations/waiting.lua index 5c03d09..2b08092 100644 --- a/graphics/elements/animations/waiting.lua +++ b/graphics/elements/animations/waiting.lua @@ -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 diff --git a/graphics/flasher.lua b/graphics/flasher.lua index 97afd83..02f0dcd 100644 --- a/graphics/flasher.lua +++ b/graphics/flasher.lua @@ -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 diff --git a/scada-common/tcallbackdsp.lua b/scada-common/tcallbackdsp.lua index 65b8ec9..3371c98 100644 --- a/scada-common/tcallbackdsp.lua +++ b/scada-common/tcallbackdsp.lua @@ -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)