Create async pools
Bug fix in do_while. Fix time in schedule taking too long.
This commit is contained in:
parent
fa7e2cd107
commit
90ea9b9b60
14
api.txt
14
api.txt
@ -5,14 +5,16 @@ on_construct_node_near_by(pos,other_pos)
|
||||
on_destruct_node_near_by(pos,other_pos)
|
||||
-- async
|
||||
--
|
||||
minetest.async.priority(resting,maxtime)
|
||||
minetest.Async.create_async_pool() --- return pool
|
||||
--
|
||||
minetest.async.iterate(from,to,func,callback)
|
||||
minetest.Async.priority(pool,resting,maxtime)
|
||||
--
|
||||
minetest.async.foreach(array, func, callback)
|
||||
minetest.Async.iterate(pool,from,to,func,callback)
|
||||
--
|
||||
minetest.async.do_while(condition, func, callback)
|
||||
minetest.Async.foreach(pool,array, func, callback)
|
||||
--
|
||||
minetest.async.register_globalstep(func)
|
||||
minetest.Async.do_while(pool,condition_func, func, callback)
|
||||
--
|
||||
minetest.async.queue_task(tasks,callback)
|
||||
minetest.Async.register_globalstep(pool,func)
|
||||
--
|
||||
minetest.Async.queue_task(pool,tasks,callback)
|
109
async.lua
109
async.lua
@ -1,74 +1,73 @@
|
||||
minetest.async = {}
|
||||
minetest.Async = {}
|
||||
|
||||
minetest.async.threads = {}
|
||||
minetest.async.globalstep_threads = {}
|
||||
minetest.async.resting = 200
|
||||
minetest.async.maxtime = 200
|
||||
minetest.async.state = "suspended"
|
||||
|
||||
function minetest.async.create_worker(func)
|
||||
local thread = coroutine.create(func)
|
||||
table.insert(minetest.async.threads, thread)
|
||||
function minetest.Async.create_async_pool()
|
||||
local pool = {threads = {},globalstep_threads = {},resting = 200,maxtime = 200,state = "suspended"}
|
||||
return pool
|
||||
end
|
||||
|
||||
function minetest.async.create_globalstep_worker(func)
|
||||
function minetest.Async.create_worker(pool,func)
|
||||
local thread = coroutine.create(func)
|
||||
table.insert(minetest.async.globalstep_threads, thread)
|
||||
table.insert(pool.threads, thread)
|
||||
end
|
||||
|
||||
function minetest.async.run_worker(index)
|
||||
local thread = minetest.async.threads[index]
|
||||
function minetest.Async.create_globalstep_worker(pool,func)
|
||||
local thread = coroutine.create(func)
|
||||
table.insert(pool.globalstep_threads, thread)
|
||||
end
|
||||
|
||||
function minetest.Async.run_worker(pool,index)
|
||||
local thread = pool.threads[index]
|
||||
if thread == nil or coroutine.status(thread) == "dead" then
|
||||
table.remove(minetest.async.threads, index)
|
||||
minetest.after(0,minetest.async.schedule_worker)
|
||||
table.remove(pool.threads, index)
|
||||
minetest.after(0,minetest.Async.schedule_worker,pool)
|
||||
return false
|
||||
else
|
||||
coroutine.resume(thread)
|
||||
minetest.after(0,minetest.async.schedule_worker)
|
||||
minetest.after(0,minetest.Async.schedule_worker,pool)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
function minetest.async.run_globalstep_worker(index)
|
||||
local thread = minetest.async.globalstep_threads[index]
|
||||
function minetest.Async.run_globalstep_worker(pool,index)
|
||||
local thread = pool.globalstep_threads[index]
|
||||
if thread == nil or coroutine.status(thread) == "dead" then
|
||||
table.remove(minetest.async.globalstep_threads, index)
|
||||
minetest.after(0,minetest.async.schedule_globalstep_worker)
|
||||
table.remove(pool.globalstep_threads, index)
|
||||
minetest.after(0,minetest.Async.schedule_globalstep_worker,pool)
|
||||
return false
|
||||
else
|
||||
coroutine.resume(thread)
|
||||
minetest.after(0,minetest.async.schedule_globalstep_worker)
|
||||
minetest.after(0,minetest.Async.schedule_globalstep_worker,pool)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
function minetest.async.schedule_worker()
|
||||
minetest.async.state = "running"
|
||||
for index,value in ipairs(minetest.async.threads) do
|
||||
minetest.after(minetest.async.resting,minetest.async.run_worker,index)
|
||||
function minetest.Async.schedule_worker(pool)
|
||||
pool.state = "running"
|
||||
for index,value in ipairs(pool.threads) do
|
||||
minetest.after(pool.resting / 1000,minetest.Async.run_worker,pool,index)
|
||||
return true
|
||||
end
|
||||
minetest.async.state = "suspended"
|
||||
pool.state = "suspended"
|
||||
return false
|
||||
end
|
||||
|
||||
function minetest.async.schedule_globalstep_worker()
|
||||
for index,value in ipairs(minetest.async.globalstep_threads) do
|
||||
minetest.after(0,minetest.async.run_globalstep_worker,index)
|
||||
function minetest.Async.schedule_globalstep_worker(pool)
|
||||
for index,value in ipairs(pool.globalstep_threads) do
|
||||
minetest.after(0,minetest.Async.run_globalstep_worker,pool,index)
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function minetest.async.priority(resting,maxtime)
|
||||
minetest.async.resting = resting
|
||||
minetest.async.maxtime = maxtime
|
||||
function minetest.Async.priority(pool,resting,maxtime)
|
||||
pool.resting = resting
|
||||
pool.maxtime = maxtime
|
||||
end
|
||||
|
||||
function minetest.async.iterate(from,to,func,callback)
|
||||
minetest.async.create_worker(function()
|
||||
function minetest.Async.iterate(pool,from,to,func,callback)
|
||||
minetest.Async.create_worker(pool,function()
|
||||
local last_time = minetest.get_us_time() * 1000
|
||||
local maxtime = minetest.async.maxtime
|
||||
local maxtime = pool.maxtime
|
||||
for i = from, to do
|
||||
func(i)
|
||||
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
||||
@ -80,13 +79,13 @@ function minetest.async.iterate(from,to,func,callback)
|
||||
callback()
|
||||
end
|
||||
end)
|
||||
minetest.async.schedule_worker()
|
||||
minetest.Async.schedule_worker(pool)
|
||||
end
|
||||
|
||||
function minetest.async.foreach(array, func, callback)
|
||||
minetest.async.create_worker(function()
|
||||
function minetest.Async.foreach(pool,array, func, callback)
|
||||
minetest.Async.create_worker(pool,function()
|
||||
local last_time = minetest.get_us_time() * 1000
|
||||
local maxtime = minetest.async.maxtime
|
||||
local maxtime = pool.maxtime
|
||||
for k,v in ipairs(array) do
|
||||
func(k,v)
|
||||
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
||||
@ -98,16 +97,16 @@ function minetest.async.foreach(array, func, callback)
|
||||
callback()
|
||||
end
|
||||
end)
|
||||
minetest.async.schedule_worker()
|
||||
minetest.Async.schedule_worker(pool)
|
||||
end
|
||||
|
||||
function minetest.async.do_while(condition, func, callback)
|
||||
minetest.async.create_worker(function()
|
||||
function minetest.Async.do_while(pool,condition_func, func, callback)
|
||||
minetest.Async.create_worker(pool,function()
|
||||
local last_time = minetest.get_us_time() * 1000
|
||||
local maxtime = minetest.async.maxtime
|
||||
while(condition) do
|
||||
local maxtime = pool.maxtime
|
||||
while(condition_func()) do
|
||||
local c = func()
|
||||
if c and c ~= condition then
|
||||
if c and c ~= condition_func() then
|
||||
break
|
||||
end
|
||||
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
||||
@ -119,11 +118,11 @@ function minetest.async.do_while(condition, func, callback)
|
||||
callback()
|
||||
end
|
||||
end)
|
||||
minetest.async.schedule_worker()
|
||||
minetest.Async.schedule_worker(pool)
|
||||
end
|
||||
|
||||
function minetest.async.register_globalstep(func)
|
||||
minetest.async.create_globalstep_worker(function()
|
||||
function minetest.Async.register_globalstep(pool,func)
|
||||
minetest.Async.create_globalstep_worker(pool,function()
|
||||
local last_time = minetest.get_us_time() * 1000
|
||||
local dtime = last_time
|
||||
while(true) do
|
||||
@ -139,15 +138,15 @@ function minetest.async.register_globalstep(func)
|
||||
end
|
||||
end
|
||||
end)
|
||||
minetest.async.schedule_globalstep_worker()
|
||||
minetest.Async.schedule_globalstep_worker(pool)
|
||||
end
|
||||
|
||||
function minetest.async.queue_task(tasks,callback)
|
||||
minetest.async.create_worker(function()
|
||||
function minetest.Async.queue_task(pool,tasks,callback)
|
||||
minetest.Async.create_worker(pool,function()
|
||||
local pass_arg = {}
|
||||
local last_time = minetest.get_us_time() * 1000
|
||||
local maxtime = minetest.async.maxtime
|
||||
for task_func, index in pairs(tasks) do
|
||||
local maxtime = pool.maxtime
|
||||
for index, task_func in pairs(tasks) do
|
||||
local p = task_func(pass_arg)
|
||||
if p then
|
||||
pass_arg = p
|
||||
@ -161,5 +160,5 @@ function minetest.async.queue_task(tasks,callback)
|
||||
callback(pass_arg)
|
||||
end
|
||||
end)
|
||||
minetest.async.schedule_worker()
|
||||
minetest.Async.schedule_worker(pool)
|
||||
end
|
Loading…
Reference in New Issue
Block a user