2016-08-21 18:10:21 +02:00
|
|
|
scheduler = {}
|
2016-10-29 16:25:57 +02:00
|
|
|
-- scheduler.queue = {[1]={first=1, last=2, groups={[1]={}, [2]={}}}}
|
|
|
|
scheduler.queue = {[1]={cur={}, nxt={}, ncur=1, nnxt=1}}
|
2016-08-21 18:10:21 +02:00
|
|
|
|
|
|
|
function scheduler.add(priority, job)
|
|
|
|
-- get asked class
|
|
|
|
local class = scheduler.queue[priority]
|
|
|
|
local p = priority
|
|
|
|
while not class do -- create all classes under 'priority'
|
2016-10-29 16:25:57 +02:00
|
|
|
--scheduler.queue[p] = {first=1, last=2, groups={[1]={}, [2]={}}}
|
|
|
|
scheduler.queue[p] = {cur={}, nxt={}, ncur=1, nnxt=1}
|
2016-08-21 18:10:21 +02:00
|
|
|
p = p-1
|
|
|
|
class = scheduler.queue[p]
|
|
|
|
end
|
|
|
|
class = scheduler.queue[priority]
|
2016-10-29 16:25:57 +02:00
|
|
|
class.nxt[class.nnxt] = job
|
|
|
|
class.nnxt = class.nnxt+1
|
2016-08-21 18:10:21 +02:00
|
|
|
-- get last group
|
2016-10-29 16:25:57 +02:00
|
|
|
--local grp = class.groups[class.last]
|
2016-08-21 18:10:21 +02:00
|
|
|
-- add job into last group
|
2016-10-29 16:25:57 +02:00
|
|
|
--grp[#grp+1] = job
|
2016-08-21 18:10:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function scheduler.asap(priority, func)
|
|
|
|
scheduler.add(priority, {
|
|
|
|
mod_name = core.get_last_run_mod(),
|
2016-09-03 19:46:20 +02:00
|
|
|
func_id = "#"..debug.getinfo(2, "S").linedefined, --imprecis
|
2016-08-21 18:10:21 +02:00
|
|
|
func_code = func,
|
|
|
|
arg = {},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function scheduler.mdebug(s)
|
|
|
|
minetest.debug("[Profnsched] "..s)
|
|
|
|
end
|
|
|
|
|
|
|
|
function scheduler.shift()
|
|
|
|
local nb = scheduler.waitingjobs()
|
2016-10-29 16:25:57 +02:00
|
|
|
local qnext = nil
|
2016-08-21 18:10:21 +02:00
|
|
|
for class,q in ipairs(scheduler.queue) do
|
2016-10-29 16:25:57 +02:00
|
|
|
q.cur = q.nxt
|
|
|
|
q.nxt = {}
|
|
|
|
q.ncur = q.nnxt
|
|
|
|
q.nnxt = 1
|
|
|
|
|
|
|
|
--local tnext = class+1
|
|
|
|
qnext = scheduler.queue[class+1]
|
|
|
|
if qnext then
|
|
|
|
local src = qnext.cur
|
|
|
|
for i,j in pairs(src) do
|
|
|
|
q.cur[q.ncur] = j
|
|
|
|
q.ncur = q.ncur+1
|
|
|
|
src[i] = nil
|
|
|
|
end
|
|
|
|
qnext.cur = {}
|
|
|
|
qnext.ncur = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
--[[
|
2016-08-21 18:10:21 +02:00
|
|
|
q.groups[q.first] = q.groups[q.last]
|
|
|
|
q.groups[q.last] = {}
|
|
|
|
local tnext = class+1
|
|
|
|
tnext = scheduler.queue[tnext]
|
|
|
|
if tnext then
|
|
|
|
tsrc = tnext.groups[tnext.first]
|
|
|
|
tdst = q.groups[q.first]
|
|
|
|
for i,j in pairs(tsrc) do
|
|
|
|
tdst[#tdst+1] = j
|
|
|
|
tsrc[i] = nil
|
|
|
|
end
|
|
|
|
end
|
2016-10-29 16:25:57 +02:00
|
|
|
end]]
|
2016-08-21 18:10:21 +02:00
|
|
|
if nb ~= scheduler.waitingjobs() then --This should never happen, left because it was used during debug phase
|
|
|
|
mdebug("ERROR, This should never happen ! Lost jobs, some mod may not work from now, please restart the server.")
|
|
|
|
end
|
|
|
|
--
|
|
|
|
end
|
|
|
|
|
|
|
|
function scheduler.fulldebug()
|
2016-10-29 16:25:57 +02:00
|
|
|
minetest.log("[Profnsched]"..#scheduler.queue.." classes")
|
2016-08-21 18:10:21 +02:00
|
|
|
for class,q in pairs(scheduler.queue) do
|
|
|
|
minetest.log("[Profnsched] class "..class..":")
|
2016-10-29 16:25:57 +02:00
|
|
|
minetest.log("[Profnsched] current "..q.ncur)
|
|
|
|
minetest.log("[Profnsched] next "..q.nnxt)
|
2016-08-21 18:10:21 +02:00
|
|
|
end
|
|
|
|
minetest.log("[Profnsched] end")
|
|
|
|
end
|
|
|
|
|
2016-10-29 16:25:57 +02:00
|
|
|
|
2016-08-21 18:10:21 +02:00
|
|
|
function scheduler.waitingjobs()
|
|
|
|
local n = 0
|
|
|
|
for class, q in pairs(scheduler.queue) do
|
2016-10-29 16:25:57 +02:00
|
|
|
for i,grp in pairs(q.cur) do
|
|
|
|
n = n+1
|
|
|
|
end
|
|
|
|
for i,grp in pairs(q.nxt) do
|
|
|
|
n = n+1
|
2016-08-21 18:10:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return n
|
|
|
|
end
|