util/async.lua
author Kim Alvefur <zash@zash.se>
Wed, 27 Mar 2024 19:33:11 +0100
changeset 13471 c2a476f4712a
parent 13341 5e258164d214
permissions -rw-r--r--
util.startup: Fix exiting on pidfile trouble prosody.shutdown() relies on prosody.main_thread, which has not been set yet at this point. Doing a clean shutdown might actually be harmful in case it tears down things set up by the conflicting Prosody, such as the very pidfile we were looking at. Thanks again SigmaTel71 for noticing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
     1
local logger = require "prosody.util.logger";
8791
7a9b680a79fb util.async: Move runner id into log tag
Kim Alvefur <zash@zash.se>
parents: 8769
diff changeset
     2
local log = logger.init("util.async");
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
     3
local new_id = require "prosody.util.id".short;
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
     4
local xpcall = require "prosody.util.xpcall".xpcall;
13334
49ecfb070240 util.async: Export a table of currently-waiting runners
Matthew Wild <mwild1@gmail.com>
parents: 12979
diff changeset
     5
local time_now = require "prosody.util.time".now;
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
8410
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
     7
local function checkthread()
8411
b751bee6a829 util.async: Fix thread check to work correctly in Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 8410
diff changeset
     8
	local thread, main = coroutine.running();
b751bee6a829 util.async: Fix thread check to work correctly in Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 8410
diff changeset
     9
	if not thread or main then
8410
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
    10
		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: 8240
diff changeset
    11
	end
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
    12
	return thread;
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
    13
end
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
    14
11965
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
    15
-- Configurable functions
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
    16
local schedule_task = nil; -- schedule_task(seconds, callback)
11966
9a70a543c727 util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents: 11965
diff changeset
    17
local next_tick = function (f)
9a70a543c727 util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents: 11965
diff changeset
    18
	f();
9a70a543c727 util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents: 11965
diff changeset
    19
end
11965
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
    20
8630
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    21
local function runner_from_thread(thread)
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    22
	local level = 0;
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    23
	-- Find the 'level' of the top-most function (0 == current level, 1 == caller, ...)
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    24
	while debug.getinfo(thread, level, "") do level = level + 1; end
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    25
	local name, runner = debug.getlocal(thread, level-1, 1);
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    26
	if name ~= "self" or type(runner) ~= "table" or runner.thread ~= thread then
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    27
		return nil;
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    28
	end
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    29
	return runner;
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    30
end
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    31
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    32
local function call_watcher(runner, watcher_name, ...)
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    33
	local watcher = runner.watchers[watcher_name];
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    34
	if not watcher then
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    35
		return false;
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    36
	end
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    37
	runner:log("debug", "Calling '%s' watcher", watcher_name);
9565
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 9180
diff changeset
    38
	local ok, err = xpcall(watcher, debug.traceback, runner, ...);
8630
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    39
	if not ok then
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    40
		runner:log("error", "Error in '%s' watcher: %s", watcher_name, err);
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    41
		return nil, err;
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    42
	end
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    43
	return true;
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    44
end
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    45
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
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
    47
	-- 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
    48
	if coroutine.status(thread) ~= "suspended" then -- This should suffice
13335
5206314d6c70 util.async: Improve debug logging in a few places
Matthew Wild <mwild1@gmail.com>
parents: 13334
diff changeset
    49
		log("error", "unexpected async state: thread not suspended (%s, %s)", thread, coroutine.status(thread));
5206314d6c70 util.async: Improve debug logging in a few places
Matthew Wild <mwild1@gmail.com>
parents: 13334
diff changeset
    50
		-- Fetching the traceback is likely to *crash* if a C library is calling us while suspended
5206314d6c70 util.async: Improve debug logging in a few places
Matthew Wild <mwild1@gmail.com>
parents: 13334
diff changeset
    51
		--log("error", "coroutine stack: %s", debug.traceback());
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
		return false;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
	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
    55
	if not ok then
8612
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8607
diff changeset
    56
		local err = state;
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
    57
		-- 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: 7362
