Working on taskmanager features

This commit is contained in:
Ryan Ward 2022-02-20 21:07:04 -05:00
parent 5172dcdf01
commit 4240737e00
3 changed files with 118 additions and 13 deletions

View File

@ -13,21 +13,42 @@ Full Update Showcase
Added: Added:
--- ---
- multi:getThreads() - `multi:lock()`
- Returns a list of all threads on a process - Locks a multi object which prevents, Destroy(), Pause(), and Resume() being processed.
- multi:newProcessor(name,nothread).run()
- new function run to the processor object to
- multi:newProcessor(name,nothread):newFunction(func,holdme) - `multi:unlock()`
- Undoes the lock
- `multi:getProcessors()`
- Returns a list of all processors
- `multi:getName()`
- Returns the name of a processor
- `multi:getFullName()`
- Returns the fullname/entire process tree of a process
- Processors can be attached to processors
- `multi:getTasks()`
- Returns a list of all non thread based objects (loops, alarms, steps, etc)
- `multi:getThreads()`
- Returns a list of all threads on a process
- `multi:newProcessor(name,nothread).run()`
- New function run to the processor object to
- `multi:newProcessor(name,nothread):newFunction(func,holdme)`
- Acts like thread:newFunction(), but binds the execution of that threaded function to the processor - Acts like thread:newFunction(), but binds the execution of that threaded function to the processor
- multi:newTLoop() member functions - `multi:newTLoop()` member functions
- `TLoop:Set(set)` - Sets the time to wait for the TLoop - `TLoop:Set(set)` - Sets the time to wait for the TLoop
- multi:newStep() member functions - `multi:newStep()` member functions
- `Step:Count(count)` - Sets the amount a step should count by - `Step:Count(count)` - Sets the amount a step should count by
- multi:newTStep() member functions - `multi:newTStep()` member functions
- `TStep:Set(set)` - Sets the time to wait for the TStep - `TStep:Set(set)` - Sets the time to wait for the TStep
@ -77,6 +98,7 @@ Changed:
Status: Done 1 2 3 nil nil nil nil nil nil nil nil nil nil nil nil Status: Done 1 2 3 nil nil nil nil nil nil nil nil nil nil nil nil
Function Done! Function Done!
``` ```
- Modified how threads are handled internally. This changes makes it so threads "regardless of amount" should not impact performance. What you do in the threads might. This change was made by internally only processing one thread per step per processor. If you have 10 processors that are all active expect one step to process 10 threads. However if one processor has 10 threads each step will only process one thread. Simply put each addition of a thread shouldn't impact performance as it did before. - Modified how threads are handled internally. This changes makes it so threads "regardless of amount" should not impact performance. What you do in the threads might. This change was made by internally only processing one thread per step per processor. If you have 10 processors that are all active expect one step to process 10 threads. However if one processor has 10 threads each step will only process one thread. Simply put each addition of a thread shouldn't impact performance as it did before.
- 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. - 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.
@ -152,9 +174,13 @@ Removed:
Fixed: Fixed:
--- ---
- [Issue](https://github.com/rayaman/multi/issues/30) with Lanes crashing the lua state. Issue seems to be related to my filesystem
- [Issue](https://github.com/rayaman/multi/issues/30) with Lanes crashing the lua state. Issue seemed to be related to my filesystem, since remounting the drive caused the issue to stop. (Windows)
- [Issue](https://github.com/rayaman/multi/issues/29) where System threaded functions not being up to date with threaded functions - [Issue](https://github.com/rayaman/multi/issues/29) where System threaded functions not being up to date with threaded functions
- Issue where gettasksdetails() would try to process a destroyed object causing it to crash - 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 - Issue with multi.hold() not pumping the mainloop and only the scheduler
ToDo: ToDo:

View File

@ -28,13 +28,14 @@ local isRunning = false
local clock = os.clock local clock = os.clock
local thread = {} local thread = {}
local in_proc = false local in_proc = false
local processes = {}
if not _G["$multi"] then if not _G["$multi"] then
_G["$multi"] = {multi=multi,thread=thread} _G["$multi"] = {multi=multi,thread=thread}
end end
multi.Version = "15.2.0" multi.Version = "15.2.0"
multi.Name = "multi.root" multi.Name = "root"
multi.NIL = {Type="NIL"} multi.NIL = {Type="NIL"}
local NIL = multi.NIL local NIL = multi.NIL
multi.Mainloop = {} multi.Mainloop = {}
@ -85,6 +86,10 @@ local priorityTable = {[false]="Disabled",[true]="Enabled"}
local ProcessName = {"SubProcessor","MainProcessor"} local ProcessName = {"SubProcessor","MainProcessor"}
local globalThreads = {} local globalThreads = {}
function multi:getProcessors()
return processes
end
function multi:getTasksDetails(t) function multi:getTasksDetails(t)
if not(t) then if not(t) then
str = { str = {
@ -441,6 +446,14 @@ function multi:getType()
return self.Type return self.Type
end end
function multi:lock()
self.__locked = true
end
function multi:unlock()
self.__locked = false
end
-- Advance Timer stuff -- Advance Timer stuff
function multi:SetTime(n) function multi:SetTime(n)
if not n then n=3 end if not n then n=3 end
@ -473,6 +486,7 @@ end
-- Timer stuff done -- Timer stuff done
multi.PausedObjects = {} multi.PausedObjects = {}
function multi:Pause() function multi:Pause()
if self.__locked then multi.print("Cannot perform action on a locked object!") return end
if self.Type=='rootprocess' then if self.Type=='rootprocess' then
multi.print("You cannot pause the main process. Doing so will stop all methods and freeze your program! However if you still want to use multi:_Pause()") multi.print("You cannot pause the main process. Doing so will stop all methods and freeze your program! However if you still want to use multi:_Pause()")
else else
@ -490,6 +504,7 @@ function multi:Pause()
end end
function multi:Resume() function multi:Resume()
if self.__locked then multi.print("Cannot perform action on a locked object!") return end
if self.Type=='process' or self.Type=='rootprocess' then if self.Type=='process' or self.Type=='rootprocess' then
self.Active=true self.Active=true
local c=self:getChildren() local c=self:getChildren()
@ -507,6 +522,7 @@ function multi:Resume()
end end
function multi:Destroy() function multi:Destroy()
if self.__locked then multi.print("Cannot perform action on a locked object!") return end
if self.Type=='process' or self.Type=='rootprocess' then if self.Type=='process' or self.Type=='rootprocess' then
local c=self:getChildren() local c=self:getChildren()
for i=1,#c do for i=1,#c do
@ -576,6 +592,7 @@ function multi:newBase(ins)
c.Act=function() end c.Act=function() end
c.Parent=self c.Parent=self
c.creationTime = os.clock() c.creationTime = os.clock()
c.__locked = false
if ins then if ins then
table.insert(self.Mainloop,ins,c) table.insert(self.Mainloop,ins,c)
else else
@ -690,6 +707,7 @@ function multi:newAlarm(set)
end end
end end
function c:Resume() function c:Resume()
if self.__locked then multi.print("Cannot perform action on a locked object!") return end
self.Parent.Resume(self) self.Parent.Resume(self)
t = count + t t = count + t
return self return self
@ -702,6 +720,7 @@ function multi:newAlarm(set)
end end
c.OnRing = self:newConnection() c.OnRing = self:newConnection()
function c:Pause() function c:Pause()
if self.__locked then multi.print("Cannot perform action on a locked object!") return end
count = clock() count = clock()
self.Parent.Pause(self) self.Parent.Pause(self)
return self return self
@ -812,11 +831,13 @@ function multi:newTLoop(func,set)
self.set = set self.set = set
end end
function c:Resume() function c:Resume()
if self.__locked then multi.print("Cannot perform action on a locked object!") return end
self.Parent.Resume(self) self.Parent.Resume(self)
self.timer:Resume() self.timer:Resume()
return self return self
end end
function c:Pause() function c:Pause()
if self.__locked then multi.print("Cannot perform action on a locked object!") return end
self.timer:Pause() self.timer:Pause()
self.Parent.Pause(self) self.Parent.Pause(self)
return self return self
@ -920,7 +941,16 @@ function multi.getCurrentTask()
return __CurrentTask return __CurrentTask
end end
function multi:getName()
return self.Name
end
function multi:getFullName()
return self.Name
end
local sandcount = 1 local sandcount = 1
function multi:newProcessor(name,nothread) function multi:newProcessor(name,nothread)
local c = {} local c = {}
setmetatable(c,{__index = multi}) setmetatable(c,{__index = multi})
@ -933,30 +963,45 @@ function multi:newProcessor(name,nothread)
c.pump = false c.pump = false
c.threads = {} c.threads = {}
c.startme = {} c.startme = {}
c.parent = self
local handler = c:createHandler(c.threads,c.startme) local handler = c:createHandler(c.threads,c.startme)
c.process = multi:newLoop(function()
c.process = self:newLoop(function()
if Active then if Active then
c:uManager() c:uManager()
handler() handler()
end end
end) end)
c.process.isProcessThread = true c.process.isProcessThread = true
c.process.PID = sandcount c.process.PID = sandcount
c.OnError = c.process.OnError c.OnError = c.process.OnError
function c:getThreads() function c:getThreads()
return self.threads return self.threads
end end
function c:getFullName()
return self.parent:getFullName() .. "." .. self.Name
end
function c:getName()
return self.Name
end
function c:newThread(name,func,...) function c:newThread(name,func,...)
in_proc = c in_proc = c
local t = thread.newThread(c,name,func,...) local t = thread.newThread(c,name,func,...)
in_proc = false in_proc = false
return t return t
end end
function c:newFunction(func,holdme) function c:newFunction(func,holdme)
return thread:newFunctionBase(function(...) return thread:newFunctionBase(function(...)
return c:newThread("TempThread",func,...) return c:newThread("TempThread",func,...)
end,holdme)() end,holdme)()
end end
function c.run() function c.run()
if not Active then return end if not Active then return end
c.pump = true c.pump = true
@ -965,21 +1010,27 @@ function multi:newProcessor(name,nothread)
c.pump = false c.pump = false
return c return c
end end
function c.isActive() function c.isActive()
return Active return Active
end end
function c.Start() function c.Start()
Active = true Active = true
return c return c
end end
function c.Stop() function c.Stop()
Active = false Active = false
return c return c
end end
function c:Destroy() function c:Destroy()
Active = false Active = false
c.process:Destroy() c.process:Destroy()
end end
table.insert(processes,c)
return c return c
end end
@ -1034,6 +1085,10 @@ function multi:getThreads()
return threads return threads
end end
function multi:getTasks()
return self.Mainloop
end
function thread.request(t,cmd,...) function thread.request(t,cmd,...)
thread.requests[t.thread] = {cmd,{...}} thread.requests[t.thread] = {cmd,{...}}
end end

View File

@ -1,8 +1,32 @@
package.path = "./?/init.lua;"..package.path package.path = "./?/init.lua;"..package.path
multi, thread = require("multi"):init() multi, thread = require("multi"):init()
local proc = multi:newProcessor("Test")
local proc2 = multi:newProcessor("Test2")
local proc3 = proc2:newProcessor("Test3")
function multi:getTaskStats() function multi:getTaskStats()
local stats = {} local stats = {
[multi.Name] = {
threads = multi:getThreads(),
tasks = multi:getTasks()
}
}
local procs = multi:getProcessors()
for i = 1, #procs do
local proc = procs[i]
stats[proc:getFullName()] = {
threads = proc:getThreads(),
tasks = proc:getTasks()
}
end
return stats
end end
multi:mainloop() local tasks = multi:getTaskStats()
for i,v in pairs(tasks) do
print("Process: "..i)
end
--multi:mainloop()