scansion/async.lua

Sun, 30 Dec 2018 09:43:36 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 30 Dec 2018 09:43:36 +0000
changeset 164
14500a149b31
parent 18
5913cc9d1b5b
permissions
-rw-r--r--

client: Ignore timeout timer if we received a stanza

18
5913cc9d1b5b scansion.async: Better logging
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
1 local log = require "verse".new_logger("util.async");
5
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local function runner_continue(thread)
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- ASSUMPTION: runner is in 'waiting' state (but we don't have the runner to know for sure)
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 if coroutine.status(thread) ~= "suspended" then -- This should suffice
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 return false;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local ok, state, runner = coroutine.resume(thread);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if not ok then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local level = 0;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 while debug.getinfo(thread, level, "") do level = level + 1; end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 ok, runner = debug.getlocal(thread, level-1, 1);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local error_handler = runner.watchers.error;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if error_handler then error_handler(runner, debug.traceback(thread, state)); end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 elseif state == "ready" then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 -- If state is 'ready', it is our responsibility to update runner.state from 'waiting'.
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 -- We also have to :run(), because the queue might have further items that will not be
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 -- processed otherwise. FIXME: It's probably best to do this in a nexttick (0 timer).
18
5913cc9d1b5b scansion.async: Better logging
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
19 log("debug", "Runner is ready (finished) - restarting", debug.traceback());
5
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 runner.state = "ready";
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 runner:run();
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return true;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local function waiter(num)
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local thread = coroutine.running();
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if not thread then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 error("Not running in an async context, see http://prosody.im/doc/developers/async");
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 num = num or 1;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local waiting;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return function ()
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if num == 0 then return; end -- already done
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 waiting = true;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 coroutine.yield("wait");
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end, function ()
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 num = num - 1;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if num == 0 and waiting then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 runner_continue(thread);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 elseif num < 0 then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 error("done() called too many times");
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local function guarder()
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local guards = {};
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return function (id, func)
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local thread = coroutine.running();
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if not thread then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 error("Not running in an async context, see http://prosody.im/doc/developers/async");
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local guard = guards[id];
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if not guard then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 guard = {};
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 guards[id] = guard;
18
5913cc9d1b5b scansion.async: Better logging
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
58 log("debug", "New guard!");
5
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 else
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 table.insert(guard, thread);
18
5913cc9d1b5b scansion.async: Better logging
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
61 log("debug", "Guarded. %d threads waiting.", #guard)
5
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 coroutine.yield("wait");
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local function exit()
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local next_waiting = table.remove(guard, 1);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if next_waiting then
18
5913cc9d1b5b scansion.async: Better logging
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
67 log("debug", "guard: Executing next waiting thread (%d left)", #guard)
5
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 runner_continue(next_waiting);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 else
18
5913cc9d1b5b scansion.async: Better logging
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
70 log("debug", "Guard off duty.")
5
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 guards[id] = nil;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 if func then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 func();
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 exit();
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 return;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 return exit;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local runner_mt = {};
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 runner_mt.__index = runner_mt;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local function runner_create_thread(func, self)
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local thread = coroutine.create(function (self)
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 while true do
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 func(coroutine.yield("ready", self));
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 assert(coroutine.resume(thread, self)); -- Start it up, it will return instantly to wait for the first input
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 return thread;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 local empty_watchers = {};
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local function runner(func, watchers, data)
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready",
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 queue = {}, watchers = watchers or empty_watchers, data = data }
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 , runner_mt);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 function runner_mt:run(input)
18
5913cc9d1b5b scansion.async: Better logging
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
104 log("debug", "RUNNING", debug.traceback())
5
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 if input ~= nil then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 table.insert(self.queue, input);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 if self.state ~= "ready" then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 return true, self.state, #self.queue;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 local q, thread = self.queue, self.thread;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 if not thread or coroutine.status(thread) == "dead" then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 thread = runner_create_thread(self.func, self);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 self.thread = thread;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 local n, state, err = #q, self.state, nil;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 self.state = "running";
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 while n > 0 and state == "ready" do
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local consumed;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 for i = 1,n do
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 local input = q[i];
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local ok, new_state = coroutine.resume(thread, input);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 if not ok then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 consumed, state, err = i, "ready", debug.traceback(thread, new_state);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 self.thread = nil;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 break;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 elseif new_state == "wait" then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 consumed, state = i, "waiting";
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 break;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 if not consumed then consumed = n; end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 if q[n+1] ~= nil then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 n = #q;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 for i = 1, n do
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 q[i] = q[consumed+i];
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 n = #q;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 self.state = state;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 if err or state ~= self.notified_state then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if err then
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 state = "error"
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 else
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 self.notified_state = state;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 local handler = self.watchers[state];
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 if handler then handler(self, err); end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 return true, state, n;
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 function runner_mt:enqueue(input)
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 table.insert(self.queue, input);
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159
6a95814d66ce async: Add util.async from Prosody (with logging disabled for now)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 return { waiter = waiter, guarder = guarder, runner = runner };

mercurial