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)
|
on_destruct_node_near_by(pos,other_pos)
|
||||||
-- async
|
-- 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 = {}
|
function minetest.Async.create_async_pool()
|
||||||
minetest.async.globalstep_threads = {}
|
local pool = {threads = {},globalstep_threads = {},resting = 200,maxtime = 200,state = "suspended"}
|
||||||
minetest.async.resting = 200
|
return pool
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.create_globalstep_worker(func)
|
function minetest.Async.create_worker(pool,func)
|
||||||
local thread = coroutine.create(func)
|
local thread = coroutine.create(func)
|
||||||
table.insert(minetest.async.globalstep_threads, thread)
|
table.insert(pool.threads, thread)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.run_worker(index)
|
function minetest.Async.create_globalstep_worker(pool,func)
|
||||||
local thread = minetest.async.threads[index]
|
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
|
if thread == nil or coroutine.status(thread) == "dead" then
|
||||||
table.remove(minetest.async.threads, index)
|
table.remove(pool.threads, index)
|
||||||
minetest.after(0,minetest.async.schedule_worker)
|
minetest.after(0,minetest.Async.schedule_worker,pool)
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
coroutine.resume(thread)
|
coroutine.resume(thread)
|
||||||
minetest.after(0,minetest.async.schedule_worker)
|
minetest.after(0,minetest.Async.schedule_worker,pool)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.run_globalstep_worker(index)
|
function minetest.Async.run_globalstep_worker(pool,index)
|
||||||
local thread = minetest.async.globalstep_threads[index]
|
local thread = pool.globalstep_threads[index]
|
||||||
if thread == nil or coroutine.status(thread) == "dead" then
|
if thread == nil or coroutine.status(thread) == "dead" then
|
||||||
table.remove(minetest.async.globalstep_threads, index)
|
table.remove(pool.globalstep_threads, index)
|
||||||
minetest.after(0,minetest.async.schedule_globalstep_worker)
|
minetest.after(0,minetest.Async.schedule_globalstep_worker,pool)
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
coroutine.resume(thread)
|
coroutine.resume(thread)
|
||||||
minetest.after(0,minetest.async.schedule_globalstep_worker)
|
minetest.after(0,minetest.Async.schedule_globalstep_worker,pool)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.schedule_worker()
|
function minetest.Async.schedule_worker(pool)
|
||||||
minetest.async.state = "running"
|
pool.state = "running"
|
||||||
for index,value in ipairs(minetest.async.threads) do
|
for index,value in ipairs(pool.threads) do
|
||||||
minetest.after(minetest.async.resting,minetest.async.run_worker,index)
|
minetest.after(pool.resting / 1000,minetest.Async.run_worker,pool,index)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
minetest.async.state = "suspended"
|
pool.state = "suspended"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.schedule_globalstep_worker()
|
function minetest.Async.schedule_globalstep_worker(pool)
|
||||||
for index,value in ipairs(minetest.async.globalstep_threads) do
|
for index,value in ipairs(pool.globalstep_threads) do
|
||||||
minetest.after(0,minetest.async.run_globalstep_worker,index)
|
minetest.after(0,minetest.Async.run_globalstep_worker,pool,index)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.priority(resting,maxtime)
|
function minetest.Async.priority(pool,resting,maxtime)
|
||||||
minetest.async.resting = resting
|
pool.resting = resting
|
||||||
minetest.async.maxtime = maxtime
|
pool.maxtime = maxtime
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.iterate(from,to,func,callback)
|
function minetest.Async.iterate(pool,from,to,func,callback)
|
||||||
minetest.async.create_worker(function()
|
minetest.Async.create_worker(pool,function()
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = minetest.async.maxtime
|
local maxtime = pool.maxtime
|
||||||
for i = from, to do
|
for i = from, to do
|
||||||
func(i)
|
func(i)
|
||||||
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
||||||
@ -80,13 +79,13 @@ function minetest.async.iterate(from,to,func,callback)
|
|||||||
callback()
|
callback()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.async.schedule_worker()
|
minetest.Async.schedule_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.foreach(array, func, callback)
|
function minetest.Async.foreach(pool,array, func, callback)
|
||||||
minetest.async.create_worker(function()
|
minetest.Async.create_worker(pool,function()
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = minetest.async.maxtime
|
local maxtime = pool.maxtime
|
||||||
for k,v in ipairs(array) do
|
for k,v in ipairs(array) do
|
||||||
func(k,v)
|
func(k,v)
|
||||||
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
||||||
@ -98,16 +97,16 @@ function minetest.async.foreach(array, func, callback)
|
|||||||
callback()
|
callback()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.async.schedule_worker()
|
minetest.Async.schedule_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.do_while(condition, func, callback)
|
function minetest.Async.do_while(pool,condition_func, func, callback)
|
||||||
minetest.async.create_worker(function()
|
minetest.Async.create_worker(pool,function()
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = minetest.async.maxtime
|
local maxtime = pool.maxtime
|
||||||
while(condition) do
|
while(condition_func()) do
|
||||||
local c = func()
|
local c = func()
|
||||||
if c and c ~= condition then
|
if c and c ~= condition_func() then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
if minetest.get_us_time() * 1000 > last_time + maxtime then
|
||||||
@ -119,11 +118,11 @@ function minetest.async.do_while(condition, func, callback)
|
|||||||
callback()
|
callback()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.async.schedule_worker()
|
minetest.Async.schedule_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.register_globalstep(func)
|
function minetest.Async.register_globalstep(pool,func)
|
||||||
minetest.async.create_globalstep_worker(function()
|
minetest.Async.create_globalstep_worker(pool,function()
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local dtime = last_time
|
local dtime = last_time
|
||||||
while(true) do
|
while(true) do
|
||||||
@ -139,15 +138,15 @@ function minetest.async.register_globalstep(func)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.async.schedule_globalstep_worker()
|
minetest.Async.schedule_globalstep_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.async.queue_task(tasks,callback)
|
function minetest.Async.queue_task(pool,tasks,callback)
|
||||||
minetest.async.create_worker(function()
|
minetest.Async.create_worker(pool,function()
|
||||||
local pass_arg = {}
|
local pass_arg = {}
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = minetest.async.maxtime
|
local maxtime = pool.maxtime
|
||||||
for task_func, index 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 then
|
if p then
|
||||||
pass_arg = p
|
pass_arg = p
|
||||||
@ -161,5 +160,5 @@ function minetest.async.queue_task(tasks,callback)
|
|||||||
callback(pass_arg)
|
callback(pass_arg)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.async.schedule_worker()
|
minetest.Async.schedule_worker(pool)
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user