From 55d521bc506a23414e07e38d1af8848659ae777b Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sat, 13 Apr 2024 17:05:30 +0000 Subject: [PATCH] Use new prefix for modules, remove test, make scheduler use priority bins other than the first --- mods/CORE/mcl_scheduler/init.lua | 92 ------------------- .../{mcl_scheduler => vl_scheduler}/fifo.lua | 2 +- mods/CORE/vl_scheduler/init.lua | 58 ++++++++++++ mods/CORE/vl_scheduler/mod.conf | 4 + .../{mcl_scheduler => vl_scheduler}/queue.lua | 2 +- 5 files changed, 64 insertions(+), 94 deletions(-) delete mode 100644 mods/CORE/mcl_scheduler/init.lua rename mods/CORE/{mcl_scheduler => vl_scheduler}/fifo.lua (96%) create mode 100644 mods/CORE/vl_scheduler/init.lua create mode 100644 mods/CORE/vl_scheduler/mod.conf rename mods/CORE/{mcl_scheduler => vl_scheduler}/queue.lua (99%) diff --git a/mods/CORE/mcl_scheduler/init.lua b/mods/CORE/mcl_scheduler/init.lua deleted file mode 100644 index 71377c84a..000000000 --- a/mods/CORE/mcl_scheduler/init.lua +++ /dev/null @@ -1,92 +0,0 @@ -local modname = minetest.get_current_modname() -local modpath = minetest.get_modpath(modname) - -mcl_scheduler = {} -local mod = mcl_scheduler - -dofile(modpath.."/queue.lua") -dofile(modpath.."/fifo.lua") - -function mod.test() - local t = mod.queue.new() - - local test_times = { 15, 1, 7, 34, 50, 150, 14, 18, 20, 20, 15, } - - local start_time = minetest.get_us_time() - for _,time in pairs(test_times) do - t:add_task({ time = time }) - - local stop_time = minetest.get_us_time() - print("took "..tostring(stop_time - start_time).."us") - start_time = stop_time - end - - print(dump(t:tick())) - print(dump(t)) - - local start_time = minetest.get_us_time() - for i=1,60 do - local s = t:tick() - print("time="..tostring(i+1)) - print(dump(s)) - - local stop_time = minetest.get_us_time() - print("took "..tostring(stop_time - start_time).."us") - start_time = stop_time - end -end - -local run_queues = {} -for i = 1,4 do - run_queues[i] = mod.fifo:new() -end -local time = 0 -local priority_queue = mod.queue:new() -local functions = {} - -print(dump(run_queues)) - -minetest.register_globalstep(function(dtime) - local start_time = minetest.get_us_time() - time = time + dtime - local limit = 4 - while time > 0.05 and limit > 0 do - -- Add tasks to the run queues - local iter = priority_queue:tick() - while iter do - local task = iter - iter = iter.next - - local priority = task.priority or 3 - run_queues[priority]:insert(task) - end - - -- Run tasks until we run out of timeslice - local i = 1 - while i < 4 and (minetest.get_us_time() - start_time) < 50000 do - local task = run_queues[i]:get() - if task then - print("Running task "..dump(task)) - local func = functions[task.fid] - local cancel = false - if func then - local err - cancel,err = pcall(func, task.dtime, table.unpack(task.args)) - if err then - minetest.log("error","Error while running task: err") - end - end - - -- Add - if task.period and not cancel then - task.time = task.period - priority_queue:add_task(task) - end - end - end - - time = time - 0.05 - limit = limit - 1 - end -end) - diff --git a/mods/CORE/mcl_scheduler/fifo.lua b/mods/CORE/vl_scheduler/fifo.lua similarity index 96% rename from mods/CORE/mcl_scheduler/fifo.lua rename to mods/CORE/vl_scheduler/fifo.lua index 5b2b47d35..92cfabb3c 100644 --- a/mods/CORE/mcl_scheduler/fifo.lua +++ b/mods/CORE/vl_scheduler/fifo.lua @@ -1,4 +1,4 @@ -local mod = mcl_scheduler +local mod = vl_scheduler function Class() local cls = {} diff --git a/mods/CORE/vl_scheduler/init.lua b/mods/CORE/vl_scheduler/init.lua new file mode 100644 index 000000000..260316223 --- /dev/null +++ b/mods/CORE/vl_scheduler/init.lua @@ -0,0 +1,58 @@ +local modname = minetest.get_current_modname() +local modpath = minetest.get_modpath(modname) + +vl_scheduler = {} +local mod = vl_scheduler + +dofile(modpath.."/queue.lua") +dofile(modpath.."/fifo.lua") + +local run_queues = {} +for i = 1,4 do + run_queues[i] = mod.fifo:new() +end +local time = 0 +local priority_queue = mod.queue:new() +local functions = {} + +minetest.register_globalstep(function(dtime) + local start_time = minetest.get_us_time() + time = time + dtime + + -- Add tasks to the run queues + local iter = priority_queue:tick() + while iter do + local task = iter + iter = iter.next + + local priority = task.priority or 3 + run_queues[priority]:insert(task) + end + + -- Run tasks until we run out of timeslice + local i = 1 + while i < 4 and (minetest.get_us_time() - start_time) < 50000 do + local task = run_queues[i]:get() + if task then + print("Running task "..dump(task)) + local func = functions[task.fid] + local cancel = false + if func then + local err + cancel,err = pcall(func, task.dtime, table.unpack(task.args)) + if err then + minetest.log("error","Error while running task: err") + end + end + + -- Add periodic tasks back into the queue + if task.period and not cancel then + task.time = task.period + priority_queue:add_task(task) + end + else + i = i + 1 + end + end +end) + diff --git a/mods/CORE/vl_scheduler/mod.conf b/mods/CORE/vl_scheduler/mod.conf new file mode 100644 index 000000000..8757ae7e8 --- /dev/null +++ b/mods/CORE/vl_scheduler/mod.conf @@ -0,0 +1,4 @@ +name = mcl_scheduler +author = teknomunk +description = Event and Process Scheduler +depends = mcl_util diff --git a/mods/CORE/mcl_scheduler/queue.lua b/mods/CORE/vl_scheduler/queue.lua similarity index 99% rename from mods/CORE/mcl_scheduler/queue.lua rename to mods/CORE/vl_scheduler/queue.lua index 1646fa5bb..3f00f9ab8 100644 --- a/mods/CORE/mcl_scheduler/queue.lua +++ b/mods/CORE/vl_scheduler/queue.lua @@ -1,4 +1,4 @@ -local mod = mcl_scheduler +local mod = vl_scheduler --[[