diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-11-21 16:37:08 +0100 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-11-21 16:37:08 +0100 |
commit | 40beef755f0c47691b5cd5a79e76fdd361320aa8 (patch) | |
tree | 023dc702cab44be6b500dc0f1ef549090498e9b6 /util.lua | |
parent | d8c45339fcef6609dcc423480dcb70986237bf61 (diff) | |
download | lua_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.lua | 21 |
1 files changed, 19 insertions, 2 deletions
@@ -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 |