Working on issue with love threaded functions not waiting when in a thread

This commit is contained in:
Ryan Ward 2023-01-18 15:42:38 -05:00
parent 6ed5555706
commit 22f1375380
2 changed files with 28 additions and 32 deletions

View File

@ -22,6 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
]] ]]
traceback = debug.traceback
local multi = {} local multi = {}
local mainloopActive = false local mainloopActive = false
local isRunning = false local isRunning = false
@ -1471,10 +1473,10 @@ function thread:newISOThread(name,func,_env,...)
env.multi = multi env.multi = multi
end end
if type(name) == "function" then if type(name) == "function" then
name = "Thread#"..threadCount name = "Thread#" .. threadCount
end end
local func = isolateFunction(func,env) local func = isolateFunction(func, env)
return thread:newThread(name,func,...) return thread:newThread(name, func,...)
end end
multi.newThread = thread.newThread multi.newThread = thread.newThread
@ -1486,7 +1488,7 @@ local ret,_
local task, thd, ref, ready local task, thd, ref, ready
local switch = { local switch = {
function(th,co)--hold function(th,co)--hold
if clock() - th.intervalR>=th.interval then if clock() - th.intervalR >= th.interval then
t0,t1,t2,t3,t4,t5,t6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16 = th.func() t0,t1,t2,t3,t4,t5,t6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16 = th.func()
if t0 then if t0 then
if t0==NIL then t0 = nil end if t0==NIL then t0 = nil end
@ -1615,6 +1617,7 @@ co_status = {
else else
ref.OnError:Fire(ref,ret,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16) ref.OnError:Fire(ref,ret,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16)
end end
print(ref.Name, traceback())
if i then if i then
table.remove(th,i) table.remove(th,i)
else else

View File

@ -20,8 +20,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
]] ]] if ISTHREAD then
if ISTHREAD then
error("You cannot require the loveManager from within a thread!") error("You cannot require the loveManager from within a thread!")
end end
local ThreadFileData = [[ local ThreadFileData = [[
@ -49,50 +48,46 @@ local multi, thread = require("multi"):init()
local THREAD = {} local THREAD = {}
__THREADID__ = 0 __THREADID__ = 0
__THREADNAME__ = "MainThread" __THREADNAME__ = "MainThread"
multi.integration={} multi.integration = {}
local THREAD = require("multi.integration.loveManager.threads") local THREAD = require("multi.integration.loveManager.threads")
local GLOBAL = THREAD.getGlobal() local GLOBAL = THREAD.getGlobal()
local THREAD_ID = 1 local THREAD_ID = 1
local OBJECT_ID = 0 local OBJECT_ID = 0
local stf = 0 local stf = 0
function multi:newSystemThread(name,func,...) function multi:newSystemThread(name, func, ...)
local c = {} local c = {}
c.name = name c.name = name
c.ID=THREAD_ID c.ID = THREAD_ID
c.thread=love.thread.newThread(ThreadFileData) c.thread = love.thread.newThread(ThreadFileData)
c.thread:start(THREAD.dump(func),c.ID,c.name,...) c.thread:start(THREAD.dump(func), c.ID, c.name, ...)
c.stab = THREAD.createStaticTable(name) c.stab = THREAD.createStaticTable(name .. c.ID)
c.OnDeath = multi:newConnection() c.OnDeath = multi:newConnection()
c.OnError = multi:newConnection() c.OnError = multi:newConnection()
GLOBAL["__THREAD_"..c.ID] = {ID=c.ID, Name=c.name, Thread=c.thread} GLOBAL["__THREAD_" .. c.ID] = {ID = c.ID, Name = c.name, Thread = c.thread}
GLOBAL["__THREAD_COUNT"] = THREAD_ID GLOBAL["__THREAD_COUNT"] = THREAD_ID
THREAD_ID=THREAD_ID + 1 THREAD_ID = THREAD_ID + 1
function c:getName() function c:getName() return c.name end
return c.name
end
thread:newThread(function() thread:newThread(function()
if name == "TempSystemThread" then if name == "TempSystemThread" then
local status_channel = love.thread.getChannel("__"..c.ID.."__MULTI__STATUS_CHANNEL__") local status_channel = love.thread.getChannel("STATCHAN_" .. c.ID)
thread.hold(function() thread.hold(function()
-- While the thread is running we might as well do something in the loop -- While the thread is running we might as well do something in the loop
local status = status_channel local status = status_channel
if status:peek()~=nil then if status:peek() ~= nil then
c.statusconnector:Fire(unpack(status:pop())) c.statusconnector:Fire(unpack(status:pop()))
end end
return not c.thread:isRunning() return not c.thread:isRunning()
end) end)
else else
thread.hold(function() thread.hold(function() return not c.thread:isRunning() end)
return not c.thread:isRunning()
end)
end end
-- If the thread is not running let's handle that. -- If the thread is not running let's handle that.
local thread_err = c.thread:getError() local thread_err = c.thread:getError()
if thread_err == "Thread Killed!\1" then if thread_err == "Thread Killed!\1" then
c.OnDeath:Fire("Thread Killed!") c.OnDeath:Fire("Thread Killed!")
elseif thread_err then elseif thread_err then
c.OnError:Fire(c,thread_err) c.OnError:Fire(c, thread_err)
elseif c.stab.returns then elseif c.stab.returns then
c.OnDeath:Fire(unpack(c.stab.returns)) c.OnDeath:Fire(unpack(c.stab.returns))
c.stab.returns = nil c.stab.returns = nil
@ -110,13 +105,11 @@ end
THREAD.newSystemThread = multi.newSystemThread THREAD.newSystemThread = multi.newSystemThread
function love.threaderror(thread, errorstr) function love.threaderror(thread, errorstr)
multi.print("Thread error!\n"..errorstr) multi.print("Thread error!\n" .. errorstr)
end end
multi.integration.GLOBAL = GLOBAL multi.integration.GLOBAL = GLOBAL
multi.integration.THREAD = THREAD multi.integration.THREAD = THREAD
require("multi.integration.loveManager.extensions") require("multi.integration.loveManager.extensions")
multi.print("Integrated Love Threading!") multi.print("Integrated Love Threading!")
return {init=function() return {init = function() return GLOBAL, THREAD end}
return GLOBAL,THREAD
end}