aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2021-11-29 14:11:24 +0000
committerMatthew Wild <mwild1@gmail.com>2021-11-29 14:11:24 +0000
commitcfef2c6ef6609d3d917edb144123295f64187545 (patch)
tree704f2854d4ec07e338f2feff58968bf20996e025 /util
parentebd4ea2bb3743aa6ee8442fec4bac465e0127c87 (diff)
downloadprosody-cfef2c6ef6609d3d917edb144123295f64187545.tar.gz
prosody-cfef2c6ef6609d3d917edb144123295f64187545.zip
util.async: Add sleep() method with configurable scheduling backend
No scheduler set by default, so it will error (we plan to initialize it in util.startup). We wanted to avoid a hard dependency on util.timer (which in turn depends on network backends, etc.), and we didn't add timer.sleep() because we didn't want to add a hard dependency on util.async for things that don't need it.
Diffstat (limited to 'util')
-rw-r--r--util/async.lua15
1 files changed, 15 insertions, 0 deletions
diff --git a/util/async.lua b/util/async.lua
index 341128d2..551a5e5c 100644
--- a/util/async.lua
+++ b/util/async.lua
@@ -11,6 +11,9 @@ local function checkthread()
return thread;
end
+-- Configurable functions
+local schedule_task = nil; -- schedule_task(seconds, callback)
+
local function runner_from_thread(thread)
local level = 0;
-- Find the 'level' of the top-most function (0 == current level, 1 == caller, ...)
@@ -118,6 +121,15 @@ local function guarder()
end;
end
+local function sleep(seconds)
+ if not schedule_task then
+ error("async.sleep() is not available - configure schedule function");
+ end
+ local wait, done = waiter();
+ schedule_task(seconds, done);
+ wait();
+end
+
local runner_mt = {};
runner_mt.__index = runner_mt;
@@ -272,4 +284,7 @@ return {
runner = runner;
wait = wait_for; -- COMPAT w/trunk pre-0.12
wait_for = wait_for;
+ sleep = sleep;
+
+ set_schedule_function = function (new_schedule_function) schedule_task = new_schedule_function; end;
};