mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#108 resolved TCD race condition
This commit is contained in:
parent
93286174d4
commit
d202a49011
@ -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()
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user