From a7ba146a64ab275e32bad698b3defaa75e761cde Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Tue, 30 Nov 2021 20:11:20 -0500 Subject: [PATCH] Cleaning up code, fixing bugs (Almost ready for release) --- README.md | 3 +- changes.md | 6 +- multi/init.lua | 28 +++++++--- test.lua | 145 ++++++++++++++++++++++++------------------------- 4 files changed, 97 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index 32442e8..2cc8711 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ - thread.hold has been updated to allow all variants to work as well as some new features. Check the changelog or documentation for more info. - multi:newProccesor() Creates a process that acts like the multi namespace that can be managed independently from the mainloop. -Found an issue? Please [submit it](https://github.com/rayaman/multi/issues) and I'll look into it! +Found an issue? Please [submit it](https://github.com/rayaman/multi/issues) and someone will look into it! My multitasking library for lua. It is a pure lua binding, with exceptions of the integrations and the love2d compat. If you find any bugs or have any issues, please [let me know](https://github.com/rayaman/multi/issues) and I'll look into it!. @@ -33,6 +33,7 @@ Planned features/TODO Usage: [Check out the documentation for more info](https://github.com/rayaman/multi/blob/master/Documentation.md)
----- + ```lua package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path local multi, thread = require("multi"):init() diff --git a/changes.md b/changes.md index 75bf54c..223c647 100644 --- a/changes.md +++ b/changes.md @@ -364,12 +364,14 @@ Fixed: --- - Threaded functions not returning multiple values [Ref. Issue](https://github.com/rayaman/multi/issues/21) +- Priority Lists not containing Very_High and Very_Low from previous update +- All functions that should have chaining now do, reminder all functions that don't return any data return a reference to itself to allow chaining of method calls. ToDo --- -- N/A - +- Work on network parallelism (I really want to make this, but time and getting it right is proving much more difficult) +- Work on QOL changes to allow cleaner code like [this](#connection-can-now-be-added-together) # Update 15.0.0 - The art of faking it Full Update Showcase diff --git a/multi/init.lua b/multi/init.lua index ea366fa..3ee7a88 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -21,11 +21,13 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] + local multi = {} local mainloopActive = false local isRunning = false local clock = os.clock local thread = {} + if not _G["$multi"] then _G["$multi"] = {multi=multi,thread=thread} end @@ -61,16 +63,18 @@ multi.Priority_Idle = 65536 multi.PriorityResolve = { [1]="Core", - [4]="High", - [16]="Above Normal", - [64]="Normal", - [256]="Below Normal", - [1024]="Low", - [4096]="Idle", + [4]="Very High", + [16]="High", + [64]="Above Normal", + [256]="Normal", + [1024]="Below Normal", + [4096]="Low", + [16384]="Very Low" + [65536]="Idle", } multi.PStep = 1 -multi.PList = {multi.Priority_Core,multi.Priority_High,multi.Priority_Above_Normal,multi.Priority_Normal,multi.Priority_Below_Normal,multi.Priority_Low,multi.Priority_Idle} +multi.PList = {multi.Priority_Core,multi.Priority_Very_High,multi.Priority_High,multi.Priority_Above_Normal,multi.Priority_Normal,multi.Priority_Below_Normal,multi.Priority_Low,multi.Priority_Very_Low,multi.Priority_Idle} multi.PriorityTick=1 multi.Priority=multi.Priority_High multi.threshold=256 @@ -314,6 +318,7 @@ end function multi:create(ref) multi.OnObjectCreated:Fire(ref,self) + return self end function multi:setName(name) @@ -429,9 +434,11 @@ function multi:newConnection(protect,func,kill) end function c:Lock() c.lock = true + return self end function c:Unlock() c.lock = false + return self end function c:Fire(...) local ret={} @@ -1582,6 +1589,7 @@ function multi:newService(func) -- Priority managed threads th:kill() c.Stop() multi.setType(c,multi.DestroyedObj) + return c end function c:SetScheme(n) if type(self)=="number" then n = self end @@ -1630,6 +1638,7 @@ function multi:newService(func) -- Priority managed threads if type(self)=="number" then pri = self end p = pri c.SetScheme(scheme) + return c end multi.create(multi,c) return c @@ -2212,10 +2221,12 @@ function multi:setPriority(s) self.defPriority = self.Priority self.PrioritySet = true end + return self end function multi:ResetPriority() self.Priority = self.defPriority + return self end function os.getOS() @@ -2348,6 +2359,7 @@ function multi:reallocate(o,n) self.Parent=o table.insert(o.Mainloop,n,self) self.Active=true + return self end function multi.timer(func,...) @@ -2366,6 +2378,7 @@ end function multi:FreeMainEvent() self.func={} + return self end function multi:connectFinal(func) @@ -2379,6 +2392,7 @@ function multi:connectFinal(func) multi.print("Warning!!! "..self.Type.." doesn't contain a Final Connection State! Use "..self.Type..":Break(func) to trigger it's final event!") self:OnBreak(func) end + return self end if os.getOS()=="windows" then diff --git a/test.lua b/test.lua index 3c77ae8..a72096a 100644 --- a/test.lua +++ b/test.lua @@ -5,7 +5,7 @@ func = thread:newFunction(function(count) local a = 0 while true do a = a + 1 - thread.sleep(.1) + thread.sleep(.5) thread.pushStatus(a,count) if a == count then break end end @@ -32,87 +32,82 @@ end) --GLOBAL,THREAD = require("multi.integration.threading"):init() -- Auto detects your environment and uses what's available --- func = thread:newFunction(function() --- thread.sleep(3) --- print("Hello World!") --- return true --- end,true) -- set holdme to true +func2 = thread:newFunction(function() + thread.sleep(3) + print("Hello World!") + return true +end,true) -- set holdme to true --- func:holdMe(false) -- reset holdme to false --- print("Calling func...") --- print(func()) +func2:holdMe(false) -- reset holdme to false +print("Calling func...") +print(func2()) +test = thread:newFunction(function(a,b) + thread.sleep(1) + return a,b +end) +print(test(1,2).connect(function(...) + print(...) +end)) +test:Pause() +print(test(1,2).connect(function(...) + print(...) +end)) +test:Resume() +print(test(1,2).connect(function(...) + print(...) +end)) +test = thread:newFunction(function() + return 1,2,nil,3,4,5,6,7,8,9 +end,true) +print(test()) +multi:newThread("testing",function() + print("#Test = ",test()) + print(thread.hold(function() + print("Hello!") + return false + end,{ + interval = 2, + cycles = 3 + })) -- End result, 3 attempts within 6 seconds. If still false then timeout + print("held") +end).OnError(function(...) + print(...) +end) --- test = thread:newFunction(function(a,b) --- thread.sleep(1) --- return a,b --- end) --- print(test(1,2).connect(function(...) --- print(...) --- end)) --- test:Pause() --- print(test(1,2).connect(function(...) --- print(...) --- end)) --- test:Resume() --- print(test(1,2).connect(function(...) --- print(...) --- end)) +sandbox = multi:newProcessor("Test Processor") +sandbox:newTLoop(function() + print("testing...") +end,1) --- test = thread:newFunction(function() --- return 1,2,nil,3,4,5,6,7,8,9 --- end,true) --- print(test()) --- multi:newThread("testing",function() --- print("#Test = ",test()) --- print(thread.hold(function() --- print("Hello!") --- return false --- end,{ --- interval = 2, --- cycles = 3 --- })) -- End result, 3 attempts within 6 seconds. If still false then timeout --- print("held") --- end).OnError(function(...) --- print(...) --- end) +test2 = multi:newTLoop(function() + print("testing2...") +end,1) --- sandbox = multi:newProcessor() --- for i,v in pairs(sandbox.process) do --- print(i,v) --- end --- io.read() --- sandbox:newTLoop(function() --- print("testing...") --- end,1) +sandbox:newThread("Test Thread",function() + local a = 0 + while true do + thread.sleep(1) + a = a + 1 + print("Thread Test: ".. multi.getCurrentProcess().Name) + if a == 10 then + sandbox.Stop() + end + end +end).OnError(function(...) + print(...) +end) --- test2 = multi:newTLoop(function() --- print("testing2...") --- end,1) +multi:newThread("Test Thread",function() + while true do + thread.sleep(1) + print("Thread Test: ".. multi.getCurrentProcess().Name) + end +end).OnError(function(...) + print(...) +end) --- sandbox:newThread("Test Thread",function() --- local a = 0 --- while true do --- thread.sleep(1) --- a = a + 1 --- print("Thread Test: ".. multi.getCurrentProcess().Name) --- if a == 10 then --- sandbox.Stop() --- end --- end --- end).OnError(function(...) --- print(...) --- end) --- multi:newThread("Test Thread",function() --- while true do --- thread.sleep(1) --- print("Thread Test: ".. multi.getCurrentProcess().Name) --- end --- end).OnError(function(...) --- print(...) --- end) - --- sandbox.Start() +sandbox.Start() multi:mainloop() \ No newline at end of file