From 9aaba0eca6773f60ba77b10f95ff19e66fdff6ea Mon Sep 17 00:00:00 2001 From: Coder12a <38924418+Coder12a@users.noreply.github.com> Date: Sun, 21 Jul 2019 20:03:09 -0500 Subject: [PATCH] Update: mod --- README.md | 38 +++++++++++++++------------- async.lua | 75 +++++++++++++++++++++++++------------------------------ 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 9d7ffc9..034b3db 100644 --- a/README.md +++ b/README.md @@ -10,38 +10,42 @@ Usage Async ```lua async = async.Async() ``` -2. set the priority of the async pool to high. +2. max time for thread before yielding (maxtime is in milliseconds). ```lua -async.priority(50, 500) +async.maxtime = 200 ``` -3. iterate from 1 to 50 and log the value i. +3. queue_threads is the amount of active threads when you run function queue_task. +```lua +async.queue_threads = 8 +``` +4. iterate from 1 to 50 and log the value i. ```lua async.iterate(1, 50, function(i) minetest.log(i) -end) +end, function() minetest.log("Callback") end) ``` -4. run throught each element in a table. +5. run throught each element in a table. ```lua local array = {"start", "text2", "text3", "text4", "text5", "end"} -async.foreach(array, function(k,v) +async.foreach(array, function(k, v) minetest.log(v) -end) +end, function() minetest.log("Callback") end) ``` -5. async do while loop. +6. async do while loop. ```lua local c = 50 -async.do_while(function() return c>0 end, function() +async.do_while(function() return c > 0 end, function() minetest.log(c) c = c - 1 -end) +end, function() minetest.log("Callback") end) ``` -6. register a async globalstep. this one spams the chat with the word spam. +7. register a async globalstep. this one spams the chat with the word spam. ```lua async.register_globalstep(function(dtime) minetest.chat_send_all("spam") end) ``` -7. chain task runs a group of functions from a table. +8. chain task runs a group of functions from a table. ```lua async.chain_task({ function(args) @@ -53,17 +57,17 @@ async.chain_task({ args.count = args.count + 1 minetest.log(args.count) return args -end}) +end}, function(args) minetest.log(args.count) end) ``` -8. adds a single function to the task queue. This is a sort of waiting list. +9. adds a single function to the task queue. This is a sort of waiting list. ```lua async.queue_task(function() minetest.log("Hello World!") -end) +end, function(args) minetest.log("callback") end) ``` -9. Same as queue_task but the task does not go into a queue. +10. Same as queue_task but the task does not go into a queue. ```lua async.single_task(function() minetest.log("Hello World!") -end) +end, function(args) minetest.log("callback") end) ``` diff --git a/async.lua b/async.lua index 3e63c9c..fe50420 100644 --- a/async.lua +++ b/async.lua @@ -1,54 +1,53 @@ +local threads = {} +local time = 0 +minetest.register_globalstep(function(dtime) + time = time + dtime + if #threads < 1 then + return + end + for i = #threads, 1, -1 do + if time >= threads[i].delay then + local thread = threads[i].thread + local state = coroutine.status(thread) + if state == "dead" then + table.remove(threads, i) + elseif state == "suspended" then + coroutine.resume(thread) + threads[i].delay = time + threads[i].delaymax + end + end + end +end) + function async.Async() local self = {} - self.task_queue = {} - self.resting = 200 self.maxtime = 200 self.queue_threads = 8 - self.state = "suspended" - self.create_worker = function(func) local thread = coroutine.create(func) if not thread or coroutine.status(thread) == "dead" then - minetest.after(0.3, self.create_worker, func) + minetest.after(0.5, self.create_worker, func) return end - self.run_worker(thread) + threads[#threads + 1] = { + thread = thread, + delay = time + self.maxtime / 1000, + delaymax = self.maxtime / 1000 + } end - self.create_globalstep_worker = function(func) local thread = coroutine.create(func) if not thread or coroutine.status(thread) == "dead" then - minetest.after(0.3, self.create_globalstep_worker, func) + minetest.after(0.5, self.create_globalstep_worker, func) return end - self.run_globalstep_worker(thread) + threads[#threads + 1] = { + thread = thread, + delay = time, + delaymax = 0 + } end - self.run_worker = function(thread) - if not thread or coroutine.status(thread) == "dead" then - return false - else - coroutine.resume(thread) - minetest.after(self.resting / 1000, self.run_worker, thread) - return true - end - end - - self.run_globalstep_worker = function(thread) - if not thread or coroutine.status(thread) == "dead" then - return false - else - coroutine.resume(thread) - minetest.after(0, self.run_globalstep_worker, thread) - return true - end - end - - self.priority = function(resting, maxtime) - self.resting = resting - self.maxtime = maxtime - end - self.iterate = function(from, to, func, callback) self.create_worker(function() local last_time = minetest.get_us_time() / 1000 @@ -69,13 +68,12 @@ function async.Async() return end) end - self.foreach = function(array, func, callback) self.create_worker(function() local last_time = minetest.get_us_time() / 1000 local maxtime = self.maxtime for k,v in ipairs(array) do - local b = func(k,v) + local b = func(k, v) if b ~= nil and b == false then break end @@ -90,7 +88,6 @@ function async.Async() return end) end - self.do_while = function(condition_func, func, callback) self.create_worker(function() local last_time = minetest.get_us_time() / 1000 @@ -111,7 +108,6 @@ function async.Async() return end) end - self.register_globalstep = function(func) self.create_globalstep_worker(function() local last_time = minetest.get_us_time() / 1000000 @@ -127,7 +123,6 @@ function async.Async() end end) end - self.chain_task = function(tasks, callback) self.create_worker(function() local pass_arg = nil @@ -149,7 +144,6 @@ function async.Async() return end) end - self.queue_task = function(func, callback) table.insert(self.task_queue, {func = func, callback = callback}) if self.queue_threads > 0 then @@ -182,7 +176,6 @@ function async.Async() end) end end - self.single_task = function(func, callback) self.create_worker(function() local pass_arg = func()