]> git.lizzy.rs Git - lua_async.git/commitdiff
Sleep for unused tick time & optional realtime
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 21 Nov 2021 15:37:08 +0000 (16:37 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 21 Nov 2021 15:37:08 +0000 (16:37 +0100)
- Optional luasocket dependency for realtime measurements (instead of CPU time)
- lua_async.run() will wait for the time to be ready

events.lua
init.lua
limiting.lua
util.lua

index ee723e817e59aab9983dd9ab50fcba18c93dbac6..479697321cbe1ecf3539d53a65fa646f49de1c65 100644 (file)
@@ -9,7 +9,7 @@ function Event(type, data)
                type = type,
                data = data,
                defaultPrevented = false,
-               timeStamp = os.clock(),
+               timeStamp = lua_async.clock(),
        }, {__index = EventPrototype})
 end
 
index 07bea3e9fa7ed97792cacb0a047c1a080a192c7f..368ffa9d5293f865f64344f73223f28bde9a2c79 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -1,5 +1,13 @@
 lua_async = {}
 
+if rawget(_G, "require") then
+       lua_async.socket = require("socket")
+end
+
+function lua_async.clock()
+       return lua_async.socket and lua_async.socket.gettime() or os.clock()
+end
+
 function lua_async.step(dtime)
        -- timers phase
        lua_async.timeouts.step(dtime)
index d5df4486897bdc5a992257f89c1220ac08de048f..394006de1d37a273ab8ed5fb1275d65df31745a3 100644 (file)
@@ -13,7 +13,7 @@ function lua_async.set_limit(ms)
 
        lua_async.limiting.pool[co] = {
                limit = limit,
-               next_yield = os.clock() + limit,
+               next_yield = lua_async.clock() + limit,
        }
 end
 
@@ -26,9 +26,9 @@ function lua_async.check_limit()
        local co = assert(coroutine.running(), "check_limit called outside of an async function")
        local limit = lua_async.limiting.pool[co]
 
-       if limit and os.clock() >= limit.next_yield then
+       if limit and lua_async.clock() >= limit.next_yield then
                lua_async.yield()
-               limit.next_yield = os.clock() + limit.limit
+               limit.next_yield = lua_async.clock() + limit.limit
                return true
        end
 
index 7061c0619f379e189766f782ec57a1c9f63ab1eb..dbef0b5275c6bace9051442fb88cb067ad90c1f3 100644 (file)
--- 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