Each processor has it's own thread handler, all processes now trigger the default thread handler.

This commit is contained in:
Ryan Ward 2022-02-08 22:40:38 -05:00
parent 03cea2d71a
commit c14a469069
3 changed files with 84 additions and 27 deletions

View File

@ -28,6 +28,7 @@ Added:
Changed:
---
- Moved `multi:newThread(...)` into the thread interface (`thread:newThread(...)`), code using `multi:newThread(...)` will still work. Also using `process:newThread(...)` binds the thread to the process, meaning if the process the thread is bound to is paused so is the thread.
- multi:mainloop(~~settings~~)/multi:uManager(~~settings~~) no longer takes a settings argument, that has been moved to multi:init(settings)
| Setting | Description |
---|---
@ -105,10 +106,11 @@ Fixed:
- Issue where gettasksdetails() would try to process a destroyed object causing it to crash
- Issue with multi.hold() not pumping the mainloop and only the scheduler
ToDo:
---
- Work on network parallelism
# Update 15.1.0 - Hold the thread!

View File

@ -27,6 +27,7 @@ local mainloopActive = false
local isRunning = false
local clock = os.clock
local thread = {}
local in_proc = false
if not _G["$multi"] then
_G["$multi"] = {multi=multi,thread=thread}
@ -926,31 +927,46 @@ function multi:newProcessor(name,nothread)
c.Type = "process"
c.Active = false or nothread
c.Name = name or ""
c.process = thread:newThread(function()
while true do
thread.hold(function() return c.Active end)
c.pump = false
c.threads = {}
c.startme = {}
local handler = c:createHandler(c.threads,c.startme)
c.process = multi:newLoop(function()
if c.Active then
c.pump = true
c:uManager()
handler()
c.pump = false
end
end)
c.process.isProcessThread = true
c.process.PID = sandcount
c.OnError = c.process.OnError
function c.run()
c:uManager()
function c:newThread(name,func,...)
in_proc = c
local t = thread.newThread(c,name,func,...)
in_proc = false
return t
end
function c:AttachThread(t)
--
function c.run()
c.pump = true
c:uManager()
handler()
c.pump = false
end
function c.Start()
if nothread then return self end
c.Active = true
return self
end
function c.Stop()
if nothread then return self end
c.Active = false
return self
end
function c:Destroy()
self.OnObjectDestroyed:Fire(c)
c.Active = false
c.process:Destroy()
end
return c
end
@ -1237,6 +1253,7 @@ function thread:newThread(name,func,...)
if type(name) == "function" then
name = "Thread#"..threadCount
end
local c={nil,nil,nil,nil,nil,nil,nil}
local env = {self=c}
c.TempRets = {nil,nil,nil,nil,nil,nil,nil,nil,nil,nil}
@ -1300,9 +1317,13 @@ function thread:newThread(name,func,...)
end
c.Destroy = c.Kill
if self.Type=="process" then
table.insert(self.threads,c)
table.insert(self.startme,c)
else
table.insert(threads,c)
table.insert(startme,c)
end
startme_len = #startme
globalThreads[c] = multi
threadid = threadid + 1
@ -1469,7 +1490,6 @@ local co_status = {
--self.setType(ref,self.DestroyedObj)
end,
}
local count = 0
local handler = coroutine.wrap(function(self)
while true do
for start = startme_len,1,-1 do
@ -1480,8 +1500,8 @@ local handler = coroutine.wrap(function(self)
yield()
end
for i=#threads,1,-1 do
if threads[i] then
ref = threads[i]
if ref then
task = ref.task
thd = ref.thread
ready = ref.__ready
@ -1492,6 +1512,28 @@ local handler = coroutine.wrap(function(self)
end
end)
function multi:createHandler(threads,startme)
return coroutine.wrap(function(self)
while true do
for start = #startme,1,-1 do
_,ret,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16 = resume(startme[start].thread,unpack(startme[start].startArgs))
co_status[status(startme[start].thread)](startme[start].thread,startme[start],t_none) -- Make sure there was no error
table.remove(startme)
yield()
end
for i=#threads,1,-1 do
ref = threads[i]
if ref then
task = ref.task
thd = ref.thread
ready = ref.__ready
co_status[status(thd)](thd,ref,task,i)
end
yield()
end
end
end)
end
function multi:newService(func) -- Priority managed threads
local c = {}

View File

@ -9,18 +9,31 @@ local conn = multi:newConnection()
local test = {}
local function bench(_,steps)
print("Steps/1s: "..steps)
os.exit()
--os.exit()
end
proc = multi:newProcessor("Test")
for i = 1,400 do
thread:newThread("Thread: "..i,function()
proc.Start()
thread:newThread(function()
thread.sleep(5)
proc.Stop()
thread.sleep(5)
proc.Start()
end)
thread:newThread(function()
while true do
thread.sleep(.1)
thread.sleep(1)
print("...")
end
end)
end
proc:benchMark(sleep_for,multi.Priority_Core,"Core:"):OnBench(bench)
proc:newThread(function()
while true do
proc.run()
thread.sleep(1)
print("Testing...")
end
end)
multi:mainloop()