1 local log = require "util.logger".init("util.async");
3 local function runner_continue(thread)
4 -- ASSUMPTION: runner is in 'waiting' state (but we don't have the runner to know for sure)
5 if coroutine.status(thread) ~= "suspended" then -- This should suffice
8 local ok, state, runner = coroutine.resume(thread);
11 while debug.getinfo(thread, level, "") do level = level + 1; end
12 ok, runner = debug.getlocal(thread, level-1, 1);
13 local error_handler = runner.watchers.error;
14 if error_handler then error_handler(runner, debug.traceback(thread, state)); end
15 elseif state == "ready" then
16 -- If state is 'ready', it is our responsibility to update runner.state from 'waiting'.
17 -- We also have to :run(), because the queue might have further items that will not be
18 -- processed otherwise. FIXME: It's probably best to do this in a nexttick (0 timer).
19 runner.state = "ready";
25 local function waiter(num)
26 local thread = coroutine.running();
28 error("Not running in an async context, see https://prosody.im/doc/developers/async");
33 if num == 0 then return; end -- already done
35 coroutine.yield("wait");
38 if num == 0 and waiting then
39 runner_continue(thread);
41 error("done() called too many times");
46 local function guarder()
48 return function (id, func)
49 local thread = coroutine.running();
51 error("Not running in an async context, see https://prosody.im/doc/developers/async");
53 local guard = guards[id];
57 log("debug", "New guard!");
59 table.insert(guard, thread);
60 log("debug", "Guarded. %d threads waiting.", #guard)
61 coroutine.yield("wait");
64 local next_waiting = table.remove(guard, 1);
66 log("debug", "guard: Executing next waiting thread (%d left)", #guard)
67 runner_continue(next_waiting);
69 log("debug", "Guard off duty.")
83 runner_mt.__index = runner_mt;
85 local function runner_create_thread(func, self)
86 local thread = coroutine.create(function (self)
88 func(coroutine.yield("ready", self));
91 assert(coroutine.resume(thread, self)); -- Start it up, it will return instantly to wait for the first input
95 local empty_watchers = {};
96 local function runner(func, watchers, data)
97 return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready",
98 queue = {}, watchers = watchers or empty_watchers, data = data }
102 function runner_mt:run(input)
104 table.insert(self.queue, input);
106 if self.state ~= "ready" then
107 return true, self.state, #self.queue;
110 local q, thread = self.queue, self.thread;
111 if not thread or coroutine.status(thread) == "dead" then
112 thread = runner_create_thread(self.func, self);
113 self.thread = thread;
116 local n, state, err = #q, self.state, nil;
117 self.state = "running";
118 while n > 0 and state == "ready" do
122 local ok, new_state = coroutine.resume(thread, input);
124 consumed, state, err = i, "ready", debug.traceback(thread, new_state);
127 elseif new_state == "wait" then
128 consumed, state = i, "waiting";
132 if not consumed then consumed = n; end
133 if q[n+1] ~= nil then
137 q[i] = q[consumed+i];
142 if err or state ~= self.notified_state then
146 self.notified_state = state;
148 local handler = self.watchers[state];
149 if handler then handler(self, err); end
151 return true, state, n;
154 function runner_mt:enqueue(input)
155 table.insert(self.queue, input);
158 return { waiter = waiter, guarder = guarder, runner = runner };