added multi.OnLoad

This commit is contained in:
Ryan Ward 2020-01-31 14:33:13 -05:00
parent 0446dd0bea
commit e9e2096999
3 changed files with 26 additions and 15 deletions

View File

@ -3,6 +3,7 @@
Update 14.1.0 Bug Fixes and a change Update 14.1.0 Bug Fixes and a change
------------- -------------
# Added: # Added:
- multi.OnLoad(func) -- A special connection that allows you to connect to the an event that triggers when the multi engine starts! This is slightly different from multi.PreLoad(func) Which connects before any variables have been set up in the multi table, before any settings are cemented into the core. In most cases they will operate exactly the same. This is a feature that was created with module creators in mind. This way they can have code be loaded and managed before the main loop starts.
- multi.OnExit(func) -- A special connection that allows you to connect onto the lua state closing event. - multi.OnExit(func) -- A special connection that allows you to connect onto the lua state closing event.
```lua ```lua
package.path="?/init.lua;?.lua;"..package.path package.path="?/init.lua;?.lua;"..package.path
@ -65,14 +66,14 @@ multi:mainloop()
# Removed: # Removed:
- multi:newTimeStamper() -- schedulejob replaces timestamper - multi:newTimeStamper() -- schedulejob replaces timestamper
# Fixed: # Fixed:
- Modified the thread.* methods to perform better - Modified the thread.* methods to perform better (Tables were being created each time one of these methods were called! Which in turn slowed things down. One table is modified to get things working properly)
-- thread.sleep() - thread.sleep()
-- thread.hold() - thread.hold()
-- thread.holdFor() - thread.holdFor()
-- thread.holdWithin() - thread.holdWithin()
-- thread.skip() - thread.skip()
-- thread.kill() - thread.kill()
-- thread.yield() - thread.yield()
Update 14.0.0 Consistency, Additions and Stability Update 14.0.0 Consistency, Additions and Stability

View File

@ -776,7 +776,7 @@ end
local CRef = { local CRef = {
Fire = function() end Fire = function() end
} }
function multi:newConnection(protect,func) function multi:newConnection(protect,func,kill)
local c={} local c={}
c.callback = func c.callback = func
c.Parent=self c.Parent=self
@ -850,6 +850,9 @@ function multi:newConnection(protect,func)
if not self.func[i] then return end if not self.func[i] then return end
table.insert(ret,{self.func[i][1](...)}) table.insert(ret,{self.func[i][1](...)})
end end
if kill then
table.remove(self.func,i)
end
end end
return ret return ret
end end
@ -981,7 +984,6 @@ function multi.nextStep(func)
next[#next+1] = func next[#next+1] = func
end end
end end
multi.OnPreLoad=multi:newConnection()
--Core Actors --Core Actors
function multi:newEvent(task) function multi:newEvent(task)
local c=self:newBase() local c=self:newBase()
@ -1340,7 +1342,9 @@ function os.exit(n)
multi.OnExit:Fire(n or 0) multi.OnExit:Fire(n or 0)
_os(n) _os(n)
end end
multi.OnExit = multi:newConnection() multi.OnPreLoad = multi:newConnection()
multi.OnLoad = multi:newConnection(nil,nil,true)
multi.OnExit = multi:newConnection(nil,nil,true)
multi.m = {onexit = function() multi.OnExit:Fire() end} multi.m = {onexit = function() multi.OnExit:Fire() end}
if _VERSION >= "Lua 5.2" then if _VERSION >= "Lua 5.2" then
setmetatable(multi.m, {__gc = multi.m.onexit}) setmetatable(multi.m, {__gc = multi.m.onexit})
@ -1549,6 +1553,7 @@ thread.__threads = {}
local threads = thread.__threads local threads = thread.__threads
local Gref = _G local Gref = _G
function multi:newThread(name,func,...) function multi:newThread(name,func,...)
multi.OnLoad:Fire()
local func = func or name local func = func or name
if type(name) == "function" then if type(name) == "function" then
name = "Thread#"..threadCount name = "Thread#"..threadCount
@ -1971,9 +1976,9 @@ function multi:newHyperThreadedProcess(name)
end end
-- Multi runners -- Multi runners
function multi:mainloop(settings) function multi:mainloop(settings)
multi.OnPreLoad:Fire()
multi.defaultSettings = settings or multi.defaultSettings multi.defaultSettings = settings or multi.defaultSettings
self.uManager=self.uManagerRef self.uManager=self.uManagerRef
multi.OnPreLoad:Fire()
local p_c,p_h,p_an,p_n,p_bn,p_l,p_i = self.Priority_Core,self.Priority_High,self.Priority_Above_Normal,self.Priority_Normal,self.Priority_Below_Normal,self.Priority_Low,self.Priority_Idle local p_c,p_h,p_an,p_n,p_bn,p_l,p_i = self.Priority_Core,self.Priority_High,self.Priority_Above_Normal,self.Priority_Normal,self.Priority_Below_Normal,self.Priority_Low,self.Priority_Idle
local P_LB = p_i local P_LB = p_i
if not isRunning then if not isRunning then
@ -2014,6 +2019,7 @@ function multi:mainloop(settings)
local autoP = 0 local autoP = 0
local solid,sRef local solid,sRef
local cc=0 local cc=0
multi.OnLoad:Fire()
while mainloopActive do while mainloopActive do
if next then if next then
local DD = table.remove(next,1) local DD = table.remove(next,1)
@ -2210,6 +2216,7 @@ function multi:uManager(settings)
multi.defaultSettings.auto_lowerbound = settings.auto_lowerbound or self.Priority_Idle multi.defaultSettings.auto_lowerbound = settings.auto_lowerbound or self.Priority_Idle
protect = settings.protect protect = settings.protect
end end
multi.OnLoad:Fire()
self.uManager=self.uManagerRef self.uManager=self.uManagerRef
end end
function multi:uManagerRef(settings) function multi:uManagerRef(settings)

View File

@ -1,5 +1,11 @@
package.path="?/init.lua;?.lua;"..package.path package.path="?/init.lua;?.lua;"..package.path
multi,thread = require("multi"):init() multi,thread = require("multi"):init()
multi.OnLoad(function()
print("Code Loaded!")
end)
multi.OnExit(function(n)
print("Code Exited")
end)
test = thread:newFunction(function() test = thread:newFunction(function()
thread.sleep(1) thread.sleep(1)
return 1,2 return 1,2
@ -27,7 +33,4 @@ multi:newThread(function()
end) end)
-- This waits for the returns since we are demanding them -- This waits for the returns since we are demanding them
end) end)
multi.OnExit(function(n)
print("Code Exited")
end)
multi:mainloop() multi:mainloop()