Small: Redo to async

This commit is contained in:
Coder12a 2019-04-18 16:04:17 -05:00
parent 977c19efde
commit 8ca43c745a

View File

@ -8,79 +8,58 @@ end
function extended_api.Async() function extended_api.Async()
local self = {} local self = {}
self.pool = {threads = {}, globalstep_threads = {}, task_queue = {}, resting = 200, maxtime = 200, queue_threads = 8, state = "suspended"} self.task_queue = {}
self.resting = 200
self.maxtime = 200
self.queue_threads = 8
self.state = "suspended"
self.create_worker = function(func) self.create_worker = function(func)
local thread = coroutine.create(func) local thread = coroutine.create(func)
if not thread or coroutine.status(thread) == "dead" then if not thread or coroutine.status(thread) == "dead" then
minetest.after(0.3, self.create_worker, func) minetest.after(0.3, self.create_worker, func)
minetest.after(0.5, self.schedule_worker)
minetest.chat_send_all("Fall")
return return
end end
table.insert(self.pool.threads, thread) self.run_worker(thread)
end end
self.create_globalstep_worker = function(func) self.create_globalstep_worker = function(func)
local thread = coroutine.create(func) local thread = coroutine.create(func)
if not thread or coroutine.status(thread) == "dead" then if not thread or coroutine.status(thread) == "dead" then
minetest.after(0.3, self.create_globalstep_worker, func) minetest.after(0.3, self.create_globalstep_worker, func)
minetest.after(0.5, self.schedule_globalstep_worker)
return return
end end
table.insert(self.pool.globalstep_threads, thread) self.run_globalstep_worker(thread)
end end
self.run_worker = function(index) self.run_worker = function(thread)
local thread = self.pool.threads[index]
if not thread or coroutine.status(thread) == "dead" then if not thread or coroutine.status(thread) == "dead" then
table.remove(self.pool.threads, index)
return false return false
else else
coroutine.resume(thread) coroutine.resume(thread)
minetest.after(0, self.schedule_worker) minetest.after(self.resting / 1000, self.run_worker, thread)
return true return true
end end
end end
self.run_globalstep_worker = function(index) self.run_globalstep_worker = function(thread)
local thread = self.pool.globalstep_threads[index]
if not thread or coroutine.status(thread) == "dead" then if not thread or coroutine.status(thread) == "dead" then
table.remove(self.pool.globalstep_threads, index)
return false return false
else else
coroutine.resume(thread) coroutine.resume(thread)
minetest.after(0, self.schedule_globalstep_worker) minetest.after(0, self.run_globalstep_worker, thread)
return true return true
end end
end end
self.schedule_worker = function()
self.pool.state = "running"
for index, value in ipairs(self.pool.threads) do
minetest.after(self.pool.resting / 1000, self.run_worker, index)
return true
end
self.pool.state = "suspended"
return false
end
self.schedule_globalstep_worker = function()
for index, value in ipairs(self.pool.globalstep_threads) do
minetest.after(0, self.run_globalstep_worker, index)
return true
end
return false
end
self.priority = function(resting, maxtime) self.priority = function(resting, maxtime)
self.pool.resting = resting self.resting = resting
self.pool.maxtime = maxtime self.maxtime = maxtime
end end
self.iterate = function(from, to, func, callback) self.iterate = function(from, to, func, callback)
self.create_worker(function() self.create_worker(function()
local last_time = minetest.get_us_time() / 1000 local last_time = minetest.get_us_time() / 1000
local maxtime = self.pool.maxtime local maxtime = self.maxtime
for i = from, to do for i = from, to do
local b = func(i) local b = func(i)
if b ~= nil and b == false then if b ~= nil and b == false then
@ -96,13 +75,12 @@ function extended_api.Async()
end end
return return
end) end)
self.schedule_worker()
end end
self.foreach = function(array, func, callback) self.foreach = function(array, func, callback)
self.create_worker(function() self.create_worker(function()
local last_time = minetest.get_us_time() / 1000 local last_time = minetest.get_us_time() / 1000
local maxtime = self.pool.maxtime local maxtime = self.maxtime
for k,v in ipairs(array) do for k,v in ipairs(array) do
local b = func(k,v) local b = func(k,v)
if b ~= nil and b == false then if b ~= nil and b == false then
@ -118,13 +96,12 @@ function extended_api.Async()
end end
return return
end) end)
self.schedule_worker()
end end
self.do_while = function(condition_func, func, callback) self.do_while = function(condition_func, func, callback)
self.create_worker(function() self.create_worker(function()
local last_time = minetest.get_us_time() / 1000 local last_time = minetest.get_us_time() / 1000
local maxtime = self.pool.maxtime local maxtime = self.maxtime
while(condition_func()) do while(condition_func()) do
local c = func() local c = func()
if c ~= nil and c ~= condition_func() then if c ~= nil and c ~= condition_func() then
@ -140,7 +117,6 @@ function extended_api.Async()
end end
return return
end) end)
self.schedule_worker()
end end
self.register_globalstep = function(func) self.register_globalstep = function(func)
@ -157,14 +133,13 @@ function extended_api.Async()
end end
end end
end) end)
self.schedule_globalstep_worker()
end end
self.chain_task = function(tasks, callback) self.chain_task = function(tasks, callback)
self.create_worker(function() self.create_worker(function()
local pass_arg = nil local pass_arg = nil
local last_time = minetest.get_us_time() / 1000 local last_time = minetest.get_us_time() / 1000
local maxtime = self.pool.maxtime local maxtime = self.maxtime
for index, task_func in pairs(tasks) do for index, task_func in pairs(tasks) do
local p = task_func(pass_arg) local p = task_func(pass_arg)
if p ~= nil then if p ~= nil then
@ -180,20 +155,19 @@ function extended_api.Async()
end end
return return
end) end)
self.schedule_worker()
end end
self.queue_task = function(func, callback) self.queue_task = function(func, callback)
table.insert(self.pool.task_queue, {func = func,callback = callback}) table.insert(self.task_queue, {func = func,callback = callback})
if self.pool.queue_threads > 0 then if self.queue_threads > 0 then
self.pool.queue_threads = self.pool.queue_threads - 1 self.queue_threads = self.queue_threads - 1
self.create_worker(function() self.create_worker(function()
local pass_arg = nil local pass_arg = nil
local last_time = minetest.get_us_time() / 1000 local last_time = minetest.get_us_time() / 1000
local maxtime = self.pool.maxtime local maxtime = self.maxtime
while(true) do while(true) do
local task_func = self.pool.task_queue[1] local task_func = self.task_queue[1]
table.remove(self.pool.task_queue, 1) table.remove(self.task_queue, 1)
if task_func and task_func.func then if task_func and task_func.func then
pass_arg = nil pass_arg = nil
local p = task_func.func() local p = task_func.func()
@ -208,12 +182,11 @@ function extended_api.Async()
last_time = minetest.get_us_time() / 1000 last_time = minetest.get_us_time() / 1000
end end
else else
self.pool.queue_threads = self.pool.queue_threads + 1 self.queue_threads = self.queue_threads + 1
return return
end end
end end
end) end)
self.schedule_worker()
end end
end end
@ -228,7 +201,6 @@ function extended_api.Async()
end end
return return
end) end)
self.schedule_worker()
end end
return self return self
end end