Software /
code /
prosody
Annotate
util/async.lua @ 7071:d9e620adfb64
Makefile: Add 'test' target that runs tests
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 10 Jan 2016 00:17:54 +0100 |
parent | 5797:a493b79cfad0 |
child | 7359:a5a080c12c96 |
rev | line source |
---|---|
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local log = require "util.logger".init("util.async"); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local function runner_continue(thread) |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
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) |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 if coroutine.status(thread) ~= "suspended" then -- This should suffice |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 return false; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local ok, state, runner = coroutine.resume(thread); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 if not ok then |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local level = 0; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 while debug.getinfo(thread, level, "") do level = level + 1; end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 ok, runner = debug.getlocal(thread, level-1, 1); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local error_handler = runner.watchers.error; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 if error_handler then error_handler(runner, debug.traceback(thread, state)); end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 elseif state == "ready" then |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 -- If state is 'ready', it is our responsibility to update runner.state from 'waiting'. |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
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 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 -- processed otherwise. FIXME: It's probably best to do this in a nexttick (0 timer). |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 runner.state = "ready"; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 runner:run(); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 return true; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
5790
959163e4d631
util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents:
5788
diff
changeset
|
25 local function waiter(num) |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local thread = coroutine.running(); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 if not thread then |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 error("Not running in an async context, see http://prosody.im/doc/developers/async"); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 num = num or 1; |
5792
aac4c6147647
util.async: waiter: Remove restriction about wait() being called before done()
Matthew Wild <mwild1@gmail.com>
parents:
5791
diff
changeset
|
31 local waiting; |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 return function () |
5792
aac4c6147647
util.async: waiter: Remove restriction about wait() being called before done()
Matthew Wild <mwild1@gmail.com>
parents:
5791
diff
changeset
|
33 if num == 0 then return; end -- already done |
aac4c6147647
util.async: waiter: Remove restriction about wait() being called before done()
Matthew Wild <mwild1@gmail.com>
parents:
5791
diff
changeset
|
34 waiting = true; |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 coroutine.yield("wait"); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 end, function () |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 num = num - 1; |
5792
aac4c6147647
util.async: waiter: Remove restriction about wait() being called before done()
Matthew Wild <mwild1@gmail.com>
parents:
5791
diff
changeset
|
38 if num == 0 and waiting then |
aac4c6147647
util.async: waiter: Remove restriction about wait() being called before done()
Matthew Wild <mwild1@gmail.com>
parents:
5791
diff
changeset
|
39 runner_continue(thread); |
5793
e8c79796ead9
util.async: waiter: Throw error if done() called too many times
Kim Alvefur <zash@zash.se>
parents:
5792
diff
changeset
|
40 elseif num < 0 then |
e8c79796ead9
util.async: waiter: Throw error if done() called too many times
Kim Alvefur <zash@zash.se>
parents:
5792
diff
changeset
|
41 error("done() called too many times"); |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 end; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
5797
a493b79cfad0
util.async: Make guarder() local
Matthew Wild <mwild1@gmail.com>
parents:
5796
diff
changeset
|
46 local function guarder() |
5796
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
47 local guards = {}; |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
48 return function (id, func) |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
49 local thread = coroutine.running(); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
50 if not thread then |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
51 error("Not running in an async context, see http://prosody.im/doc/developers/async"); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
52 end |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
53 local guard = guards[id]; |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
54 if not guard then |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
55 guard = {}; |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
56 guards[id] = guard; |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
57 log("debug", "New guard!"); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
58 else |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
59 table.insert(guard, thread); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
60 log("debug", "Guarded. %d threads waiting.", #guard) |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
61 coroutine.yield("wait"); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
62 end |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
63 local function exit() |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
64 local next_waiting = table.remove(guard, 1); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
65 if next_waiting then |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
66 log("debug", "guard: Executing next waiting thread (%d left)", #guard) |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
67 runner_continue(next_waiting); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
68 else |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
69 log("debug", "Guard off duty.") |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
70 guards[id] = nil; |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
71 end |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
72 end |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
73 if func then |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
74 func(); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
75 exit(); |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
76 return; |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
77 end |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
78 return exit; |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
79 end; |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
80 end |
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
81 |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 local runner_mt = {}; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 runner_mt.__index = runner_mt; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 local function runner_create_thread(func, self) |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 local thread = coroutine.create(function (self) |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 while true do |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 func(coroutine.yield("ready", self)); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 end); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 assert(coroutine.resume(thread, self)); -- Start it up, it will return instantly to wait for the first input |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 return thread; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 local empty_watchers = {}; |
5790
959163e4d631
util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents:
5788
diff
changeset
|
96 local function runner(func, watchers, data) |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready", |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 queue = {}, watchers = watchers or empty_watchers, data = data } |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 , runner_mt); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 function runner_mt:run(input) |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 if input ~= nil then |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 table.insert(self.queue, input); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 if self.state ~= "ready" then |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 return true, self.state, #self.queue; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 local q, thread = self.queue, self.thread; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 if not thread or coroutine.status(thread) == "dead" then |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 thread = runner_create_thread(self.func, self); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 self.thread = thread; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 local n, state, err = #q, self.state, nil; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 self.state = "running"; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 while n > 0 and state == "ready" do |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 local consumed; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 for i = 1,n do |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 local input = q[i]; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local ok, new_state = coroutine.resume(thread, input); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 if not ok then |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 consumed, state, err = i, "ready", debug.traceback(thread, new_state); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 self.thread = nil; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 break; |
5791
2c98061b6b1e
util.async: runner: Fix check for new state to recognise transition to 'waiting'
Matthew Wild <mwild1@gmail.com>
parents:
5790
diff
changeset
|
127 elseif new_state == "wait" then |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 consumed, state = i, "waiting"; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 break; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 if not consumed then consumed = n; end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 if q[n+1] ~= nil then |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 n = #q; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 for i = 1, n do |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 q[i] = q[consumed+i]; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 n = #q; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 self.state = state; |
5794
66c3ad5d29ad
util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents:
5793
diff
changeset
|
142 if err or state ~= self.notified_state then |
66c3ad5d29ad
util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents:
5793
diff
changeset
|
143 if err then |
66c3ad5d29ad
util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents:
5793
diff
changeset
|
144 state = "error" |
66c3ad5d29ad
util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents:
5793
diff
changeset
|
145 else |
66c3ad5d29ad
util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents:
5793
diff
changeset
|
146 self.notified_state = state; |
66c3ad5d29ad
util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents:
5793
diff
changeset
|
147 end |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 local handler = self.watchers[state]; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 if handler then handler(self, err); end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 return true, state, n; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 function runner_mt:enqueue(input) |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 table.insert(self.queue, input); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 |
5796
0b66cb959161
util.async: Add guarder method, to create guards to ensure only a single runner can pass through a section of code at a time
Matthew Wild <mwild1@gmail.com>
parents:
5794
diff
changeset
|
158 return { waiter = waiter, guarder = guarder, runner = runner }; |