diff changeset
    58
		-- in order to inform the error handler
8630
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    59
		runner = runner_from_thread(thread);
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    60
		if not runner then
8632
ddb04cacb8b1 util.async: Bump log warnings to error level
Matthew Wild <mwild1@gmail.com>
parents: 8630
diff changeset
    61
			log("error", "unexpected async state: unable to locate runner during error handling");
8616
dbb4788db8e3 util.async: Convert asserts to a return false (same as other unexpected behaviour)
Matthew Wild <mwild1@gmail.com>
parents: 8614
diff changeset
    62
			return false;
dbb4788db8e3 util.async: Convert asserts to a return false (same as other unexpected behaviour)
Matthew Wild <mwild1@gmail.com>
parents: 8614
diff changeset
    63
		end
8630
24b59a62acce util.async: Split runner_continue into smaller functions for easier testing and safety
Matthew Wild <mwild1@gmail.com>
parents: 8628
diff changeset
    64
		call_watcher(runner, "error", debug.traceback(thread, err));
10934
69a4b0e3565f util.async: Call coroutine.close() on dead threads (Lua 5.4)
Matthew Wild <mwild1@gmail.com>
parents: 10932
diff changeset
    65
		runner.state = "ready";
8619
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
    66
		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
    67
	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
    68
		-- 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
    69
		-- 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
    70
		-- processed otherwise. FIXME: It's probably best to do this in a nexttick (0 timer).
11966
9a70a543c727 util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents: 11965
diff changeset
    71
		next_tick(function ()
9a70a543c727 util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents: 11965
diff changeset
    72
			runner.state = "ready";
9a70a543c727 util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents: 11965
diff changeset
    73
			runner:run();
9a70a543c727 util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents: 11965
diff changeset
    74
		end);
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	return true;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
12314
91af1697ddd8 util.async: Optionally allow too many 'done' callbacks
Kim Alvefur <zash@zash.se>
parents: 11966
diff changeset
    79
local function waiter(num, allow_many)
8410
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
    80
	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
    81
	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
    82
	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
    83
	return function ()
5792
aac4c6147647 util.async: waiter: Remove restriction about wait() being called before done()
Matthew Wild <mwild1@gmail.com>
parents: 5791
diff changeset
    84
		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
    85
		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
    86
		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
    87
	end, function ()
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
		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
    89
		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
    90
			runner_continue(thread);
12314
91af1697ddd8 util.async: Optionally allow too many 'done' callbacks
Kim Alvefur <zash@zash.se>
parents: 11966
diff changeset
    91
		elseif not allow_many and num < 0 then
5793
e8c79796ead9 util.async: waiter: Throw error if done() called too many times
Kim Alvefur <zash@zash.se>
parents: 5792
diff changeset
    92
			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
    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
	end;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
5797
a493b79cfad0 util.async: Make guarder() local
Matthew Wild <mwild1@gmail.com>
parents: 5796
diff changeset
    97
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
    98
	local guards = {};
8658
ba6a6a04b46c util.async: Allow nil as a guard key
Matthew Wild <mwild1@gmail.com>
parents: 8654
diff changeset
    99
	local default_id = {};
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
   100
	return function (id, func)
8658
ba6a6a04b46c util.async: Allow nil as a guard key
Matthew Wild <mwild1@gmail.com>
parents: 8654
diff changeset
   101
		id = id or default_id;
8410
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
   102
		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
   103
		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
   104
		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
   105
			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
   106
			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
   107
			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
   108
		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
   109
			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
   110
			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
   111
			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
   112
		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
   113
		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
   114
			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
   115
			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
   116
				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
   117
				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
   118
			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
   119
				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
   120
				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
   121
			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
   122
		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
   123
		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
   124
			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
   125
			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
   126
			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
   127
		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
   128
		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
   129
	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
   130
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
   131
11965
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   132
local function sleep(seconds)
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   133
	if not schedule_task then
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   134
		error("async.sleep() is not available - configure schedule function");
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   135
	end
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   136
	local wait, done = waiter();
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   137
	schedule_task(seconds, done);
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   138
	wait();
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   139
end
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   140
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
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
   142
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
   143
