aboutsummaryrefslogtreecommitdiffstats
path: root/util/async.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2020-09-30 09:50:33 +0100
committerMatthew Wild <mwild1@gmail.com>2020-09-30 09:50:33 +0100
commit785c20f6ee7e61a5a91a8b6259623bc2a2bbffaa (patch)
tree3a37db7b805b1555d08cc61c7938cf512ff35ce0 /util/async.lua
parente55d037cdf89efac99c6144b381c9fa880f7fb93 (diff)
parentb80ff2ae4f86aa26e055890a8284b55170ef2056 (diff)
downloadprosody-785c20f6ee7e61a5a91a8b6259623bc2a2bbffaa.tar.gz
prosody-785c20f6ee7e61a5a91a8b6259623bc2a2bbffaa.zip
Merge 0.11->trunk
Diffstat (limited to 'util/async.lua')
-rw-r--r--util/async.lua23
1 files changed, 22 insertions, 1 deletions
diff --git a/util/async.lua b/util/async.lua
index 20397785..341128d2 100644
--- a/util/async.lua
+++ b/util/async.lua
@@ -53,7 +53,7 @@ local function runner_continue(thread)
return false;
end
call_watcher(runner, "error", debug.traceback(thread, err));
- runner.state, runner.thread = "ready", nil;
+ runner.state = "ready";
return runner:run();
elseif state == "ready" then
-- If state is 'ready', it is our responsibility to update runner.state from 'waiting'.
@@ -159,6 +159,10 @@ function runner_mt:run(input)
local q, thread = self.queue, self.thread;
if not thread or coroutine.status(thread) == "dead" then
+ --luacheck: ignore 143/coroutine
+ if thread and coroutine.close then
+ coroutine.close(thread);
+ end
self:log("debug", "creating new coroutine");
-- Create a new coroutine for this runner
thread = runner_create_thread(self.func, self);
@@ -246,9 +250,26 @@ local function ready()
return pcall(checkthread);
end
+local function wait_for(promise)
+ local async_wait, async_done = waiter();
+ local ret, err = nil, nil;
+ promise:next(
+ function (r) ret = r; end,
+ function (e) err = e; end)
+ :finally(async_done);
+ async_wait();
+ if ret then
+ return ret;
+ else
+ return nil, err;
+ end
+end
+
return {
ready = ready;
waiter = waiter;
guarder = guarder;
runner = runner;
+ wait = wait_for; -- COMPAT w/trunk pre-0.12
+ wait_for = wait_for;
};