diff --git a/multi/integration/lanesManager/threads.lua b/multi/integration/lanesManager/threads.lua index b8e2c57..68cb733 100644 --- a/multi/integration/lanesManager/threads.lua +++ b/multi/integration/lanesManager/threads.lua @@ -28,6 +28,7 @@ local function getOS() return "unix" end end + local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda) local THREAD = {} THREAD.Priority_Core = 3 @@ -37,12 +38,15 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda) THREAD.Priority_Below_Normal = -1 THREAD.Priority_Low = -2 THREAD.Priority_Idle = -3 + function THREAD.set(name, val) __GlobalLinda:set(name, val) end + function THREAD.get(name) return __GlobalLinda:get(name) end + function THREAD.waitFor(name) local function wait() math.randomseed(os.time()) @@ -53,14 +57,17 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda) until __GlobalLinda:get(name) return __GlobalLinda:get(name) end + if getOS() == "windows" then THREAD.__CORES = tonumber(os.getenv("NUMBER_OF_PROCESSORS")) else THREAD.__CORES = tonumber(io.popen("nproc --all"):read("*n")) end + function THREAD.getCores() return THREAD.__CORES end + function THREAD.getConsole() local c = {} c.queue = _Console @@ -73,32 +80,41 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda) end return c end + function THREAD.getThreads() return GLOBAL.__THREADS__ end + if os.getOS() == "windows" then THREAD.__CORES = tonumber(os.getenv("NUMBER_OF_PROCESSORS")) else THREAD.__CORES = tonumber(io.popen("nproc --all"):read("*n")) end + 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 = {...} __StatusLinda:send(nil,THREAD_ID, args) end + _G.THREAD_ID = 0 + function THREAD.sleep(n) math.randomseed(os.time()) __SleepingLinda:receive(n, "__non_existing_variable") end + function THREAD.hold(n) local function wait() math.randomseed(os.time()) @@ -108,6 +124,7 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda) wait() until n() end + local GLOBAL = {} setmetatable(GLOBAL, { __index = function(t, k) @@ -119,6 +136,7 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda) }) return GLOBAL, THREAD end + return {init = function(g,s,st) return INIT(g,s,st) end} \ No newline at end of file diff --git a/multi/integration/loveManager/threads.lua b/multi/integration/loveManager/threads.lua index afcb951..c66e7dc 100644 --- a/multi/integration/loveManager/threads.lua +++ b/multi/integration/loveManager/threads.lua @@ -28,12 +28,15 @@ require("love.thread") local socket = require("socket") local multi, thread = require("multi").init() local threads = {} + function threads.loadDump(d) return loadstring(d:getString()) end + function threads.dump(func) return love.data.newByteData(string.dump(func)) end + local fRef = {"func",nil} local function manage(channel, value) channel:clear() @@ -45,6 +48,7 @@ local function manage(channel, value) channel:push(value) end end + local function RandomVariable(length) local res = {} math.randomseed(socket.gettime()*10000) @@ -53,12 +57,14 @@ local function RandomVariable(length) end return table.concat(res) end + local GNAME = "__GLOBAL_" local proxy = {} function threads.set(name,val) if not proxy[name] then proxy[name] = love.thread.getChannel(GNAME..name) end proxy[name]:performAtomic(manage, val) end + function threads.get(name) if not proxy[name] then proxy[name] = love.thread.getChannel(GNAME..name) end local dat = proxy[name]:peek() @@ -68,6 +74,7 @@ function threads.get(name) return dat end end + function threads.waitFor(name) if thread.isThread() then return thread.hold(function() @@ -83,23 +90,28 @@ function threads.waitFor(name) end return dat end + function threads.package(name,val) local init = val.init val.init=threads.dump(val.init) GLOBAL[name]=val val.init=init end + function threads.getCores() return love.system.getProcessorCount() end + function threads.kill() error("Thread Killed!\1") end + function THREAD.pushStatus(...) local status_channel = love.thread.getChannel("__"..__THREADID__.."__MULTI__STATUS_CHANNEL__") local args = {...} status_channel:push(__THREADID__, args) end + function threads.getThreads() local t = {} for i=1,GLOBAL["__THREAD_COUNT"] do @@ -107,18 +119,23 @@ function threads.getThreads() end return t end + 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 + function threads.getGlobal() return setmetatable({}, { @@ -131,6 +148,7 @@ function threads.getGlobal() } ) end + function threads.createTable(n) local _proxy = {} local function set(name,val) @@ -157,6 +175,7 @@ function threads.createTable(n) } ) end + function threads.getConsole() local c = {} c.queue = love.thread.getChannel("__CONSOLE__") @@ -169,6 +188,7 @@ function threads.getConsole() end return c end + if not ISTHREAD then local clock = os.clock local lastproc = clock() @@ -187,6 +207,7 @@ if not ISTHREAD then end end) end + function threads.createStaticTable(n) local __proxy = {} local function set(name,val) @@ -218,10 +239,12 @@ function threads.createStaticTable(n) } ) end + function threads.hold(n) local dat while not(dat) do dat = n() end end + return threads \ No newline at end of file diff --git a/multi/integration/pesudoManager/init.lua b/multi/integration/pesudoManager/init.lua index 6c19d28..6297dc5 100644 --- a/multi/integration/pesudoManager/init.lua +++ b/multi/integration/pesudoManager/init.lua @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] package.path = "?/init.lua;?.lua;" .. package.path -local multi, thread = require("multi").init() +local multi, thread = require("multi"):init() if multi.integration then return { @@ -32,7 +32,7 @@ if multi.integration then } end -local GLOBAL, THREAD = require("multi.integration.pesudoManager.threads"):init(thread) +local GLOBAL, THREAD = require("multi.integration.pesudoManager.threads").init(thread) function multi:canSystemThread() -- We are emulating system threading return true @@ -41,6 +41,7 @@ end function multi:getPlatform() return "pesudo" end + local function split(str) local tab = {} for word in string.gmatch(str, '([^,]+)') do @@ -48,13 +49,15 @@ local function split(str) end return tab end + THREAD.newFunction=thread.newFunction + local id = 0 function multi:newSystemThread(name,func,...) GLOBAL["$THREAD_NAME"] = name GLOBAL["$__THREADNAME__"] = name GLOBAL["$THREAD_ID"] = id - --GLOBAL["$thread"] = thread + GLOBAL["$thread"] = thread local env = { GLOBAL = GLOBAL, THREAD = THREAD, @@ -75,6 +78,7 @@ function multi:newSystemThread(name,func,...) end) id = id + 1 end + THREAD.newSystemThread = multi.newSystemThread -- System threads as implemented here cannot share memory, but use a message passing system. -- An isolated thread allows us to mimic that behavior so if access data from the "main" thread happens things will not work. This behavior is in line with how the system threading works diff --git a/multi/integration/pesudoManager/threads.lua b/multi/integration/pesudoManager/threads.lua index 901bef9..0cbae07 100644 --- a/multi/integration/pesudoManager/threads.lua +++ b/multi/integration/pesudoManager/threads.lua @@ -28,7 +28,9 @@ local function getOS() return "unix" end end -local function INIT(env,thread) + +local function INIT(thread) + print("T",thread.sleep) local THREAD = {} local GLOBAL = {} THREAD.Priority_Core = 3 @@ -38,23 +40,29 @@ local function INIT(env,thread) THREAD.Priority_Below_Normal = -1 THREAD.Priority_Low = -2 THREAD.Priority_Idle = -3 + function THREAD.set(name, val) GLOBAL[name] = val end + function THREAD.get(name) return GLOBAL[name] end + function THREAD.waitFor(name) return thread.hold(function() return GLOBAL[name] end) end + if getOS() == "windows" then THREAD.__CORES = tonumber(os.getenv("NUMBER_OF_PROCESSORS")) else THREAD.__CORES = tonumber(io.popen("nproc --all"):read("*n")) end + function THREAD.getCores() return THREAD.__CORES end + function THREAD.getConsole() local c = {} function c.print(...) @@ -65,33 +73,38 @@ local function INIT(env,thread) end return c end + function THREAD.getThreads() return {}--GLOBAL.__THREADS__ end - THREAD.pushstatus = thread.pushstatus + + THREAD.pushStatus = thread.pushStatus + if os.getOS() == "windows" then THREAD.__CORES = tonumber(os.getenv("NUMBER_OF_PROCESSORS")) else THREAD.__CORES = tonumber(io.popen("nproc --all"):read("*n")) end + function THREAD.kill() error("Thread was killed!") end + function THREAD.getName() return GLOBAL["$THREAD_NAME"] end + function THREAD.getID() return GLOBAL["$THREAD_ID"] end - THREAD.pushstatus - function THREAD.sleep(n) - thread.sleep(n) - end - function THREAD.hold(n) - return thread.hold(n) - end + + THREAD.sleep = thread.sleep + + THREAD.hold = thread.hold + return GLOBAL, THREAD end + return {init = function(thread) - return INIT(nil,thread) + return INIT(thread) end} \ No newline at end of file diff --git a/test.lua b/test.lua index d17f6cc..78b7dcf 100644 --- a/test.lua +++ b/test.lua @@ -1,6 +1,6 @@ package.path = "./?/init.lua;"..package.path multi, thread = require("multi"):init() -GLOBAL, THREAD = require("multi.integration.lanesManager"):init() +GLOBAL, THREAD = require("multi.integration.pesudoManager"):init() func = THREAD:newFunction(function(count) print("Starting Status test: ",count) @@ -13,6 +13,7 @@ func = THREAD:newFunction(function(count) end return "Done" end) + local ret = func(10) local ret2 = func(15) local ret3 = func(20)