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
lua5.4/*
test.lua

View File

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

View File

@ -196,7 +196,7 @@ function multi:newSystemThreadedConnection(name)
end
return r
end
c.CID = THREAD.getID()
c.CID = THREAD_ID
c.subscribe = multi:newSystemThreadedQueue("SUB_STC_"..self.Name):init()
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.
@ -262,7 +262,7 @@ function multi:newSystemThreadedConnection(name)
function c:Fire(...)
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
link:push {self.TRIG, args}
end
@ -284,7 +284,7 @@ function multi:newSystemThreadedConnection(name)
tempMT.__index = self.proxy_conn
tempMT.__call = function(t,func) self.proxy_conn(func) end
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()
local item
local link_self_ref = multi:newSystemThreadedQueue()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
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)
local LANES, LOVE, PSEUDO = 1, 2, 3
local env, we_good
@ -31,18 +31,21 @@ multi.print("Testing THREAD.setENV() if the multi_assert is not found then there
THREAD.setENV({
multi_assert = function(expected, actual, s)
if expected ~= actual then
multi.error(s .. " Expected: '".. expected .."' Actual: '".. actual .."'")
multi.error(s .. " Expected: '".. tostring(expected) .."' Actual: '".. tostring(actual) .."'")
end
end
},"Test_ENV")
})
multi:newThread("Scheduler Thread",function()
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)
queue = THREAD.waitFor("Test_Queue"):init()
THREAD.exposeENV("Test_ENV")
multi_assert("Test_Thread_1", THREAD.getName(), "Thread name does not match!")
multi_assert("Test_Thread_1", THREAD_NAME, "Thread name does not match!")
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("table", type(f), "Argument f is not a table!")