Software /
code /
prosody
Annotate
util/async.lua @ 8616:a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 17 Mar 2018 17:28:07 +0000 |
parent | 8615:e77b37de482e |
child | 8625:08bf4df6fdb7 |
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"); |
8600
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
2 local new_id = require "util.id".short; |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
8407
f652e1ea2f69
util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents:
8237
diff
changeset
|
4 local function checkthread() |
8408
b751bee6a829
util.async: Fix thread check to work correctly in Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
8407
diff
changeset
|
5 local thread, main = coroutine.running(); |
b751bee6a829
util.async: Fix thread check to work correctly in Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
8407
diff
changeset
|
6 if not thread or main then |
8407
f652e1ea2f69
util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents:
8237
diff
changeset
|
7 error("Not running in an async context, see https://prosody.im/doc/developers/util/async"); |
f652e1ea2f69
util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents:
8237
diff
changeset
|
8 end |
f652e1ea2f69
util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents:
8237
diff
changeset
|
9 return thread; |
f652e1ea2f69
util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents:
8237
diff
changeset
|
10 end |
f652e1ea2f69
util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents:
8237
diff
changeset
|
11 |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 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
|
13 -- 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
|
14 if coroutine.status(thread) ~= "suspended" then -- This should suffice |
8601
ce5b16e13573
util.async: Log warning when unexpected state is reached
Matthew Wild <mwild1@gmail.com>
parents:
8600
diff
changeset
|
15 log("warn", "unexpected async state: thread not suspended"); |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 return false; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 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
|
19 if not ok then |
8609
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8604
diff
changeset
|
20 local err = state; |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
21 -- Running the coroutine failed, which means we have to find the runner manually, |
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
22 -- in order to inform the error handler |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local level = 0; |
8611 | 24 -- Find the 'level' of the top-most function (0 == current level, 1 == caller, ...) |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 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
|
26 ok, runner = debug.getlocal(thread, level-1, 1); |
8613
dbb4788db8e3
util.async: Convert asserts to a return false (same as other unexpected behaviour)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
27 if ok ~= "self" or runner.thread ~= thread then |
dbb4788db8e3
util.async: Convert asserts to a return false (same as other unexpected behaviour)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
28 log("warn", "unexpected async state: unable to locate runner during error handling, got %s", ok); |
dbb4788db8e3
util.async: Convert asserts to a return false (same as other unexpected behaviour)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
29 return false; |
dbb4788db8e3
util.async: Convert asserts to a return false (same as other unexpected behaviour)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
30 end |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local error_handler = runner.watchers.error; |
8609
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8604
diff
changeset
|
32 if error_handler then error_handler(runner, debug.traceback(thread, err)); end |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8604
diff
changeset
|
33 local ready_handler = runner.watchers.ready; |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8604
diff
changeset
|
34 runner.state, runner.thread = "ready", nil; |
8616
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8615
diff
changeset
|
35 return runner:run(); |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 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
|
37 -- 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
|
38 -- 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
|
39 -- 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
|
40 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
|
41 runner:run(); |
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 return true; |
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 |
5790
959163e4d631
util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents:
5788
diff
changeset
|
46 local function waiter(num) |
8407
f652e1ea2f69
util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents:
8237
diff
changeset
|
47 local thread = checkthread(); |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 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
|
49 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
|
50 return function () |
5792
aac4c6147647
util.async: waiter: Remove restriction about wait() being called before done()
Matthew Wild <mwild1@gmail.com>
parents:
5791
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 end, function () |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 |
5797
a493b79cfad0
util.async: Make guarder() local
Matthew Wild <mwild1@gmail.com>
parents:
5796
diff
changeset
|
64 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
|
65 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
|
66 return function (id, func) |
8407
f652e1ea2f69
util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents:
8237
diff
changeset
|
67 local thread = checkthread(); |
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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
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 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 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
|
98 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
|
99 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 local function runner_create_thread(func, self) |
7725
f928695a2af1
util.async: Add annotation to ignore warning [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7724
diff
changeset
|
101 local thread = coroutine.create(function (self) -- luacheck: ignore 432/self |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 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
|
103 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
|
104 end |
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 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
|
107 return thread; |
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 empty_watchers = {}; |
5790
959163e4d631
util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents:
5788
diff
changeset
|
111 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
|
112 return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready", |
8600
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
113 queue = {}, watchers = watchers or empty_watchers, data = data, id = new_id() } |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 , runner_mt); |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
117 -- Add a task item for the runner to process |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 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
|
119 if input ~= nil then |
8602
9901deadc068
util.async: Fix order of statements so queue count makes more sense
Matthew Wild <mwild1@gmail.com>
parents:
8601
diff
changeset
|
120 table.insert(self.queue, input); |
8600
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
121 self:log("debug", "queued new work item, %d items queued", #self.queue); |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 if self.state ~= "ready" then |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
124 -- The runner is busy. Indicate that the task item has been |
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
125 -- queued, and return information about the current runner state |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 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
|
127 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 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
|
130 if not thread or coroutine.status(thread) == "dead" then |
8600
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
131 self:log("debug", "creating new coroutine"); |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
132 -- Create a new coroutine for this runner |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 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
|
134 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
|
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 |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
137 -- Process task item(s) while the queue is not empty, and we're not blocked |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 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
|
139 self.state = "running"; |
8600
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
140 self:log("debug", "running main loop"); |
8603
dc5f3302a642
util.async: Bugfix, don't continue main loop while there is a pending error
Matthew Wild <mwild1@gmail.com>
parents:
8602
diff
changeset
|
141 while n > 0 and state == "ready" and not err do |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 local consumed; |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
143 -- Loop through queue items, and attempt to run them |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 for i = 1,n do |
7724
20a69ef5570c
util.async: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7436
diff
changeset
|
145 local queued_input = q[i]; |
20a69ef5570c
util.async: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7436
diff
changeset
|
146 local ok, new_state = coroutine.resume(thread, queued_input); |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 if not ok then |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
148 -- There was an error running the coroutine, save the error, mark runner as ready to begin again |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 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
|
150 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
|
151 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
|
152 elseif new_state == "wait" then |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
153 -- Runner is blocked on waiting for a task item to complete |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 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
|
155 break; |
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 end |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
158 -- Loop ended - either queue empty because all tasks passed without blocking (consumed == nil) |
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
159 -- or runner is blocked/errored, and consumed will contain the number of tasks processed so far |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 if not consumed then consumed = n; end |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
161 -- Remove consumed items from the queue array |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 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
|
163 n = #q; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 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
|
166 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
|
167 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 n = #q; |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 end |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
170 -- Runner processed all items it can, so save current runner state |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 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
|
172 if err or state ~= self.notified_state then |
8604
1c8c7fd259c8
util.async: Log the non-error state as well when there is an error being processed
Matthew Wild <mwild1@gmail.com>
parents:
8603
diff
changeset
|
173 self:log("debug", "changed state from %s to %s", self.notified_state, err and ("error ("..state..")") or 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
|
174 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
|
175 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
|
176 else |
66c3ad5d29ad
util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents:
5793
diff
changeset
|
177 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
|
178 end |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 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
|
180 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
|
181 end |
8615
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8613
diff
changeset
|
182 if n > 0 then |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8613
diff
changeset
|
183 return self:run(); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8613
diff
changeset
|
184 end |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 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
|
186 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 |
7436
649b89b2c840
util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents:
7359
diff
changeset
|
188 -- Add a task item to the queue without invoking the runner, even if it is idle |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 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
|
190 table.insert(self.queue, input); |
8602
9901deadc068
util.async: Fix order of statements so queue count makes more sense
Matthew Wild <mwild1@gmail.com>
parents:
8601
diff
changeset
|
191 self:log("debug", "queued new work item, %d items queued", #self.queue); |
5788
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 end |
3556f338caa3
util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 |
8600
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
194 function runner_mt:log(level, fmt, ...) |
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
195 return log(level, "[runner %s] "..fmt, self.id, ...); |
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
196 end |
96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents:
8408
diff
changeset
|
197 |
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
|
198 return { waiter = waiter, guarder = guarder, runner = runner }; |