13334
49ecfb070240 util.async: Export a table of currently-waiting runners
Matthew Wild <mwild1@gmail.com>
parents: 12979
diff changeset
   144
local waiting_runners = {};
49ecfb070240 util.async: Export a table of currently-waiting runners
Matthew Wild <mwild1@gmail.com>
parents: 12979
diff changeset
   145
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
local function runner_create_thread(func, self)
7728
f928695a2af1 util.async: Add annotation to ignore warning [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7727
diff changeset
   147
	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
   148
		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
   149
			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
   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
	end);
8930
ed0891383e78 util.async: Copy hooks from main thread into coroutines
Matthew Wild <mwild1@gmail.com>
parents: 8791
diff changeset
   152
	debug.sethook(thread, debug.gethook());
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
	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
   154
	return thread;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
8684
0c077800cd70 util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents: 8672
diff changeset
   157
local function default_error_watcher(runner, err)
0c077800cd70 util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents: 8672
diff changeset
   158
	runner:log("error", "Encountered error: %s", err);
0c077800cd70 util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents: 8672
diff changeset
   159
	error(err);
0c077800cd70 util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents: 8672
diff changeset
   160
end
0c077800cd70 util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents: 8672
diff changeset
   161
local function default_func(f) f(); end
5790
959163e4d631 util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents: 5788
diff changeset
   162
local function runner(func, watchers, data)
8791
7a9b680a79fb util.async: Move runner id into log tag
Kim Alvefur <zash@zash.se>
parents: 8769
diff changeset
   163
	local id = new_id();
7a9b680a79fb util.async: Move runner id into log tag
Kim Alvefur <zash@zash.se>
parents: 8769
diff changeset
   164
	local _log = logger.init("runner" .. id);
8684
0c077800cd70 util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents: 8672
diff changeset
   165
	return setmetatable({ func = func or default_func, thread = false, state = "ready", notified_state = "ready",
8791
7a9b680a79fb util.async: Move runner id into log tag
Kim Alvefur <zash@zash.se>
parents: 8769
diff changeset
   166
		queue = {}, watchers = watchers or { error = default_error_watcher }, data = data, id = id, _log = _log; }
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
	, runner_mt);
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   170
-- 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
   171
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
   172
	if input ~= nil then
8605
9901deadc068 util.async: Fix order of statements so queue count makes more sense
Matthew Wild <mwild1@gmail.com>
parents: 8604
diff changeset
   173
		table.insert(self.queue, input);
8979
92f0876b9230 MUC: Add config option to allow members to invite other members to the room (previously only owners/admins could do this)
Matthew Wild <mwild1@gmail.com>
parents: 8930
diff changeset
   174
		--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
   175
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
	if self.state ~= "ready" then
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   177
		-- 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: 7362
diff changeset
   178
		-- 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
   179
		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
   180
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
	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
   183
	if not thread or coroutine.status(thread) == "dead" then
10934
69a4b0e3565f util.async: Call coroutine.close() on dead threads (Lua 5.4)
Matthew Wild <mwild1@gmail.com>
parents: 10932
diff changeset
   184
		--luacheck: ignore 143/coroutine
10935
558f0555ba02 util.async: Don't attempt to close thread if not created yet
Matthew Wild <mwild1@gmail.com>
parents: 10934
diff changeset
   185
		if thread and coroutine.close then
10934
69a4b0e3565f util.async: Call coroutine.close() on dead threads (Lua 5.4)
Matthew Wild <mwild1@gmail.com>
parents: 10932
diff changeset
   186
			coroutine.close(thread);
69a4b0e3565f util.async: Call coroutine.close() on dead threads (Lua 5.4)
Matthew Wild <mwild1@gmail.com>
parents: 10932
diff changeset
   187
		end
8603
96f20cf92b84 util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
   188
		self:log("debug", "creating new coroutine");
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   189
		-- 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
   190
		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
   191
		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
   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
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   194
	-- 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
   195
	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
   196
	self.state = "running";
