c81f8639b490b30905af66ac3428e70fe6eedbe0
[prosody.git] / util / async.lua
1 local log = require "util.logger".init("util.async");
2
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
6                 return false;
7         end
8         local ok, state, runner = coroutine.resume(thread);
9         if not ok then
10                 local level = 0;
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";
20                 runner:run();
21         end
22         return true;
23 end
24
25 local function waiter(num)
26         local thread = coroutine.running();
27         if not thread then
28                 error("Not running in an async context, see http://prosody.im/doc/developers/async");
29         end
30         num = num or 1;
31         local waiting;
32         return function ()
33                 if num == 0 then return; end -- already done
34                 waiting = true;
35                 coroutine.yield("wait");
36         end, function ()
37                 num = num - 1;
38                 if num == 0 and waiting then
39                         runner_continue(thread);
40                 end
41         end;
42 end
43
44 local runner_mt = {};
45 runner_mt.__index = runner_mt;
46
47 local function runner_create_thread(func, self)
48         local thread = coroutine.create(function (self)
49                 while true do
50                         func(coroutine.yield("ready", self));
51                 end
52         end);
53         assert(coroutine.resume(thread, self)); -- Start it up, it will return instantly to wait for the first input
54         return thread;
55 end
56
57 local empty_watchers = {};
58 local function runner(func, watchers, data)
59         return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready",
60                 queue = {}, watchers = watchers or empty_watchers, data = data }
61         , runner_mt);
62 end
63
64 function runner_mt:run(input)
65         if input ~= nil then
66                 table.insert(self.queue, input);
67         end
68         if self.state ~= "ready" then
69                 return true, self.state, #self.queue;
70         end
71
72         local q, thread = self.queue, self.thread;
73         if not thread or coroutine.status(thread) == "dead" then
74                 thread = runner_create_thread(self.func, self);
75                 self.thread = thread;
76         end
77
78         local n, state, err = #q, self.state, nil;
79         self.state = "running";
80         while n > 0 and state == "ready" do
81                 local consumed;
82                 for i = 1,n do
83                         local input = q[i];
84                         local ok, new_state = coroutine.resume(thread, input);
85                         if not ok then
86                                 consumed, state, err = i, "ready", debug.traceback(thread, new_state);
87                                 self.thread = nil;
88                                 break;
89                         elseif new_state == "wait" then
90                                 consumed, state = i, "waiting";
91                                 break;
92                         end
93                 end
94                 if not consumed then consumed = n; end
95                 if q[n+1] ~= nil then
96                         n = #q;
97                 end
98                 for i = 1, n do
99                         q[i] = q[consumed+i];
100                 end
101                 n = #q;
102         end
103         self.state = state;
104         if state ~= self.notified_state then
105                 self.notified_state = state;
106                 local handler = self.watchers[state];
107                 if handler then handler(self, err); end
108         end
109         return true, state, n;
110 end
111
112 function runner_mt:enqueue(input)
113         table.insert(self.queue, input);
114 end
115
116 return { waiter = waiter, runner = runner };