From 0e36ec917dc76797ed36edc74037a328e83648f9 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 2 Jun 2017 17:00:21 -0400 Subject: [PATCH] Added Threaded TLoops Modified the benchmark command Fixed bugs in: Timer s Conditions and Ranges --- multi/docs/features.txt | 1 + multi/init.lua | 19 ++++++++++++++---- multi/threading/tloop.lua | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 multi/threading/tloop.lua diff --git a/multi/docs/features.txt b/multi/docs/features.txt index fbf6000..0b6b8a0 100644 --- a/multi/docs/features.txt +++ b/multi/docs/features.txt @@ -33,6 +33,7 @@ Constructors [ACTORS] eventObj=multi:newEvent([function: TASK defualt: function() end]) alarmObj=multi:newAlarm([number: SET defualt: 0]) loopObj=multi:newLoop([function: FUNC]) +tloopObj=multi:newTLoop([function: FUNC], number: n) stepObj=multi:newStep([number: START defualt: 0],[number: RESET defualt: inf],[number: COUNT defualt: 1],[number: SKIP defualt: 0]) tstepObj=multi:newTStep([number: START defualt: 0],[number: RESET defualt: inf],[number: COUNT defualt: 1],[number: SET defualt: 1]) updaterObj=multi:newUpdater([number: SKIP defualt: 0]) diff --git a/multi/init.lua b/multi/init.lua index 6a07fb3..0d97c6b 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -311,9 +311,12 @@ function multi:fromfile(path,int) end return test2 end -function multi:benchMark(sec,p) +function multi:benchMark(sec,p,pt) local temp=self:newLoop(function(t,self) if self.clock()-self.init>self.sec then + if pt then + print(pt.." "..self.c.." Steps in "..sec.." second(s)!") + end self.tt(self.sec,self.c) self:Destroy() else @@ -682,17 +685,24 @@ function multi:newTimer() c.Type='timer' c.time=0 c.count=0 + c.paused=false function c:Start() self.time=os.clock() end function c:Get() + if self:isPaused() then return self.time end return (os.clock()-self.time)+self.count end + function c:isPaused() + return c.paused + end c.Reset=c.Start function c:Pause() self.time=self:Get() + self.paused=true end function c:Resume() + self.paused=false self.time=os.clock()-self.time end function c:tofile(path) @@ -746,10 +756,10 @@ function multi:newConnection(protect) end return ret end - function c:bind(t) + function c:Bind(t) self.func=t end - function c:remove() + function c:Remove() self.func={} end function c:connect(func,name) @@ -775,7 +785,7 @@ function multi:newConnection(protect) return self.func(...) end end, - remove=function(self) + Remove=function(self) for i=1,#self.Link do if self.Link[i][2]~=nil then if self.Link[i][2]==self.ID then @@ -794,6 +804,7 @@ function multi:newConnection(protect) end return temp end + c.Connect=c.connect function c:tofile(path) local m=bin.new() m:addBlock(self.Type) diff --git a/multi/threading/tloop.lua b/multi/threading/tloop.lua new file mode 100644 index 0000000..ed66c0a --- /dev/null +++ b/multi/threading/tloop.lua @@ -0,0 +1,42 @@ +require("multi.threading") +function multi:newThreadedTLoop(name,func,n) + local c=self:newTBase() + c.Type='tloopThread' + c.restN=n or 1 + if func then + c.func={func} + end + function c:tofile(path) + local m=bin.new() + m:addBlock(self.Type) + m:addBlock(self.func) + m:addBlock(self.Active) + m:tofile(path) + end + function c:Resume() + self.rest=false + end + function c:Pause() + self.rest=true + end + function c:OnLoop(func) + table.insert(self.func,func) + end + c.rest=false + c.updaterate=0 + c.restRate=.75 + multi:newThread(name,function(ref) + while true do + if c.rest then + thread.sleep(c.restRate) -- rest a bit more when a thread is paused + else + for i=1,#c.func do + c.func[i](c) + end + thread.sleep(c.restN) -- lets rest a bit + end + end + end) + self:create(c) + return c +end