From 6b4ed1a30aa2a8b7e07ba7397564f41af53565ba Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 29 Sep 2019 18:42:35 +0200 Subject: util.async: Add function for waiting on promises and unpacking the results --- util/async.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'util/async.lua') diff --git a/util/async.lua b/util/async.lua index 20397785..d338071f 100644 --- a/util/async.lua +++ b/util/async.lua @@ -246,9 +246,25 @@ local function ready() return pcall(checkthread); end +local function wait(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; }; -- cgit v1.2.3 From 9691969c448cd101a3ac55e9a833097fcb879065 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 13 Jun 2020 08:01:57 +0100 Subject: util.async: Rename wait -> wait_for (w/compat) Agreed this name is clearer and wait_for(thing) reads well in code. --- util/async.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'util/async.lua') diff --git a/util/async.lua b/util/async.lua index d338071f..16dce200 100644 --- a/util/async.lua +++ b/util/async.lua @@ -246,7 +246,7 @@ local function ready() return pcall(checkthread); end -local function wait(promise) +local function wait_for(promise) local async_wait, async_done = waiter(); local ret, err = nil, nil; promise:next( @@ -266,5 +266,6 @@ return { waiter = waiter; guarder = guarder; runner = runner; - wait = wait; + wait = wait_for; -- COMPAT w/trunk pre-0.12 + wait_for = wait_for; }; -- cgit v1.2.3 From 3c53df784751cef688a5ff41db26d4d11b7da2f7 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 14 Jun 2020 08:49:32 +0100 Subject: util.async: Call coroutine.close() on dead threads (Lua 5.4) --- util/async.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'util/async.lua') diff --git a/util/async.lua b/util/async.lua index 16dce200..24378d8c 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 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); -- cgit v1.2.3 From 020454b7a3afe41edcc365f593bffc97c827e962 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 14 Jun 2020 09:40:08 +0100 Subject: util.async: Don't attempt to close thread if not created yet --- util/async.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util/async.lua') diff --git a/util/async.lua b/util/async.lua index 24378d8c..341128d2 100644 --- a/util/async.lua +++ b/util/async.lua @@ -160,7 +160,7 @@ 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 coroutine.close then + if thread and coroutine.close then coroutine.close(thread); end self:log("debug", "creating new coroutine"); -- cgit v1.2.3