util/async.lua
author Kim Alvefur <zash@zash.se>
Tue, 21 Nov 2017 21:48:14 +0100
changeset 8410 f652e1ea2f69
parent 8240 f81cd9aaf994
child 8411 b751bee6a829
permissions -rw-r--r--
util.async: Factor out thread check into a function
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local log = require "util.logger".init("util.async");
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
8410
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
     3
local function checkthread()
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
     4
	local thread = coroutine.running();
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
     5
	if not thread then
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
     6
		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
     7
	end
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
     8
	return thread;
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
     9
end
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
    10
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
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
    12
	-- 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
    13
	if coroutine.status(thread) ~= "suspended" then -- This should suffice
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
		return false;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	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
    17
	if not ok then
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
    18
		-- 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
    19
		-- 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
    20
		local level = 0;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
		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
    22
		ok, runner = debug.getlocal(thread, level-1, 1);
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
		local error_handler = runner.watchers.error;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		if error_handler then error_handler(runner, debug.traceback(thread, state)); end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
	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
    26
		-- 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
    27
		-- 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
    28
		-- 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
    29
		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
    30
		runner:run();
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
	return true;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
5790
959163e4d631 util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents: 5788
diff changeset
    35
local function waiter(num)
8410
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
    36
	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
    37
	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
    38
	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
    39
	return function ()
5792
aac4c6147647 util.async: waiter: Remove restriction about wait() being called before done()
Matthew Wild <mwild1@gmail.com>
parents: 5791
diff changeset
    40
		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
    41
		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
    42
		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
    43
	end, function ()
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
		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
    45
		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
    46
			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
    47
		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
    48
			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
    49
		end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	end;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
5797
a493b79cfad0 util.async: Make guarder() local
Matthew Wild <mwild1@gmail.com>
parents: 5796
diff changeset
    53
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
    54
	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
    55
	return function (id, func)
8410
f652e1ea2f69 util.async: Factor out thread check into a function
Kim Alvefur <zash@zash.se>
parents: 8240
diff changeset
    56
		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
    57
		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
    58
		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
    59
			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
    60
			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
    61
			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
    62
		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
    63
			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
    64
			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
    65
			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
    66
		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
    67
		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
    68
			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
    69
			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
    70
				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
    71
				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
    72
			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
    73
				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
    74
				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
    75
			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
    76
		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
    77
		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
    78
			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
    79
			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
    80
			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
    81
		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
    82
		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
    83
	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
    84
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
    85
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
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
    87
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
    88
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
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
    90
	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
    91
		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
    92
			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
    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
	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
    96
	return thread;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
local empty_watchers = {};
5790
959163e4d631 util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents: 5788
diff changeset
   100
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
   101
	return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready",
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
		queue = {}, watchers = watchers or empty_watchers, data = data }
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
	, runner_mt);
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
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   106
-- 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
   107
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
   108
	if input ~= nil then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
		table.insert(self.queue, input);
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
	if self.state ~= "ready" then
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   112
		-- 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
   113
		-- 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
   114
		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
   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
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
	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
   118
	if not thread or coroutine.status(thread) == "dead" then
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   119
		-- 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
   120
		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
   121
		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
   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
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   124
	-- 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
   125
	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
   126
	self.state = "running";
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
	while n > 0 and state == "ready" do
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
		local consumed;
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   129
		-- 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
   130
		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
   131
			local queued_input = q[i];
20a69ef5570c util.async: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7439
diff changeset
   132
			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
   133
			if not ok then
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   134
				-- 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
   135
				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
   136
				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
   137
				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
   138
			elseif new_state == "wait" then
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   139
				 -- 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
   140
				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
   141
				break;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
			end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
		end
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   144
		-- 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
   145
		-- 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
   146
		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
   147
		-- 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
   148
		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
   149
			n = #q;
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
		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
   152
			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
   153
		end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
		n = #q;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
	end
7439
649b89b2c840 util.async: Add some more comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 7362
diff changeset
   156
	-- 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
   157
	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
   158
	if err or state ~= self.notified_state then
66c3ad5d29ad util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents: 5793
diff changeset
   159
		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
   160
			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
   161
		else
66c3ad5d29ad util.async: Fix logic bug that prevented error watcher being called for runners
Matthew Wild <mwild1@gmail.com>
parents: 5793
diff changeset
   162
			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
   163
		end
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
		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
   165
		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
   166
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
	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
   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 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
   171
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
   172
	table.insert(self.queue, input);
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
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
   175
return { waiter = waiter, guarder = guarder, runner = runner };