From 0af807a930ef1bd7239076af1c8d9e3bd78481fa Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Thu, 2 Nov 2023 09:31:09 -0400 Subject: [PATCH] Fixed newTask() --- docs/changes.md | 2 ++ init.lua | 36 +++++++++++++++++++----------------- tests/test.lua | 7 +++++++ 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index d638d78..e835fcd 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -523,6 +523,8 @@ Added Changed --- +- multi:newTask(task) is not tied to the processor it is created on. +- `multi:getTasks()` renamed to `multi:getRunners()`, should help with confusion between multi:newTask() - changed how multi adds unpack to the global namespace. Instead we capture that value into multi.unpack. - multi:newUpdater(skip, func) -- Now accepts func as the second argument. So you don't need to call OnUpdate(func) after creation. - multi errors now internally call `multi.error` instead of `multi.print` diff --git a/init.lua b/init.lua index 709ae0c..c0f75a6 100644 --- a/init.lua +++ b/init.lua @@ -1011,10 +1011,10 @@ function multi:newTStep(start,reset,count,set) return c end -local tasks = {} +multi.tasks = {} function multi:newTask(func) - tasks[#tasks + 1] = func + self.tasks[#self.tasks + 1] = func end local scheduledjobs = {} @@ -1086,6 +1086,7 @@ function multi:newProcessor(name, nothread, priority) c.Type = multi.registerType("process", "processes") local Active = nothread or false c.Name = name or "" + c.tasks = {} c.threads = {} c.startme = {} c.parent = self @@ -1174,6 +1175,20 @@ function multi:newProcessor(name, nothread, priority) Active = false c.process:Destroy() end + + c:newThread("Task Handler", function() + local self = multi:getCurrentProcess() + local function task_holder() + return #self.tasks > 0 + end + while true do + if #self.tasks > 0 then + table.remove(self.tasks,1)() + else + thread.hold(task_holder) + end + end + end).OnError(multi.error) table.insert(processes,c) self:create(c) @@ -1213,7 +1228,7 @@ function multi:getThreads() return threads end -function multi:getTasks() +function multi:getRunners() local tasks = {} for i,v in pairs(self.Mainloop) do if not v.__ignore then @@ -2503,6 +2518,7 @@ else end threadManager = multi:newProcessor("Global_Thread_Manager", nil, true).Start() +threadManager.tasks = multi.tasks -- The main multi interface is a bit different. function multi:getThreadManagerProcess() return threadManager @@ -2512,18 +2528,4 @@ function multi:getHandler() return threadManager:getHandler() end -local function task_holder() - return #tasks > 0 -end - -multi:newThread("Task Handler", function() - while true do - if #tasks > 0 then - table.remove(tasks)() - else - thread.hold(task_holder) - end - end -end).OnError(multi.error) - return multi \ No newline at end of file diff --git a/tests/test.lua b/tests/test.lua index a9ec6e8..c1b0192 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -98,6 +98,11 @@ multi, thread = require("multi"):init{print=true,warn=true,error=true,debugging= -- multi:newTLoop(func, 1) -- multi:mainloop() +for i = 1, 100 do + multi:newTask(function() + print("Task "..i) + end) +end local conn = multi:newConnection() conn(function() print("Test 1") end) @@ -113,6 +118,8 @@ conn:Fire() print(#conn) +multi:mainloop() + -- local conn1, conn2, conn3 = multi:newConnection(nil,nil,true), multi:newConnection(), multi:newConnection() -- local link = conn1(function()