Comparison

util/async.lua @ 13330:49ecfb070240

util.async: Export a table of currently-waiting runners This can be used for debugging and introspection.
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Nov 2023 18:11:40 +0000
parent 12975:d10957394a3c
child 13331:5206314d6c70
comparison
equal deleted inserted replaced
13329:649876680bf0 13330:49ecfb070240
1 local logger = require "prosody.util.logger"; 1 local logger = require "prosody.util.logger";
2 local log = logger.init("util.async"); 2 local log = logger.init("util.async");
3 local new_id = require "prosody.util.id".short; 3 local new_id = require "prosody.util.id".short;
4 local xpcall = require "prosody.util.xpcall".xpcall; 4 local xpcall = require "prosody.util.xpcall".xpcall;
5 local time_now = require "prosody.util.time".now;
5 6
6 local function checkthread() 7 local function checkthread()
7 local thread, main = coroutine.running(); 8 local thread, main = coroutine.running();
8 if not thread or main then 9 if not thread or main then
9 error("Not running in an async context, see https://prosody.im/doc/developers/util/async"); 10 error("Not running in an async context, see https://prosody.im/doc/developers/util/async");
135 wait(); 136 wait();
136 end 137 end
137 138
138 local runner_mt = {}; 139 local runner_mt = {};
139 runner_mt.__index = runner_mt; 140 runner_mt.__index = runner_mt;
141
142 local waiting_runners = {};
140 143
141 local function runner_create_thread(func, self) 144 local function runner_create_thread(func, self)
142 local thread = coroutine.create(function (self) -- luacheck: ignore 432/self 145 local thread = coroutine.create(function (self) -- luacheck: ignore 432/self
143 while true do 146 while true do
144 func(coroutine.yield("ready", self)); 147 func(coroutine.yield("ready", self));
232 if handler then handler(self, err); end 235 if handler then handler(self, err); end
233 end 236 end
234 if n > 0 then 237 if n > 0 then
235 return self:run(); 238 return self:run();
236 end 239 end
240 waiting_runners[self] = state == "waiting" and time_now() or nil;
237 return true, state, n; 241 return true, state, n;
238 end 242 end
239 243
240 -- Add a task item to the queue without invoking the runner, even if it is idle 244 -- Add a task item to the queue without invoking the runner, even if it is idle
241 function runner_mt:enqueue(input) 245 function runner_mt:enqueue(input)
291 wait_for = wait_for; 295 wait_for = wait_for;
292 sleep = sleep; 296 sleep = sleep;
293 297
294 set_nexttick = function(new_next_tick) next_tick = new_next_tick; end; 298 set_nexttick = function(new_next_tick) next_tick = new_next_tick; end;
295 set_schedule_function = function (new_schedule_function) schedule_task = new_schedule_function; end; 299 set_schedule_function = function (new_schedule_function) schedule_task = new_schedule_function; end;
300 waiting_runners = waiting_runners;
296 }; 301 };