From 7da192e8dc920ef158f4cafbdd5cd0d91b53b215 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Sat, 21 Jan 2023 13:18:44 -0500 Subject: [PATCH] Fixed some issues with threads --- docs/changes.md | 27 +++++++++++++++++++++++++-- init.lua | 20 ++++++++++++++++---- integration/loveManager/init.lua | 11 ++++++++--- integration/loveManager/threads.lua | 4 ++-- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index a61588e..503444b 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -148,11 +148,34 @@ Changed Removed --- -- conn:SetHelper(func) -- With the removal of old Connect this function is nolonger needed -- connection events can nolonger can be chained with connect. Connect only takes a function that you want to connect +- conn:SetHelper(func) -- With the removal of old Connect this function is no longer needed +- connection events can no longer can be chained with connect. Connect only takes a function that you want to connect Fixed --- +- Oversight with how pushStatus worked with nesting threaded functions, connections and forwarding events + ```lua + func = thread:newFunction(function() + for i=1,10 do + thread.sleep(1) + thread.pushStatus(i) + end + end) + + func2 = thread:newFunction(function() + local ref = func() + ref.OnStatus(function(num) + -- do stuff with this data + + thread.pushStatus(num*2) -- Technically this is not ran within a thread. This is ran outside of a thread inside the thread handler. + end) + end) + + local handler = func2() + handler.OnStatus(function(num) + print(num) + end) + ``` ToDo --- diff --git a/init.lua b/init.lua index cfba28b..a38ab30 100644 --- a/init.lua +++ b/init.lua @@ -30,6 +30,7 @@ local thread = {} local processes = {} local find_optimization = false local threadManager +local __CurrentConnectionThread if not _G["$multi"] then _G["$multi"] = {multi = multi, thread = thread} @@ -305,7 +306,7 @@ function multi:newConnection(protect, func, kill) end function c:Fire(...) - for i=1,#call_funcs do + for i=1, #call_funcs do call_funcs[i](...) end end @@ -314,6 +315,17 @@ function multi:newConnection(protect, func, kill) function c:fastMode() return self end function c:Connect(func) + local th + if thread.getRunningThread then + th = thread.getRunningThread() + end + if th then + local fref = func + func = function(...) + __CurrentConnectionThread = th + fref(...) + end + end table.insert(call_funcs, func) local temp = {fast = true} setmetatable(temp,{ @@ -1244,7 +1256,7 @@ local function cleanReturns(...) end function thread.pushStatus(...) - local t = thread.getRunningThread() + local t = thread.getRunningThread() or __CurrentConnectionThread t.statusconnector:Fire(...) end @@ -1286,8 +1298,8 @@ function thread:newFunctionBase(generator, holdme) return cleanReturns(rets[1],rets[2],rets[3],rets[4],rets[5],rets[6],rets[7],rets[8],rets[9],rets[10],rets[11],rets[12],rets[13],rets[14],rets[15],rets[16]) end end - tfunc.__call = function(t,...) - if t.Active == false then + tfunc.__call = function(th,...) + if th.Active == false then if holdme then return nil, "Function is paused" end diff --git a/integration/loveManager/init.lua b/integration/loveManager/init.lua index 0cb3ee9..b58a84e 100644 --- a/integration/loveManager/init.lua +++ b/integration/loveManager/init.lua @@ -47,7 +47,14 @@ multi.integration.THREAD = THREAD pcall(require,"multi.integration.loveManager.extensions") stab["returns"] = {THREAD.loadDump(__FUNC__)(unpack(__IMPORTS))} ]] + local multi, thread = require("multi"):init() + +-- We do not want to load this module twice +if multi.integration and multi.integration.THREAD then + return multi.integration.GLOBAL, multi.integration.THREAD +end + local THREAD = {} __THREADID__ = 0 __THREADNAME__ = "MainThread" @@ -55,8 +62,6 @@ multi.integration = {} local THREAD = require("multi.integration.loveManager.threads") local GLOBAL = THREAD.getGlobal() local THREAD_ID = 1 -local OBJECT_ID = 0 -local stf = 0 function multi:newSystemThread(name, func, ...) local c = {} @@ -72,7 +77,7 @@ function multi:newSystemThread(name, func, ...) THREAD_ID = THREAD_ID + 1 function c:getName() return c.name end thread:newThread(name .. "_System_Thread_Handler",function() - if name == "TempSystemThread" then + if name == "SystemThreaded Function Handler" then local status_channel = love.thread.getChannel("STATCHAN_" .. c.ID) thread.hold(function() -- While the thread is running we might as well do something in the loop diff --git a/integration/loveManager/threads.lua b/integration/loveManager/threads.lua index 5176eaf..74b4954 100644 --- a/integration/loveManager/threads.lua +++ b/integration/loveManager/threads.lua @@ -97,9 +97,9 @@ function threads.kill() end function threads.pushStatus(...) - local status_channel = love.thread.getChannel("__"..__THREADID__.."__MULTI__STATUS_CHANNEL__") + local status_channel = love.thread.getChannel("STATCHAN_" ..__THREADID__) local args = {...} - status_channel:push(__THREADID__, args) + status_channel:push(args) end function threads.getThreads()