mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
added util adaptive_delay to replace repeated code
This commit is contained in:
parent
f14d715070
commit
aff166e27d
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user