From 91561f7f249c2d17e3a16f00b9275ccd673128bf Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Sun, 26 Nov 2023 12:49:23 -0500 Subject: [PATCH] Fixed tests on lanes and pseudo threading, todo fix love2d threading --- docs/changes.md | 2 +- init.lua | 40 +++++++++++++++++++++------ integration/pseudoManager/init.lua | 1 - integration/sharedExtensions/init.lua | 8 ++---- tests/main.lua | 4 +-- tests/runtests.lua | 5 +++- tests/test.lua | 34 ++++++++++++++++++++++- tests/threadtests.lua | 10 +++---- 8 files changed, 78 insertions(+), 26 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 294a38c..0206192 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -85,7 +85,7 @@ Allows the user to have multi auto set priorities (Requires chronos). Also adds Added --- - thread.defer(func) -- When using a co-routine thread or co-routine threaded function, defer will call it's function at the end of the the threads life through normal execution or an error. In the case of a function, when the function returns or errors. -- multi:setTaskDelay(delay), Tasks which are now tied to a processor can have an optional delay between the execution between each task. Useful perhaps for rate limiting. Without a delay all grouped tasks will be handled in one step. +- multi:setTaskDelay(delay), Tasks which are now tied to a processor can have an optional delay between the execution between each task. Useful perhaps for rate limiting. Without a delay all grouped tasks will be handled in one step. `delay` can be a function as well and will be processed as if thread.hold was called. - processor's now have a boost function which causes it to run its processes the number of times specified in the `boost(count)` function - thread.hold will now use a custom hold method for objects with a `Hold` method. This is called like `obj:Hold(opt)`. The only argument passed is the optional options table that thread.hold can pass. There is an exception for connection objects. While they do contain a Hold method, the Hold method isn't used and is there for proxy objects, though they can be used in non proxy/thread situations. Hold returns all the arguments that the connection object was fired with. - shared_table = STP:newSharedTable(tbl_name) -- Allows you to create a shared table that all system threads in a process have access to. Returns a reference to that table for use on the main thread. Sets `_G[tbl_name]` on the system threads so you can access it there. diff --git a/init.lua b/init.lua index f758de4..4959e2a 100644 --- a/init.lua +++ b/init.lua @@ -189,6 +189,7 @@ local optimization_stats = {} local ignoreconn = true local empty_func = function() end function multi:newConnection(protect,func,kill) + local processor = self local c={} local lock = false local fast = {} @@ -380,7 +381,7 @@ function multi:newConnection(protect,func,kill) end if kill then table.insert(kills,i) - multi:newTask(function() + processor:newTask(function() for _, k in pairs(kills) do table.remove(kills, _) table.remove(fast, k) @@ -418,7 +419,7 @@ function multi:newConnection(protect,func,kill) fast[i](...) if kill then table.insert(kills,i) - multi:newTask(function() + processor:newTask(function() for _, k in pairs(kills) do table.remove(kills, _) table.remove(fast, k) @@ -1147,14 +1148,29 @@ function multi:newProcessor(name, nothread, priority) function c:boost(count) boost = count or 1 + if boost > 1 then + self.run = function() + if not Active then return end + for i=1,boost do + c:uManager(true) + handler() + end + return c + end + else + self.run = function() + if not Active then return end + c:uManager(true) + handler() + return c + end + end end function c.run() if not Active then return end - for i=1,boost do - c:uManager(true) - handler() - end + c:uManager(true) + handler() return c end @@ -1178,7 +1194,11 @@ function multi:newProcessor(name, nothread, priority) end function c:setTaskDelay(delay) - task_delay = tonumber(delay) or 0 + if type(delay) == "function" then + task_delay = delay + else + task_delay = tonumber(delay) or 0 + end end c:newThread("Task Handler", function() @@ -1193,7 +1213,7 @@ function multi:newProcessor(name, nothread, priority) thread.hold(task_holder) end if task_delay~=0 then - thread.sleep(task_delay) + thread.hold(task_delay) end end end).OnError(multi.error) @@ -2481,12 +2501,14 @@ function multi.success(...) io.write("\x1b[92mSUCCESS:\x1b[0m " .. table.concat(t," ") .. "\n") end +-- Old things for compatability multi.GetType = multi.getType multi.IsPaused = multi.isPaused multi.IsActive = multi.isActive -multi.Reallocate = multi.Reallocate +multi.Reallocate = multi.reallocate multi.ConnectFinal = multi.connectFinal multi.ResetTime = multi.SetTime +multi.setTime = multi.SetTime multi.IsDone = multi.isDone multi.SetName = multi.setName diff --git a/integration/pseudoManager/init.lua b/integration/pseudoManager/init.lua index e1b7b03..31106e9 100644 --- a/integration/pseudoManager/init.lua +++ b/integration/pseudoManager/init.lua @@ -66,7 +66,6 @@ function multi:newSystemThread(name, func, ...) THREAD = THREAD, THREAD_NAME = tostring(name), __THREADNAME__ = tostring(name), - test = "testing", THREAD_ID = id, thread = thread, multi = multi, diff --git a/integration/sharedExtensions/init.lua b/integration/sharedExtensions/init.lua index a048b5f..8526954 100644 --- a/integration/sharedExtensions/init.lua +++ b/integration/sharedExtensions/init.lua @@ -60,6 +60,7 @@ function multi:newProxy(list) return res end if not(self.is_init) then + THREAD.sleep(.3) self.is_init = true local multi, thread = require("multi"):init() self.proxy_link = "PL" .. multi.randomString(12) @@ -92,8 +93,6 @@ function multi:newProxy(list) local sref = table.remove(data, 1) local ret - print(_G[list[0]], func) - if sref then ret = {_G[list[0]][func](_G[list[0]], multi.unpack(data))} else @@ -120,6 +119,7 @@ function multi:newProxy(list) end) return self else + THREAD.sleep(.3) local function copy(obj) if type(obj) ~= 'table' then return obj end local res = {} @@ -145,7 +145,6 @@ function multi:newProxy(list) setmetatable(v[2],getmetatable(multi:newConnection())) else self[v] = thread:newFunction(function(self,...) - multi.print("Pushing: " .. v) if self == me then me.send:push({v, true, ...}) else @@ -192,9 +191,6 @@ function multi:newProxy(list) THREAD = multi.integration.THREAD end local proxy = THREAD.waitFor(self.proxy_link) - for i,v in pairs(proxy) do - print("proxy",i,v) - end proxy.funcs = self.funcs return proxy:init() end diff --git a/tests/main.lua b/tests/main.lua index a21d620..b23a884 100644 --- a/tests/main.lua +++ b/tests/main.lua @@ -1,6 +1,6 @@ package.path = "../?/init.lua;../?.lua;"..package.path --- require("runtests") --- require("threadtests") +require("runtests") +require("threadtests") -- Allows you to run "love tests" which runs the tests multi, thread = require("multi"):init() diff --git a/tests/runtests.lua b/tests/runtests.lua index 65a2f67..c62f04b 100644 --- a/tests/runtests.lua +++ b/tests/runtests.lua @@ -175,7 +175,7 @@ runTest = thread:newFunction(function() end if not love then local ec = 0 - if _VERSION == "5.1" then + if _VERSION == "Lua 5.1" then multi.print("Testing pseudo threading") _, str, ecc = os.execute("lua tests/threadtests.lua p") ec = ec + ecc @@ -185,14 +185,17 @@ runTest = thread:newFunction(function() if ec ~= 0 then os.exit(1) end + os.exit(0) else multi.print("Testing pseudo threading") ec = ec + os.execute("lua tests/threadtests.lua p") multi.print("Testing lanes threading") ec = ec + os.execute("lua tests/threadtests.lua l") + multi:Stop() if ec ~= 0 then os.exit(1) end + os.exit(0) end end end) diff --git a/tests/test.lua b/tests/test.lua index d9cd152..8ca374e 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -1,5 +1,8 @@ package.path = "../?/init.lua;../?.lua;"..package.path multi, thread = require("multi"):init{print=true,warn=true,debugging=true} +for i,v in pairs(thread) do + print(i,v) +end -- require("multi.integration.priorityManager") -- multi.debugging.OnObjectCreated(function(obj, process) @@ -266,4 +269,33 @@ multi:mainloop() -- multi:mainloop() -- end) --- multi:mainloop() \ No newline at end of file +-- multi:mainloop() +--[[ + newFunction function: 0x00fad170 + waitFor function: 0x00fad0c8 + request function: 0x00fa4f10 + newThread function: 0x00fad1b8 + --__threads table: 0x00fa4dc8 + defer function: 0x00fa4f98 + isThread function: 0x00facd40 + holdFor function: 0x00fa5058 + yield function: 0x00faccf8 + hold function: 0x00fa51a0 + chain function: 0x00fa5180 + __CORES 32 + newISOThread function: 0x00fad250 + newFunctionBase function: 0x00fad128 + requests table: 0x00fa4e68 + newProcessor function: 0x00fad190 + exec function: 0x00fa50e8 + pushStatus function: 0x00fad108 + kill function: 0x00faccd8 + get function: 0x00fad0a8 + set function: 0x00fad088 + getCores function: 0x00facd60 + skip function: 0x00faccb0 + --_Requests function: 0x00fa50a0 + getRunningThread function: 0x00fa4fb8 + holdWithin function: 0x00facc80 + sleep function: 0x00fa4df0 +]] \ No newline at end of file diff --git a/tests/threadtests.lua b/tests/threadtests.lua index 0b91544..6a21cbc 100644 --- a/tests/threadtests.lua +++ b/tests/threadtests.lua @@ -146,7 +146,7 @@ multi:newThread("Scheduler Thread",function() local stp = multi:newSystemThreadedProcessor(5) local tloop = stp:newTLoop(function() - print("Test") + --print("Test") end, 1) multi:newSystemThread("Testing proxy copy THREAD",function(tloop) @@ -157,11 +157,11 @@ multi:newThread("Scheduler Thread",function() thread:newThread(function() while true do thread.hold(tloop.OnLoop) - print(THREAD_NAME,"Loopy") + --print(THREAD_NAME,"Loopy") end end) tloop.OnLoop(function(a) - print(THREAD_NAME, "Got loop...") + --print(THREAD_NAME, "Got loop...") end) multi:mainloop() end, tloop:getTransferable()) @@ -183,12 +183,12 @@ multi:newThread("Scheduler Thread",function() thread:newThread(function() while true do thread.hold(tloop.OnLoop) - print(THREAD_NAME,"Local Loopy") + --print(THREAD_NAME,"Local Loopy") end end) tloop.OnLoop(function() - print("OnLoop",THREAD_NAME) + --print("OnLoop",THREAD_NAME) end) t, val = thread.hold(function()