Fixed newTask()

This commit is contained in:
Ryan Ward 2023-11-02 09:31:09 -04:00
parent 9ad2c45b8f
commit 0af807a930
3 changed files with 28 additions and 17 deletions

View File

@ -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`

View File

@ -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
@ -1175,6 +1176,20 @@ function multi:newProcessor(name, nothread, priority)
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)
return 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

View File

@ -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()