Reworking the loops

This commit is contained in:
Ryan Ward 2022-01-18 23:39:17 -05:00
parent d98f353936
commit 6c1e9f26f0
2 changed files with 226 additions and 170 deletions

View File

@ -1664,8 +1664,14 @@ function multi:mainloop(settings)
local delay = 3 local delay = 3
if settings then if settings then
priority = settings.priority priority = settings.priority
if settings.auto_priority then if priority == 1 then
priority = -1 self.uManager = self.uManagerRefP1
elseif self.priority == 2 then
self.uManager = self.uManagerRefP2
elseif self.priority == 3 then
self.uManager = self.uManagerRefP3
elseif auto_priority then
self.uManager = self.uManagerRefP0
end end
if settings.preLoop then if settings.preLoop then
settings.preLoop(self) settings.preLoop(self)
@ -1879,15 +1885,22 @@ function multi:mainloop(settings)
return "Already Running!" return "Already Running!"
end end
end end
function multi:uManager(settings) function multi:uManager(settings)
__CurrentProcess = self __CurrentProcess = self
multi.OnPreLoad:Fire() multi.OnPreLoad:Fire()
multi.defaultSettings = settings or multi.defaultSettings multi.defaultSettings = settings or multi.defaultSettings
self.t,self.tt = clock(),0 self.t,self.tt = clock(),0
if settings then if settings then
priority = settings.priority local priority = settings.priority
if settings.auto_priority then if priority == 1 then
priority = -1 self.uManager = self.uManagerRefP1
elseif self.priority == 2 then
self.uManager = self.uManagerRefP2
elseif self.priority == 3 then
self.uManager = self.uManagerRefP3
elseif auto_priority then
self.uManager = self.uManagerRefP0
end end
if settings.preLoop then if settings.preLoop then
settings.preLoop(self) settings.preLoop(self)
@ -1905,8 +1918,8 @@ function multi:uManager(settings)
end end
multi.OnLoad:Fire() multi.OnLoad:Fire()
self.uManager=self.uManagerRef self.uManager=self.uManagerRef
self.lManager=self.lManagerRef
end end
function multi:lManager(settings) function multi:lManager(settings)
__CurrentProcess = self __CurrentProcess = self
multi.OnPreLoad:Fire() multi.OnPreLoad:Fire()
@ -1935,6 +1948,7 @@ function multi:lManager(settings)
self.uManager=self.uManagerRef self.uManager=self.uManagerRef
self.lManager=self.lManagerRef self.lManager=self.lManagerRef
end end
function multi:lManagerRef() function multi:lManagerRef()
if self.Active then if self.Active then
local Loop=self.Mainloop local Loop=self.Mainloop
@ -1953,28 +1967,32 @@ function multi:lManagerRef()
end end
end end
end end
function multi:uManagerRef()
local ctask
local Loop
function multi:uManagerRefP1()
if self.Active then if self.Active then
local Loop=self.Mainloop __CurrentProcess = self
local PS=self Loop=self.Mainloop
if multi.defaultSettings.priority==1 then
for _D=#Loop,1,-1 do for _D=#Loop,1,-1 do
__CurrentTask = Loop[_D]
ctask = __CurrentTask
for P=1,7 do for P=1,7 do
if Loop[_D] then if ctask then
if (PS.PList[P])%Loop[_D].Priority==0 then if (PS.PList[P])%ctask.Priority==0 then
if Loop[_D].Active then if ctask.Active then
self.CID=_D
if not multi.defaultSettings.protect then if not multi.defaultSettings.protect then
Loop[_D]:Act() ctask:Act()
__CurrentProcess = self __CurrentProcess = self
else else
local status, err=pcall(Loop[_D].Act,Loop[_D]) local status, err=pcall(ctask.Act,ctask)
__CurrentProcess = self __CurrentProcess = self
if err then if err then
Loop[_D].error=err ctask.error=err
self.OnError:Fire(Loop[_D],err) self.OnError:Fire(ctask,err)
if multi.defaultSettings.stopOnError then if multi.defaultSettings.stopOnError then
Loop[_D]:Destroy() ctask:Destroy()
end end
end end
end end
@ -1983,23 +2001,31 @@ function multi:uManagerRef()
end end
end end
end end
elseif multi.defaultSettings.priority==2 then end
end
function multi:uManagerRefP2()
if self.Active then
__CurrentProcess = self
Loop=self.Mainloop
for _D=#Loop,1,-1 do for _D=#Loop,1,-1 do
if Loop[_D] then __CurrentTask = Loop[_D]
if (PS.PStep)%Loop[_D].Priority==0 then ctask = __CurrentTask
if Loop[_D].Active then if ctask then
if (PS.PStep)%ctask.Priority==0 then
if ctask.Active then
self.CID=_D self.CID=_D
if not multi.defaultSettings.protect then if not multi.defaultSettings.protect then
Loop[_D]:Act() ctask:Act()
__CurrentProcess = self __CurrentProcess = self
else else
local status, err=pcall(Loop[_D].Act,Loop[_D]) local status, err=pcall(ctask.Act,ctask)
__CurrentProcess = self __CurrentProcess = self
if err then if err then
Loop[_D].error=err ctask.error=err
self.OnError:Fire(Loop[_D],err) self.OnError:Fire(ctask,err)
if multi.defaultSettings.stopOnError then if multi.defaultSettings.stopOnError then
Loop[_D]:Destroy() ctask:Destroy()
end end
end end
end end
@ -2011,25 +2037,32 @@ function multi:uManagerRef()
if PS.PStep>self.Priority_Idle then if PS.PStep>self.Priority_Idle then
PS.PStep=0 PS.PStep=0
end end
elseif priority == 3 then end
end
function multi:uManagerRefP3()
if self.Active then
self.tt = clock()-self.t self.tt = clock()-self.t
self.t = clock() self.t = clock()
__CurrentProcess = self
Loop=self.Mainloop
for _D=#Loop,1,-1 do for _D=#Loop,1,-1 do
if Loop[_D] then __CurrentTask = Loop[_D]
if Loop[_D].Priority == self.Priority_Core or (Loop[_D].Priority == self.Priority_High and tt<.5) or (Loop[_D].Priority == self.Priority_Above_Normal and tt<.125) or (Loop[_D].Priority == self.Priority_Normal and tt<.063) or (Loop[_D].Priority == self.Priority_Below_Normal and tt<.016) or (Loop[_D].Priority == self.Priority_Low and tt<.003) or (Loop[_D].Priority == self.Priority_Idle and tt<.001) then ctask = __CurrentTask
if Loop[_D].Active then if ctask then
self.CID=_D if ctask.Priority == self.Priority_Core or (ctask.Priority == self.Priority_High and tt<.5) or (ctask.Priority == self.Priority_Above_Normal and tt<.125) or (ctask.Priority == self.Priority_Normal and tt<.063) or (ctask.Priority == self.Priority_Below_Normal and tt<.016) or (ctask.Priority == self.Priority_Low and tt<.003) or (ctask.Priority == self.Priority_Idle and tt<.001) then
if ctask.Active then
if not protect then if not protect then
Loop[_D]:Act() ctask:Act()
__CurrentProcess = self __CurrentProcess = self
else else
local status, err=pcall(Loop[_D].Act,Loop[_D]) local status, err=pcall(ctask.Act,ctask)
__CurrentProcess = self __CurrentProcess = self
if err then if err then
Loop[_D].error=err ctask.error=err
self.OnError:Fire(Loop[_D],err) self.OnError:Fire(ctask,err)
if multi.defaultSettings.stopOnError then if multi.defaultSettings.stopOnError then
Loop[_D]:Destroy() ctask:Destroy()
end end
end end
end end
@ -2037,53 +2070,59 @@ function multi:uManagerRef()
end end
end end
end end
elseif priority == -1 then end
end
function multi:uManagerRefP0()
if self.Active then
__CurrentProcess = self
Loop=self.Mainloop
for _D=#Loop,1,-1 do for _D=#Loop,1,-1 do
local sRef = Loop[_D] __CurrentTask = Loop[_D]
if Loop[_D] then ctask = __CurrentTask
if (sRef.Priority == self.Priority_Core) or PStep==0 then if ctask then
if sRef.Active then if (ctask.Priority == self.Priority_Core) or PStep==0 then
self.CID=_D if ctask.Active then
if not protect then if not protect then
if sRef.solid then if ctask.solid then
sRef:Act() ctask:Act()
__CurrentProcess = self __CurrentProcess = self
solid = true solid = true
else else
time = multi.timer(sRef.Act,sRef) time = multi.timer(ctask.Act,ctask)
sRef.solid = true ctask.solid = true
solid = false solid = false
end end
if Loop[_D] and not solid then if ctask and not solid then
if time == 0 then if time == 0 then
Loop[_D].Priority = self.Priority_Core ctask.Priority = self.Priority_Core
else else
Loop[_D].Priority = multi.defaultSettings.auto_lowerbound ctask.Priority = multi.defaultSettings.auto_lowerbound
end end
end end
else else
if Loop[_D].solid then if ctask.solid then
Loop[_D]:Act() ctask:Act()
__CurrentProcess = self __CurrentProcess = self
solid = true solid = true
else else
time, status, err=multi.timer(pcall,Loop[_D].Act,Loop[_D]) time, status, err=multi.timer(pcall,ctask.Act,ctask)
__CurrentProcess = self __CurrentProcess = self
Loop[_D].solid = true ctask.solid = true
solid = false solid = false
end end
if Loop[_D] and not solid then if ctask and not solid then
if time == 0 then if time == 0 then
Loop[_D].Priority = self.Priority_Core ctask.Priority = self.Priority_Core
else else
Loop[_D].Priority = multi.defaultSettings.auto_lowerbound ctask.Priority = multi.defaultSettings.auto_lowerbound
end end
end end
if err then if err then
Loop[_D].error=err ctask.error=err
self.OnError:Fire(Loop[_D],err) self.OnError:Fire(ctask,err)
if multi.defaultSettings.stopOnError then if multi.defaultSettings.stopOnError then
Loop[_D]:Destroy() ctask:Destroy()
end end
end end
end end
@ -2101,31 +2140,56 @@ function multi:uManagerRef()
end end
end end
end end
else end
end
function multi:uManagerRefLight()
if self.Active then
local Loop=self.Mainloop
local ctask
__CurrentProcess = self
for _D=#Loop,1,-1 do for _D=#Loop,1,-1 do
if Loop[_D] then __CurrentTask = Loop[_D]
if Loop[_D].Active then ctask = __CurrentTask
self.CID=_D if ctask.Active then
if not multi.defaultSettings.protect then if not protect then
Loop[_D]:Act() ctask:Act()
end
end
end
end
end
function multi:uManagerRef()
if self.Active then
__CurrentProcess = self __CurrentProcess = self
else Loop=self.Mainloop
local status, err=pcall(Loop[_D].Act,Loop[_D]) for _D=#Loop,1,-1 do
__CurrentTask = Loop[_D]
ctask = __CurrentTask
if ctask then
if ctask.Active then
if multi.defaultSettings.protect then
__CurrentProcess = self __CurrentProcess = self
local status, err = pcall(ctask.Act,ctask)
if err then if err then
Loop[_D].error=err ctask.error = err
self.OnError:Fire(Loop[_D],err) self.OnError:Fire(ctask,err)
end
end end
else
__CurrentProcess = self
ctask:Act()
end end
end end
end end
end end
end end
end end
-------- --------
-- UTILS -- UTILS
-------- --------
function table.merge(t1, t2) function table.merge(t1, t2)
for k,v in pairs(t2) do for k,v in pairs(t2) do
if type(v) == 'table' then if type(v) == 'table' then

View File

@ -29,18 +29,10 @@ local multi,thread = require("multi"):init()
-- os.exit() -- os.exit()
-- end) -- end)
multi:benchMark(1):OnBench(function(sec,steps)
local test = multi:newProcessor("test",true)
test:benchMark(1):OnBench(function(sec,steps)
print("Steps:",steps) print("Steps:",steps)
--os.exit() --os.exit()
end) end)
test:newThread(function() --multi:mainloop()
while true do
thread.sleep(1)
print("hi")
end
end)
test:lightloop()
-- multi:lightloop() multi:lightloop()