From 40beef755f0c47691b5cd5a79e76fdd361320aa8 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sun, 21 Nov 2021 16:37:08 +0100 Subject: Sleep for unused tick time & optional realtime - Optional luasocket dependency for realtime measurements (instead of CPU time) - lua_async.run() will wait for the time to be ready --- util.lua | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'util.lua') diff --git a/util.lua b/util.lua index 7061c06..dbef0b5 100644 --- a/util.lua +++ b/util.lua @@ -27,13 +27,30 @@ function lua_async.resume(co) end function lua_async.run() - local last_time = os.clock() + assert(lua_async.socket) + local last_time = lua_async.clock() while true do - local current_time = os.clock() + local current_time = lua_async.clock() local dtime = current_time - last_time last_time = current_time lua_async.step(dtime) + + local next = math.huge + + for _, timeout in pairs(lua_async.timeouts.pool) + next = math.min(next, timeout.time_left) + end + + for _, interval in pairs(lua_async.intervals.pool) + next = math.min(next, interval.time_left) + end + + if next == math.huge then + return + end + + lua_async.socket.sleep(next) end end -- cgit v1.2.3