From d30ee3788ecc53a97d8893caf58960a512541533 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Mon, 11 Apr 2022 23:48:45 -0400 Subject: [PATCH] THREAD.pushStatus for lanes works, todo love --- multi/integration/lanesManager/init.lua | 17 +++++--- multi/integration/lanesManager/threads.lua | 10 +++-- test.lua | 49 +++++++++++++++++----- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/multi/integration/lanesManager/init.lua b/multi/integration/lanesManager/init.lua index 80ccbd8..4b8ac89 100644 --- a/multi/integration/lanesManager/init.lua +++ b/multi/integration/lanesManager/init.lua @@ -48,7 +48,9 @@ end local __GlobalLinda = lanes.linda() -- handles global stuff local __SleepingLinda = lanes.linda() -- handles sleeping stuff local __ConsoleLinda = lanes.linda() -- handles console stuff -local GLOBAL,THREAD = require("multi.integration.lanesManager.threads").init(__GlobalLinda,__SleepingLinda) +local __StatusLinda = lanes.linda() -- handles pushstatus for stfunctions + +local GLOBAL,THREAD = require("multi.integration.lanesManager.threads").init(__GlobalLinda, __SleepingLinda, __StatusLinda) local count = 1 local started = false local livingThreads = {} @@ -110,12 +112,17 @@ function multi.InitSystemThreadErrorHandler() started = true thread:newThread("SystemThreadScheduler",function() local threads = multi.SystemThreads + local _,data,status,push,temp while true do - thread.sleep(.005) -- switching states often takes a huge hit on performance. half a second to tell me there is an error is good enough. - local _,data = __ConsoleLinda:receive(0, "Q") + thread.yield() + _,data = __ConsoleLinda:receive(0, "Q") for i = #threads, 1, -1 do - local status = threads[i].thread.status - local temp = threads[i] + temp = threads[i] + status = temp.thread.status + push = __StatusLinda:get(temp.Id) + if push then + temp.statusconnector:Fire(unpack(({__StatusLinda:receive(nil, temp.Id)})[2])) + end if status == "done" or temp.returns:get("returns") then livingThreads[temp.Id] = {false, temp.Name} temp.alive = false diff --git a/multi/integration/lanesManager/threads.lua b/multi/integration/lanesManager/threads.lua index 04277d1..b8e2c57 100644 --- a/multi/integration/lanesManager/threads.lua +++ b/multi/integration/lanesManager/threads.lua @@ -28,7 +28,7 @@ local function getOS() return "unix" end end -local function INIT(__GlobalLinda,__SleepingLinda) +local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda) local THREAD = {} THREAD.Priority_Core = 3 THREAD.Priority_High = 2 @@ -90,6 +90,10 @@ local function INIT(__GlobalLinda,__SleepingLinda) function THREAD.getID() return THREAD_ID end + function THREAD.pushStatus(...) + local args = {...} + __StatusLinda:send(nil,THREAD_ID, args) + end _G.THREAD_ID = 0 function THREAD.sleep(n) math.randomseed(os.time()) @@ -115,6 +119,6 @@ local function INIT(__GlobalLinda,__SleepingLinda) }) return GLOBAL, THREAD end -return {init = function(g,s) - return INIT(g,s) +return {init = function(g,s,st) + return INIT(g,s,st) end} \ No newline at end of file diff --git a/test.lua b/test.lua index f81fd25..5668fd6 100644 --- a/test.lua +++ b/test.lua @@ -2,18 +2,47 @@ package.path = "./?/init.lua;"..package.path multi, thread = require("multi"):init() GLOBAL, THREAD = require("multi.integration.lanesManager"):init() -THREAD:newSystemThread("Test",function() - print("In a thread...") - while true do - THREAD.sleep(1) - print("In thread") - end +thread:newThread(function() + func = THREAD:newFunction(function(count) + print("Starting Status test: ",count) + local a = 0 + while true do + a = a + 1 + THREAD.sleep(.1) + THREAD.pushStatus(a,count) + if a == count then break end + end + return "Done" + end) + local ret = func(10) + local ret2 = func(15) + local ret3 = func(20) + local s1,s2,s3 = 0,0,0 + ret.OnError(function(...) + print("Error:",...) + end) + ret2.OnError(function(...) + print("Error:",...) + end) + ret3.OnError(function(...) + print("Error:",...) + end) + ret.OnStatus(function(part,whole) + s1 = math.ceil((part/whole)*1000)/10 + print(s1) + end) + ret2.OnStatus(function(part,whole) + s2 = math.ceil((part/whole)*1000)/10 + print(s2) + end) + ret3.OnStatus(function(part,whole) + s3 = math.ceil((part/whole)*1000)/10 + print(s3) + end) + local err, timeout = thread.hold(ret.OnReturn + ret2.OnReturn + ret3.OnReturn) + print("Done") end) -multi:newTLoop(function() - print("...") -end,1) - -- local proc = multi:newProcessor("Test") -- local proc2 = multi:newProcessor("Test2") -- local proc3 = proc2:newProcessor("Test3")