util/async.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 12 Aug 2013 10:27:08 +0100
changeset 5790 959163e4d631
parent 5788 3556f338caa3
child 5791 2c98061b6b1e
permissions -rw-r--r--
util.async: Make functions local
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
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
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
     4
	-- 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
     5
	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
     6
		return false;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
	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
     9
	if not ok then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
		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
    11
		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
    12
		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
    13
		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
    14
		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
    15
	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
    16
		-- 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
    17
		-- 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
    18
		-- 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
    19
		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
    20
		runner:run();
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
	return true;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
5790
959163e4d631 util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents: 5788
diff changeset
    25
local function waiter(num)
5788
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	local thread = coroutine.running();
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	if not thread then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
		error("Not running in an async context, see http://prosody.im/doc/developers/async");
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
	num = num or 1;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
	return function ()
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
		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
    33
	end, function ()
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		num = num - 1;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
		if num == 0 then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
			if not runner_continue(thread) then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
				error("done() called without wait()!");
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
			end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
		end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	end;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
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
    44
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
    45
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_create_thread(func, self)
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
	local thread = coroutine.create(function (self)
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
		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
    49
			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
    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
	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
    53
	return thread;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
local empty_watchers = {};
5790
959163e4d631 util.async: Make functions local
Matthew Wild <mwild1@gmail.com>
parents: 5788
diff changeset
    57
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
    58
	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
    59
		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
    60
	, runner_mt);
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
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
    64
	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
    65
		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
    66
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
	if self.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
		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
    69
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	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
    72
	if not thread or coroutine.status(thread) == "dead" then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
		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
    74
		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
    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
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
	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
    78
	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
    79
	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
    80
		local consumed;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
		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
    82
			local input = q[i];
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
			local ok, new_state = coroutine.resume(thread, input);
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
			if not ok then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
				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
    86
				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
    87
				break;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
			elseif state == "wait" then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
				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
    90
				break;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
			end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
		end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
		if not consumed then consumed = n; end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
		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
    95
			n = #q;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
		end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
		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
    98
			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
    99
		end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
		n = #q;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
	self.state = state;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
	if state ~= self.notified_state then
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
		self.notified_state = state;
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
		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
   106
		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
   107
	end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
	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
   109
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
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
   112
	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
   113
end
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
3556f338caa3 util.async: New library to provide support around coroutine-based non-blocking functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
return { waiter = waiter, runner = runner };