From acc94ea17eef0502011b53435431e2620f80f8c1 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Tue, 4 Jul 2023 16:00:37 -0400 Subject: [PATCH] Removed system threaded connections, soon to be replaced by proxies --- init.lua | 7 +- integration/loveManager/extensions.lua | 3 +- integration/pseudoManager/extensions.lua | 3 +- integration/sharedExtensions/init.lua | 38 +++---- tests/threadtests.lua | 134 ++++++++++++++++------- 5 files changed, 121 insertions(+), 64 deletions(-) diff --git a/init.lua b/init.lua index 4135359..cb24772 100644 --- a/init.lua +++ b/init.lua @@ -2389,7 +2389,7 @@ end function multi.print(...) if multi.defaultSettings.print then local t = {} - for i,v in pairs(multi.pack(...)) do t[#t+1] = tostring(v) end + for i,v in ipairs(multi.pack(...)) do t[#t+1] = tostring(v) end io.write("\x1b[94mINFO:\x1b[0m " .. table.concat(t," ") .. "\n") end end @@ -2397,7 +2397,7 @@ end function multi.warn(...) if multi.defaultSettings.warn then local t = {} - for i,v in pairs(multi.pack(...)) do t[#t+1] = tostring(v) end + for i,v in ipairs(multi.pack(...)) do t[#t+1] = tostring(v) end io.write("\x1b[93mWARNING:\x1b[0m " .. table.concat(t," ") .. "\n") end end @@ -2414,7 +2414,7 @@ end function multi.success(...) local t = {} - for i,v in pairs(multi.pack(...)) do t[#t+1] = tostring(v) end + for i,v in ipairs(multi.pack(...)) do t[#t+1] = tostring(v) end io.write("\x1b[92mSUCCESS:\x1b[0m " .. table.concat(t," ") .. "\n") end @@ -2431,7 +2431,6 @@ multi.SetName = multi.setName local _os = os.exit function os.exit(n) - print("ERROR_"..n) multi.OnExit:Fire(n or 0) _os(n) end diff --git a/integration/loveManager/extensions.lua b/integration/loveManager/extensions.lua index 1dd7295..32a1215 100644 --- a/integration/loveManager/extensions.lua +++ b/integration/loveManager/extensions.lua @@ -380,4 +380,5 @@ function multi:newSystemThreadedConnection(name) self:create(c) return c -end \ No newline at end of file +end +require("multi.integration.sharedExtensions") \ No newline at end of file diff --git a/integration/pseudoManager/extensions.lua b/integration/pseudoManager/extensions.lua index 7ad8d6c..b6a4884 100644 --- a/integration/pseudoManager/extensions.lua +++ b/integration/pseudoManager/extensions.lua @@ -152,4 +152,5 @@ function multi:newSystemThreadedConnection(name) conn.init = function(self) return self end GLOBAL[name or "_"] = conn return conn -end \ No newline at end of file +end +require("multi.integration.sharedExtensions") \ No newline at end of file diff --git a/integration/sharedExtensions/init.lua b/integration/sharedExtensions/init.lua index c1bcfea..a4b16ef 100644 --- a/integration/sharedExtensions/init.lua +++ b/integration/sharedExtensions/init.lua @@ -22,26 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] -local function copy(obj) - if type(obj) ~= 'table' then return obj end - local res = {} - for k, v in pairs(obj) do res[copy(k)] = copy(v) end - return res -end - -function tprint (tbl, indent) - if not indent then indent = 0 end - for k, v in pairs(tbl) do - formatting = string.rep(" ", indent) .. k .. ": " - if type(v) == "table" then - print(formatting) - tprint(v, indent+1) - else - print(formatting .. tostring(v)) - end - end - end - local multi, thread = require("multi"):init() -- Returns a handler that allows a user to interact with an object on another thread! @@ -73,6 +53,12 @@ function multi:newProxy(list) local multi, thread = nil, nil function c:init() local multi, thread = nil, nil + local function copy(obj) + if type(obj) ~= 'table' then return obj end + local res = {} + for k, v in pairs(obj) do res[copy(k)] = copy(v) end + return res + end if not(c.is_init) then c.is_init = true local multi, thread = require("multi"):init() @@ -132,6 +118,12 @@ function multi:newProxy(list) end).OnError(multi.error) return self else + local function copy(obj) + if type(obj) ~= 'table' then return obj end + local res = {} + for k, v in pairs(obj) do res[copy(k)] = copy(v) end + return res + end local multi, thread = require("multi"):init() local me = self local funcs = copy(self.funcs) @@ -180,6 +172,12 @@ function multi:newProxy(list) function c:getTransferable() local cp = {} local multi, thread = require("multi"):init() + local function copy(obj) + if type(obj) ~= 'table' then return obj end + local res = {} + for k, v in pairs(obj) do res[copy(k)] = copy(v) end + return res + end cp.is_init = true cp.proxy_link = self.proxy_link cp.name = self.name diff --git a/tests/threadtests.lua b/tests/threadtests.lua index 8a96c27..daf75d7 100644 --- a/tests/threadtests.lua +++ b/tests/threadtests.lua @@ -131,53 +131,111 @@ multi:newThread("Scheduler Thread",function() multi.success("SystemThreadedJobQueues: Ok") - queue2 = multi:newSystemThreadedQueue("Test_Queue2"):init() - multi:newSystemThread("Test_Thread_2",function() - queue2 = THREAD.waitFor("Test_Queue2"):init() - connOut = THREAD.waitFor("ConnectionNAMEHERE"):init() - connOut(function(arg) - queue2:push("Test_Thread_2") - end) - multi:mainloop() - end).OnError(multi.error) - multi:newSystemThread("Test_Thread_3",function() - queue2 = THREAD.waitFor("Test_Queue2"):init() - connOut = THREAD.waitFor("ConnectionNAMEHERE"):init() - connOut(function(arg) - queue2:push("Test_Thread_3") - end) - multi:mainloop() - end).OnError(multi.error) - connOut = multi:newSystemThreadedConnection("ConnectionNAMEHERE"):init() - a=0 - connOut(function(arg) - queue2:push("Main") - end) - for i=1,3 do - thread.sleep(.1) - connOut:Fire("Test From Main Thread: "..i.."\n") - end - thread.sleep(2) - local count = 0 - multi:newThread(function() - while count < 9 do - if queue2:pop() then - count = count + 1 + -- queue2 = multi:newSystemThreadedQueue("Test_Queue2"):init() + -- multi:newSystemThread("Test_Thread_2",function() + -- queue2 = THREAD.waitFor("Test_Queue2"):init() + -- connOut = THREAD.waitFor("ConnectionNAMEHERE"):init() + -- connOut(function(arg) + -- queue2:push("Test_Thread_2") + -- end) + -- multi:mainloop() + -- end).OnError(multi.error) + -- multi:newSystemThread("Test_Thread_3",function() + -- queue2 = THREAD.waitFor("Test_Queue2"):init() + -- connOut = THREAD.waitFor("ConnectionNAMEHERE"):init() + -- connOut(function(arg) + -- queue2:push("Test_Thread_3") + -- end) + -- multi:mainloop() + -- end).OnError(multi.error) + -- connOut = multi:newSystemThreadedConnection("ConnectionNAMEHERE"):init() + -- a=0 + -- connOut(function(arg) + -- queue2:push("Main") + -- end) + -- for i=1,3 do + -- thread.sleep(.1) + -- connOut:Fire("Test From Main Thread: "..i.."\n") + -- end + -- thread.sleep(2) + -- local count = 0 + -- multi:newThread(function() + -- while count < 9 do + -- if queue2:pop() then + -- count = count + 1 + -- end + -- end + -- end).OnError(multi.error) + -- _, err = thread.hold(function() return count == 9 end,{sleep=.3}) + -- if err == multi.TIMEOUT then + -- multi.error("SystemThreadedConnections: Failed") + -- end + -- multi.success("SystemThreadedConnections: Ok") + + local stp = multi:newSystemThreadedProcessor(8) + + local tloop = stp:newTLoop(nil, 1) + + local proxy_test = false + + multi:newSystemThread("Testing proxy copy THREAD",function(tloop) + local multi, thread = require("multi"):init() + tloop = tloop:init() + multi.print("tloop type:",tloop.Type) + multi.print("Testing proxies on other threads") + thread:newThread(function() + while true do + thread.hold(tloop.OnLoop) + print(THREAD_NAME,"Loopy") end + end) + tloop.OnLoop(function(a) + print(THREAD_NAME, "Got loop...") + end) + multi:mainloop() + end, tloop:getTransferable()).OnError(multi.error) + + multi.print("tloop", tloop.Type) + multi.print("tloop.OnLoop", tloop.OnLoop.Type) + + thread:newThread(function() + multi.print("Testing holding on a proxy connection!") + thread.hold(tloop.OnLoop) + multi.print("Held on proxy connection... once") + thread.hold(tloop.OnLoop) + multi.print("Held on proxy connection... twice") + proxy_test = true + end).OnError(print) + + thread:newThread(function() + while true do + thread.hold(tloop.OnLoop) + print(THREAD_NAME,"Loopy") end - end).OnError(multi.error) - _, err = thread.hold(function() return count == 9 end,{sleep=.3}) - if err == multi.TIMEOUT then - multi.error("SystemThreadedConnections: Failed") + end) + + tloop.OnLoop(function() + print("OnLoop",THREAD_NAME) + end) + + t, val = thread.hold(function() + return count == 10 + end,{sleep=5}) + + if val == multi.TIMEOUT then + multi.error("SystemThreadedProcessor/Proxies: Failed") + os.exit(1) end - multi.success("SystemThreadedConnections: Ok") + + thread.sleep(2) + + multi.success("SystemThreadedProcessor: OK") we_good = true os.exit(1) end).OnError(multi.error) multi.OnExit(function(err_or_errorcode) - print("Final status!",err_or_errorcode) if not we_good then multi.info("There was an error running some tests!") return