diff --git a/.gitignore b/.gitignore index f3ede8b..0a96143 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.code-workspace -lua5.4/* \ No newline at end of file +lua5.4/* +test.lua \ No newline at end of file diff --git a/docs/changes.md b/docs/changes.md index 20457ea..92673dd 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -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 diff --git a/init.lua b/init.lua index 092cf1f..16ec046 100644 --- a/init.lua +++ b/init.lua @@ -473,15 +473,13 @@ 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 - else - return load(dmp,"IsolatedThread_PesudoThreading", "bt", env) - end + if setfenv then + return setfenv(func, env) + else + local env = env or {} + local dmp = string.dump(func) + return load(dmp,"IsolatedThread_PesudoThreading", "bt", env) + end end function multi:Break() @@ -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 diff --git a/integration/lanesManager/extensions.lua b/integration/lanesManager/extensions.lua index 42eee9f..e2db201 100644 --- a/integration/lanesManager/extensions.lua +++ b/integration/lanesManager/extensions.lua @@ -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() diff --git a/integration/lanesManager/threads.lua b/integration/lanesManager/threads.lua index 274a562..a1477ef 100644 --- a/integration/lanesManager/threads.lua +++ b/integration/lanesManager/threads.lua @@ -88,14 +88,6 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda, __Console) function THREAD.kill() -- trigger the lane destruction 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 = {...} diff --git a/integration/loveManager/init.lua b/integration/loveManager/init.lua index e12098a..8f26e30 100644 --- a/integration/loveManager/init.lua +++ b/integration/loveManager/init.lua @@ -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() diff --git a/integration/loveManager/threads.lua b/integration/loveManager/threads.lua index b0de12f..25008fe 100644 --- a/integration/loveManager/threads.lua +++ b/integration/loveManager/threads.lua @@ -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 diff --git a/integration/pseudoManager/extensions.lua b/integration/pseudoManager/extensions.lua index bd59f10..5227573 100644 --- a/integration/pseudoManager/extensions.lua +++ b/integration/pseudoManager/extensions.lua @@ -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 diff --git a/integration/pseudoManager/init.lua b/integration/pseudoManager/init.lua index 887d1ad..e495223 100644 --- a/integration/pseudoManager/init.lua +++ b/integration/pseudoManager/init.lua @@ -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 GLOBAL, THREAD = activator.init(thread, env) - local th = thread:newISOThread(name,func,env,...) + local th = thread:newISOThread(name, func, env, ...) id = id + 1 diff --git a/integration/pseudoManager/threads.lua b/integration/pseudoManager/threads.lua index 9aa2621..2b46982 100644 --- a/integration/pseudoManager/threads.lua +++ b/integration/pseudoManager/threads.lua @@ -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} \ No newline at end of file diff --git a/tests/multi b/tests/multi deleted file mode 120000 index d78adc0..0000000 --- a/tests/multi +++ /dev/null @@ -1 +0,0 @@ -D:/VSCWorkspace/multi \ No newline at end of file diff --git a/tests/threadtests.lua b/tests/threadtests.lua index 6698e8a..f7126d1 100644 --- a/tests/threadtests.lua +++ b/tests/threadtests.lua @@ -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!")