Software / code / prosody
Comparison
util/async.lua @ 12310:91af1697ddd8
util.async: Optionally allow too many 'done' callbacks
Sometimes, like in mod_c2s and mod_s2s during shutdown, all you want is
to wait for the first done() and not complicate things.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 22 Feb 2022 14:17:10 +0100 |
| parent | 11962:9a70a543c727 |
| child | 12975:d10957394a3c |
comparison
equal
deleted
inserted
replaced
| 12309:926a6c5d13e7 | 12310:91af1697ddd8 |
|---|---|
| 71 end); | 71 end); |
| 72 end | 72 end |
| 73 return true; | 73 return true; |
| 74 end | 74 end |
| 75 | 75 |
| 76 local function waiter(num) | 76 local function waiter(num, allow_many) |
| 77 local thread = checkthread(); | 77 local thread = checkthread(); |
| 78 num = num or 1; | 78 num = num or 1; |
| 79 local waiting; | 79 local waiting; |
| 80 return function () | 80 return function () |
| 81 if num == 0 then return; end -- already done | 81 if num == 0 then return; end -- already done |
| 83 coroutine.yield("wait"); | 83 coroutine.yield("wait"); |
| 84 end, function () | 84 end, function () |
| 85 num = num - 1; | 85 num = num - 1; |
| 86 if num == 0 and waiting then | 86 if num == 0 and waiting then |
| 87 runner_continue(thread); | 87 runner_continue(thread); |
| 88 elseif num < 0 then | 88 elseif not allow_many and num < 0 then |
| 89 error("done() called too many times"); | 89 error("done() called too many times"); |
| 90 end | 90 end |
| 91 end; | 91 end; |
| 92 end | 92 end |
| 93 | 93 |