aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2017-11-21 21:48:14 +0100
committerKim Alvefur <zash@zash.se>2017-11-21 21:48:14 +0100
commitffc6fcf5d32aecc207c9260d86851150a4be48fc (patch)
tree842de57db0b2d2db49cb1990c03710ad81807717 /util
parent38c157896a7541d903cdf1c09c0cafa35c5d7820 (diff)
downloadprosody-ffc6fcf5d32aecc207c9260d86851150a4be48fc.tar.gz
prosody-ffc6fcf5d32aecc207c9260d86851150a4be48fc.zip
util.async: Factor out thread check into a function
Diffstat (limited to 'util')
-rw-r--r--util/async.lua18
1 files changed, 10 insertions, 8 deletions
diff --git a/util/async.lua b/util/async.lua
index 2aff2f2d..17a3bae1 100644
--- a/util/async.lua
+++ b/util/async.lua
@@ -1,5 +1,13 @@
local log = require "util.logger".init("util.async");
+local function checkthread()
+ local thread = coroutine.running();
+ if not thread then
+ error("Not running in an async context, see https://prosody.im/doc/developers/util/async");
+ end
+ return thread;
+end
+
local function runner_continue(thread)
-- ASSUMPTION: runner is in 'waiting' state (but we don't have the runner to know for sure)
if coroutine.status(thread) ~= "suspended" then -- This should suffice
@@ -25,10 +33,7 @@ local function runner_continue(thread)
end
local function waiter(num)
- local thread = coroutine.running();
- if not thread then
- error("Not running in an async context, see https://prosody.im/doc/developers/util/async");
- end
+ local thread = checkthread();
num = num or 1;
local waiting;
return function ()
@@ -48,10 +53,7 @@ end
local function guarder()
local guards = {};
return function (id, func)
- local thread = coroutine.running();
- if not thread then
- error("Not running in an async context, see https://prosody.im/doc/developers/util/async");
- end
+ local thread = checkthread();
local guard = guards[id];
if not guard then
guard = {};