8979
92f0876b9230 MUC: Add config option to allow members to invite other members to the room (previously only owners/admins could do this)
Matthew Wild <mwild1@gmail.com>
parents: 8930
diff changeset
   197
	--self:log("debug", "running main loop");
8606
dc5f3302a642 util.async: Bugfix, don't continue main loop while there is a pending error
Matthew Wild <mwild1@gmail.com>
parents: 8605
diff changeset
   198
	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
   199
		local consumed;
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   200
		-- 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
   201
		for i = 1,n do
7727
20a69ef5570c util.async: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7439
diff changeset
   202
			local queued_input = q[i];
13335
5206314d6c70 util.async: Improve debug logging in a few places
Matthew Wild <mwild1@gmail.com>
parents: 13334
diff changeset
   203
			self:log("Resuming thread with new item [%s]", thread);
13336
17cb965e55b7 util.async: Record current work item in the runner object
Matthew Wild <mwild1@gmail.com>
parents: 13335
diff changeset
   204
			self.current_item = queued_input;
7727
20a69ef5570c util.async: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7439
diff changeset
   205
			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
   206
			if not ok then
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   207
				-- 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
   208
				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
   209
				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
   210
				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
   211
			elseif new_state == "wait" then
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   212
				 -- 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
   213
				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
   214
				break;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
			end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
		end
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   217
		-- 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: 7362
diff changeset
   218
		-- 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
   219
		if not consumed then consumed = n; end
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   220
		-- 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
   221
		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
   222
			n = #q;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
		end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
		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
   225
			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
   226
		end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   227
		n = #q;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
	end
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   229
	-- 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
   230
	self.state = state;
13336
17cb965e55b7 util.async: Record current work item in the runner object
Matthew Wild <mwild1@gmail.com>
parents: 13335
diff changeset
   231
	if state == "ready" and self.current_item then
17cb965e55b7 util.async: Record current work item in the runner object
Matthew Wild <mwild1@gmail.com>
parents: 13335
diff changeset
   232
		self.current_item = nil;
17cb965e55b7 util.async: Record current work item in the runner object
Matthew Wild <mwild1@gmail.com>
parents: 13335
diff changeset
   233
	end
17cb965e55b7 util.async: Record current work item in the runner object
Matthew Wild <mwild1@gmail.com>
parents: 13335
diff changeset
   234
5794
66c3ad5d29ad util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents: 5793
diff changeset
   235
	if err or state ~= self.notified_state then
13341
5e258164d214 util.async: Clip long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13337
diff changeset
   236
		self:log("debug", "changed state from %s to %s [%s %s]", self.notified_state, err and ("error (" .. state .. ")") or state, self.thread,
5e258164d214 util.async: Clip long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13337
diff changeset
   237
			self.thread and coroutine.status(self.thread));
5794
66c3ad5d29ad util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents: 5793
diff changeset
   238
		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
   239
			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
   240
		else
66c3ad5d29ad util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents: 5793
diff changeset
   241
			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
   242
		end
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
		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
   244
		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
   245
	end
8618
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8616
diff changeset
   246
	if n > 0 then
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8616
diff changeset
   247
		return self:run();
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8616
diff changeset
   248
	end
13334
49ecfb070240 util.async: Export a table of currently-waiting runners
Matthew Wild <mwild1@gmail.com>
parents: 12979
diff changeset
   249
	waiting_runners[self] = state == "waiting" and time_now() or nil;
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
	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
   251
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   253
-- 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
   254
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
   255
	table.insert(self.queue, input);
