aboutsummaryrefslogtreecommitdiff
path: root/util.lua
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2021-11-21 16:37:08 +0100
committerElias Fleckenstein <eliasfleckenstein@web.de>2021-11-21 16:37:08 +0100
commit40beef755f0c47691b5cd5a79e76fdd361320aa8 (patch)
tree023dc702cab44be6b500dc0f1ef549090498e9b6 /util.lua
parentd8c45339fcef6609dcc423480dcb70986237bf61 (diff)
downloadlua_async-40beef755f0c47691b5cd5a79e76fdd361320aa8.tar.xz
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
Diffstat (limited to 'util.lua')
-rw-r--r--util.lua21
1 files changed, 19 insertions, 2 deletions
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