added util adaptive_delay to replace repeated code

This commit is contained in:
Mikayla Fischler 2022-04-27 19:06:01 -04:00
parent f14d715070
commit aff166e27d
3 changed files with 16 additions and 18 deletions

View File

@ -287,12 +287,8 @@ function thread__iss(smem)
break
end
-- delay before next check, only if >50ms since we did already yield
local sleep_for = ISS_SLEEP - (util.time() - last_update)
last_update = util.time()
if sleep_for >= 50 then
psleep(sleep_for / 1000.0)
end
-- delay before next check
last_update = util.adaptive_delay(ISS_SLEEP, last_update)
end
end
@ -344,12 +340,8 @@ function thread__comms(smem)
break
end
-- delay before next check, only if >50ms since we did already yield
local sleep_for = COMMS_SLEEP - (util.time() - last_update)
last_update = util.time()
if sleep_for >= 50 then
psleep(sleep_for / 1000.0)
end
-- delay before next check
last_update = util.adaptive_delay(COMMS_SLEEP, last_update)
end
end

View File

@ -144,12 +144,8 @@ function thread__comms(smem)
break
end
-- delay before next check, only if >50ms since we did already yield
local sleep_for = COMMS_SLEEP - (util.time() - last_update)
last_update = util.time()
if sleep_for >= 50 then
psleep(sleep_for / 1000.0)
end
-- delay before next check
last_update = util.adaptive_delay(COMMS_SLEEP, last_update)
end
end

View File

@ -50,6 +50,16 @@ function nop()
psleep(0.05)
end
-- attempt to maintain a minimum loop timing (duration of execution)
function adaptive_delay(target_timing, last_update)
local sleep_for = target_timing - (time() - last_update)
-- only if >50ms since worker loops already yield 0.05s
if sleep_for >= 50 then
psleep(sleep_for / 1000.0)
end
return time()
end
-- WATCHDOG --
-- ComputerCraft OS Timer based Watchdog