Changes to threads

This commit is contained in:
Ryan Ward 2023-05-07 23:03:15 -04:00
parent ec9f7dec61
commit a5add93747
12 changed files with 63 additions and 67 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.code-workspace *.code-workspace
lua5.4/* lua5.4/*
test.lua

View File

@ -259,6 +259,8 @@ Changed
Removed Removed
--- ---
- THREAD.getName() use THREAD_NAME instead
- THREAD.getID() use THREAD_ID instead
- conn:SetHelper(func) -- With the removal of old Connect this function is no longer needed - 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 - connection events can no longer can be chained with connect. Connect only takes a function that you want to connect

View File

@ -473,13 +473,11 @@ end
-- Used with ISO Threads -- Used with ISO Threads
local function isolateFunction(func, env) local function isolateFunction(func, env)
local dmp = string.dump(func)
local env = env or {}
if setfenv then if setfenv then
local f = loadstring(dmp, "IsolatedThread_PesudoThreading") return setfenv(func, env)
setfenv(f, env)
return f
else else
local env = env or {}
local dmp = string.dump(func)
return load(dmp,"IsolatedThread_PesudoThreading", "bt", env) return load(dmp,"IsolatedThread_PesudoThreading", "bt", env)
end end
end end
@ -1585,9 +1583,9 @@ function thread:newThread(name, func, ...)
return c return c
end end
function thread:newISOThread(name, func, _env, ...) function thread:newISOThread(name, func, env, ...)
local func = func or name local func = func or name
local env = _env or {} local env = env or {}
if not env.thread then if not env.thread then
env.thread = thread env.thread = thread
end end
@ -2340,7 +2338,7 @@ function multi.error(self, err)
if type(err) == "bool" then crash = err end if type(err) == "bool" then crash = err end
if type(self) == "string" then err = self end if type(self) == "string" then err = self end
io.write("\x1b[91mERROR:\x1b[0m " .. err .. "\n") io.write("\x1b[91mERROR:\x1b[0m " .. err .. "\n")
error("^^^") error("^^^ " .. multi:getCurrentProcess():getFullName() .. " " .. multi:getCurrentTask().Type)
if multi.defaultSettings.error then if multi.defaultSettings.error then
os.exit(1) os.exit(1)
end end

View File

@ -196,7 +196,7 @@ function multi:newSystemThreadedConnection(name)
end end
return r return r
end end
c.CID = THREAD.getID() c.CID = THREAD_ID
c.subscribe = multi:newSystemThreadedQueue("SUB_STC_"..self.Name):init() c.subscribe = multi:newSystemThreadedQueue("SUB_STC_"..self.Name):init()
c.Name = name c.Name = name
c.links = {} -- All triggers sent from main connection. When a connection is triggered on another thread, they speak to the main then send stuff out. c.links = {} -- All triggers sent from main connection. When a connection is triggered on another thread, they speak to the main then send stuff out.
@ -262,7 +262,7 @@ function multi:newSystemThreadedConnection(name)
function c:Fire(...) function c:Fire(...)
local args = {...} local args = {...}
if self.CID == THREAD.getID() then -- Host Call if self.CID == THREAD_ID then -- Host Call
for _, link in pairs(self.links) do for _, link in pairs(self.links) do
link:push {self.TRIG, args} link:push {self.TRIG, args}
end end
@ -284,7 +284,7 @@ function multi:newSystemThreadedConnection(name)
tempMT.__index = self.proxy_conn tempMT.__index = self.proxy_conn
tempMT.__call = function(t,func) self.proxy_conn(func) end tempMT.__call = function(t,func) self.proxy_conn(func) end
setmetatable(self, tempMT) setmetatable(self, tempMT)
if self.CID == THREAD.getID() then return self end if self.CID == THREAD_ID then return self end
thread:newThread("STC_CONN_MAN"..name,function() thread:newThread("STC_CONN_MAN"..name,function()
local item local item
local link_self_ref = multi:newSystemThreadedQueue() local link_self_ref = multi:newSystemThreadedQueue()

View File

@ -89,14 +89,6 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda, __Console)
error("Thread was killed!\1") error("Thread was killed!\1")
end end
function THREAD.getName()
return THREAD_NAME
end
function THREAD.getID()
return THREAD_ID
end
function THREAD.pushStatus(...) function THREAD.pushStatus(...)
local args = {...} local args = {...}
__StatusLinda:send(nil,THREAD_ID, args) __StatusLinda:send(nil,THREAD_ID, args)

View File

@ -34,6 +34,8 @@ __IMPORTS = {...}
__FUNC__=table.remove(__IMPORTS,1) __FUNC__=table.remove(__IMPORTS,1)
__THREADID__=table.remove(__IMPORTS,1) __THREADID__=table.remove(__IMPORTS,1)
__THREADNAME__=table.remove(__IMPORTS,1) __THREADNAME__=table.remove(__IMPORTS,1)
THREAD_NAME = __THREADNAME__
THREAD_ID = __THREADID__
math.randomseed(__THREADID__) math.randomseed(__THREADID__)
math.random() math.random()
math.random() math.random()

View File

@ -114,14 +114,6 @@ function threads.getThread(n)
return GLOBAL["__THREAD_"..n] return GLOBAL["__THREAD_"..n]
end end
function threads.getName()
return __THREADNAME__
end
function threads.getID()
return __THREADID__
end
function threads.sleep(n) function threads.sleep(n)
love.timer.sleep(n) love.timer.sleep(n)
end end

View File

@ -50,6 +50,7 @@ function multi:newSystemThreadedQueue(name)
GLOBAL[name or "_"] = c GLOBAL[name or "_"] = c
return c return c
end end
function multi:newSystemThreadedTable(name) function multi:newSystemThreadedTable(name)
local c = {} local c = {}
function c:init() function c:init()
@ -58,6 +59,7 @@ function multi:newSystemThreadedTable(name)
GLOBAL[name or "_"] = c GLOBAL[name or "_"] = c
return c return c
end end
local setfenv = setfenv local setfenv = setfenv
if not setfenv then if not setfenv then
if not debug then if not debug then
@ -68,6 +70,7 @@ if not setfenv then
end end
end end
end end
function multi:newSystemThreadedJobQueue(n) function multi:newSystemThreadedJobQueue(n)
local c = {} local c = {}
c.cores = n or THREAD.getCores()*2 c.cores = n or THREAD.getCores()*2
@ -76,26 +79,32 @@ function multi:newSystemThreadedJobQueue(n)
local ID=1 local ID=1
local jid = 1 local jid = 1
local env = {} local env = {}
setmetatable(env,{ setmetatable(env,{
__index = _G __index = _G
}) })
local funcs = {} local funcs = {}
function c:doToAll(func) function c:doToAll(func)
setfenv(func,env)() setfenv(func,env)()
return self return self
end end
function c:registerFunction(name,func) function c:registerFunction(name,func)
funcs[name] = setfenv(func,env) funcs[name] = setfenv(func,env)
return self return self
end end
function c:pushJob(name,...) function c:pushJob(name,...)
table.insert(jobs,{name,jid,{...}}) table.insert(jobs,{name,jid,{...}})
jid = jid + 1 jid = jid + 1
return jid-1 return jid-1
end end
function c:isEmpty() function c:isEmpty()
return #jobs == 0 return #jobs == 0
end end
local nFunc = 0 local nFunc = 0
function c:newFunction(name,func,holup) -- This registers with the queue function c:newFunction(name,func,holup) -- This registers with the queue
local func = stripUpValues(func) local func = stripUpValues(func)
@ -133,7 +142,7 @@ function multi:newSystemThreadedJobQueue(n)
thread.sleep(.05) thread.sleep(.05)
end end
end end
end).OnError(print) end).OnError(multi.error)
end end
return c return c
end end

View File

@ -31,8 +31,8 @@ if multi.integration then
end end
} }
end end
local activator = require("multi.integration.pseudoManager.threads")
local GLOBAL, THREAD = require("multi.integration.pseudoManager.threads").init(thread) local GLOBAL, THREAD = activator.init(thread)
function multi:canSystemThread() -- We are emulating system threading function multi:canSystemThread() -- We are emulating system threading
return true return true
@ -55,22 +55,28 @@ tab = split(tab)
local id = 0 local id = 0
function multi:newSystemThread(name,func,...) print("Outerglobal",_G)
GLOBAL["$THREAD_NAME"] = name
GLOBAL["$__THREADNAME__"] = name
GLOBAL["$THREAD_ID"] = id
GLOBAL["$thread"] = thread
local env = { function multi:newSystemThread(name, func, ...)
local env
env = {
GLOBAL = GLOBAL, GLOBAL = GLOBAL,
THREAD = THREAD, THREAD = THREAD,
THREAD_NAME = name, THREAD_NAME = tostring(name),
__THREADNAME__ = name, __THREADNAME__ = tostring(name),
test = "testing",
THREAD_ID = id, THREAD_ID = id,
thread = thread, thread = thread,
multi = multi,
} }
env.__env = env for i, v in pairs(_G) do
if not(env[i]) and not(i == "_G") and not(i == "local_global") then
env[i] = v
else
multi.warn("skipping:",i)
end
end
if GLOBAL["__env"] then if GLOBAL["__env"] then
for i,v in pairs(GLOBAL["__env"]) do for i,v in pairs(GLOBAL["__env"]) do
@ -78,9 +84,9 @@ function multi:newSystemThread(name,func,...)
end end
end end
for i = 1,#tab do env._G = env
env[tab[i]] = _G[tab[i]]
end local GLOBAL, THREAD = activator.init(thread, env)
local th = thread:newISOThread(name, func, env, ...) local th = thread:newISOThread(name, func, env, ...)

View File

@ -33,6 +33,7 @@ end
local function INIT(thread) local function INIT(thread)
local THREAD = {} local THREAD = {}
local GLOBAL = {} local GLOBAL = {}
THREAD.Priority_Core = 3 THREAD.Priority_Core = 3
THREAD.Priority_High = 2 THREAD.Priority_High = 2
THREAD.Priority_Above_Normal = 1 THREAD.Priority_Above_Normal = 1
@ -84,14 +85,6 @@ local function INIT(thread)
error("Thread was killed!") error("Thread was killed!")
end end
function THREAD.getName()
return GLOBAL["$THREAD_NAME"]
end
function THREAD.getID()
return GLOBAL["$THREAD_ID"]
end
THREAD.sleep = thread.sleep THREAD.sleep = thread.sleep
THREAD.hold = thread.hold THREAD.hold = thread.hold
@ -107,18 +100,17 @@ local function INIT(thread)
end end
function THREAD.exposeENV(name) function THREAD.exposeENV(name)
print("env",__env)
name = name or "__env" name = name or "__env"
local env = THREAD.getENV(name) local env = THREAD.getENV(name)
for i,v in pairs(env) do for i,v in pairs(env) do
-- This may need to be reworked! -- This may need to be reworked!
_G[i] = v local_global[i] = v
end end
end end
return GLOBAL, THREAD return GLOBAL, THREAD
end end
return {init = function(thread) return {init = function(thread, global)
return INIT(thread) return INIT(thread, global)
end} end}

View File

@ -1 +0,0 @@
D:/VSCWorkspace/multi

View File

@ -1,5 +1,5 @@
package.path = "../?/init.lua;../?.lua;"..package.path package.path = "../?/init.lua;../?.lua;"..package.path
multi, thread = require("multi"):init{print=true,warn=true,error=false}--{priority=true} multi, thread = require("multi"):init{}--{priority=true}
proc = multi:newProcessor("Thread Test",true) proc = multi:newProcessor("Thread Test",true)
local LANES, LOVE, PSEUDO = 1, 2, 3 local LANES, LOVE, PSEUDO = 1, 2, 3
local env, we_good local env, we_good
@ -31,18 +31,21 @@ multi.print("Testing THREAD.setENV() if the multi_assert is not found then there
THREAD.setENV({ THREAD.setENV({
multi_assert = function(expected, actual, s) multi_assert = function(expected, actual, s)
if expected ~= actual then if expected ~= actual then
multi.error(s .. " Expected: '".. expected .."' Actual: '".. actual .."'") multi.error(s .. " Expected: '".. tostring(expected) .."' Actual: '".. tostring(actual) .."'")
end end
end end
},"Test_ENV") })
multi:newThread("Scheduler Thread",function() multi:newThread("Scheduler Thread",function()
queue = multi:newSystemThreadedQueue("Test_Queue"):init() queue = multi:newSystemThreadedQueue("Test_Queue"):init()
multi:newSystemThread("Test_Thread_0", function()
print("The name should be Test_Thread_0",THREAD_NAME,THREAD_NAME,_G.THREAD_NAME)
end)
th1 = multi:newSystemThread("Test_Thread_1", function(a,b,c,d,e,f) th1 = multi:newSystemThread("Test_Thread_1", function(a,b,c,d,e,f)
queue = THREAD.waitFor("Test_Queue"):init() queue = THREAD.waitFor("Test_Queue"):init()
THREAD.exposeENV("Test_ENV") multi_assert("Test_Thread_1", THREAD_NAME, "Thread name does not match!")
multi_assert("Test_Thread_1", THREAD.getName(), "Thread name does not match!")
multi_assert("Passing some args", a, "First argument is not as expected 'Passing some args'") multi_assert("Passing some args", a, "First argument is not as expected 'Passing some args'")
multi_assert(true, e, "Argument e is not true!") multi_assert(true, e, "Argument e is not true!")
multi_assert("table", type(f), "Argument f is not a table!") multi_assert("table", type(f), "Argument f is not a table!")