1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/utils/task.lua

119 lines
4.5 KiB
Lua
Raw Normal View History

-- Threading simulation module
2018-01-27 17:49:55 +02:00
-- Task.sleep()
-- @author Valansch and Grilledham
2019-02-04 20:07:40 +02:00
-- github: https://github.com/Refactorio/RedMew
-- ======================================================= --
local Queue = require 'utils.queue'
2018-11-26 03:07:03 +02:00
local PriorityQueue = require 'utils.priority_queue'
local Event = require 'utils.event'
2018-11-26 03:07:03 +02:00
local Token = require 'utils.token'
2019-02-25 03:04:19 +02:00
local ErrorLogging = require 'utils.error_logging'
2018-01-27 17:49:55 +02:00
local Task = {}
global.callbacks = global.callbacks or PriorityQueue.new()
global.next_async_callback_time = -1
global.task_queue = global.task_queue or Queue.new()
2018-01-28 20:33:25 +02:00
global.total_task_weight = 0
global.task_queue_speed = 1
local function comp(a, b)
return a.time < b.time
end
2018-11-06 13:55:52 +02:00
global.tpt = global.task_queue_speed
local function get_task_per_tick()
if game.tick % 300 == 0 then
local size = global.total_task_weight
global.tpt = math.floor(math.log10(size + 1)) * global.task_queue_speed
if global.tpt < 1 then
global.tpt = 1
end
end
return global.tpt
end
local function on_tick()
local queue = global.task_queue
for i = 1, get_task_per_tick() do
local task = Queue.peek(queue)
if task ~= nil then
-- result is error if not success else result is a boolean for if the task should stay in the queue.
local success, result = pcall(Token.get(task.func_token), task.params)
if not success then
if _DEBUG then
error(result)
else
log(result)
2019-02-25 03:04:19 +02:00
ErrorLogging.generate_error_report(result)
end
Queue.pop(queue)
global.total_task_weight = global.total_task_weight - task.weight
elseif not result then
Queue.pop(queue)
global.total_task_weight = global.total_task_weight - task.weight
end
end
end
local callbacks = global.callbacks
local callback = PriorityQueue.peek(callbacks)
while callback ~= nil and game.tick >= callback.time do
local success, result = pcall(Token.get(callback.func_token), callback.params)
if not success then
if _DEBUG then
error(result)
else
log(result)
2019-02-25 03:04:19 +02:00
ErrorLogging.generate_error_report(result)
end
end
PriorityQueue.pop(callbacks, comp)
callback = PriorityQueue.peek(callbacks)
end
end
2019-02-04 20:07:40 +02:00
--- Allows you to set a timer (in ticks) after which the tokened function will be run with params given as an argument
-- Cannot be called before init
2019-02-04 20:07:40 +02:00
-- @param ticks <number>
-- @param func_token <number> a token for a function store via the token system
-- @param params <any> the argument to send to the tokened function
function Task.set_timeout_in_ticks(ticks, func_token, params)
2019-02-18 08:43:59 +02:00
if not game then
error('cannot call when game is not available', 2)
2019-02-04 23:17:00 +02:00
end
local time = game.tick + ticks
local callback = {time = time, func_token = func_token, params = params}
PriorityQueue.push(global.callbacks, callback, comp)
end
2019-02-04 20:07:40 +02:00
--- Allows you to set a timer (in seconds) after which the tokened function will be run with params given as an argument
-- Cannot be called before init
2019-02-04 20:07:40 +02:00
-- @param sec <number>
-- @param func_token <number> a token for a function store via the token system
-- @param params <any> the argument to send to the tokened function
function Task.set_timeout(sec, func_token, params)
2019-02-18 08:43:59 +02:00
if not game then
error('cannot call when game is not available', 2)
2019-02-04 23:17:00 +02:00
end
Task.set_timeout_in_ticks(60 * sec, func_token, params)
2017-10-06 02:13:56 +02:00
end
2019-02-04 20:07:40 +02:00
--- Queueing allows you to split up heavy tasks which don't need to be completed in the same tick.
-- Queued tasks are generally run 1 per tick. If the queue backs up, more tasks will be processed per tick.
-- @param func_token <number> a token for a function stored via the token system
-- If this function returns `true` it will run again the next tick, delaying other queued tasks (see weight)
-- @param params <any> the argument to send to the tokened function
-- @param weight <number> (defaults to 1) weight is the number of ticks a task is expected to take.
-- Ex. if the task is expected to repeat multiple times (ie. the function returns true and loops several ticks)
function Task.queue_task(func_token, params, weight)
weight = weight or 1
global.total_task_weight = global.total_task_weight + weight
Queue.push(global.task_queue, {func_token = func_token, params = params, weight = weight})
2017-10-06 02:13:56 +02:00
end
2018-04-06 21:58:50 +02:00
Event.add(defines.events.on_tick, on_tick)
2018-01-27 17:49:55 +02:00
return Task