8605
9901deadc068 util.async: Fix order of statements so queue count makes more sense
Matthew Wild <mwild1@gmail.com>
parents: 8604
diff changeset
   256
	self:log("debug", "queued new work item, %d items queued", #self.queue);
8769
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   257
	return self;
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   258
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
8603
96f20cf92b84 util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
   260
function runner_mt:log(level, fmt, ...)
8791
7a9b680a79fb util.async: Move runner id into log tag
Kim Alvefur <zash@zash.se>
parents: 8769
diff changeset
   261
	return self._log(level, fmt, ...);
8603
96f20cf92b84 util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
   262
end
96f20cf92b84 util.async: Add per-runner ids and add runner:log() method
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
   263
8769
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   264
function runner_mt:onready(f)
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   265
	self.watchers.ready = f;
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   266
	return self;
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   267
end
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   268
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   269
function runner_mt:onwaiting(f)
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   270
	self.watchers.waiting = f;
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   271
	return self;
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   272
end
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   273
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   274
function runner_mt:onerror(f)
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   275
	self.watchers.error = f;
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   276
	return self;
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   277
end
01264bc60c2b util.async: Add helper methods for setting watchers
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   278
8651
ca710a71d730 util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents: 8632
diff changeset
   279
local function ready()
ca710a71d730 util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents: 8632
diff changeset
   280
	return pcall(checkthread);
ca710a71d730 util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents: 8632
diff changeset
   281
end
ca710a71d730 util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents: 8632
diff changeset
   282
10932
79faf5b98395 util.async: Rename wait -> wait_for (w/compat)
Matthew Wild <mwild1@gmail.com>
parents: 10295
diff changeset
   283
local function wait_for(promise)
10295
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   284
	local async_wait, async_done = waiter();
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   285
	local ret, err = nil, nil;
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   286
	promise:next(
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   287
		function (r) ret = r; end,
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   288
		function (e) err = e; end)
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   289
		:finally(async_done);
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   290
	async_wait();
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   291
	if ret then
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   292
		return ret;
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   293
	else
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   294
		return nil, err;
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   295
	end
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   296
end
7b48b620164c util.async: Add function for waiting on promises and unpacking the results
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
   297
8652
9246f64d6f1e util.async: Add once() to create temporary runners
Matthew Wild <mwild1@gmail.com>
parents: 8651
diff changeset
   298
return {
9246f64d6f1e util.async: Add once() to create temporary runners
Matthew Wild <mwild1@gmail.com>
parents: 8651
diff changeset
   299
	ready = ready;
9246f64d6f1e util.async: Add once() to create temporary runners
Matthew Wild <mwild1@gmail.com>
parents: 8651
diff changeset
   300
	waiter = waiter;
9246f64d6f1e util.async: Add once() to create temporary runners
Matthew Wild <mwild1@gmail.com>
parents: 8651
diff changeset
   301
	guarder = guarder;
8654
1b7c5933b215 util.async: Add sleep() method
Matthew Wild <mwild1@gmail.com>
parents: 8652
diff changeset
   302
	runner = runner;
10932
79faf5b98395 util.async: Rename wait -> wait_for (w/compat)
Matthew Wild <mwild1@gmail.com>
parents: 10295
diff changeset
   303
	wait = wait_for; -- COMPAT w/trunk pre-0.12
79faf5b98395 util.async: Rename wait -> wait_for (w/compat)
Matthew Wild <mwild1@gmail.com>
parents: 10295
diff changeset
   304
	wait_for = wait_for;
11965
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   305
	sleep = sleep;
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   306
11966
9a70a543c727 util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents: 11965
diff changeset
   307
	set_nexttick = function(new_next_tick) next_tick = new_next_tick; end;
11965
542a9a503073 util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents: 10935
diff changeset
   308
	set_schedule_function = function (new_schedule_function) schedule_task = new_schedule_function; end;
13337
fa4143163e01 util.async: Expose default runner function
Matthew Wild <mwild1@gmail.com>
parents: 13336
diff changeset
   309
13334
49ecfb070240 util.async: Export a table of currently-waiting runners
Matthew Wild <mwild1@gmail.com>
parents: 12979
diff changeset
   310
	waiting_runners = waiting_runners;
13337
fa4143163e01 util.async: Expose default runner function
Matthew Wild <mwild1@gmail.com>
parents: 13336
diff changeset
   311
	default_runner_func = default_func;
8652
9246f64d6f1e util.async: Add once() to create temporary runners
Matthew Wild <mwild1@gmail.com>
parents: 8651
diff changeset
   312
};