Fixing pseudothreading

This commit is contained in:
Ryan Ward 2020-05-15 01:38:24 -04:00
parent 300827b7bd
commit d2ce7e070b
4 changed files with 46 additions and 23 deletions

View File

@ -1179,7 +1179,9 @@ function multi:newISOThread(name,func,...)
if type(name) == "function" then if type(name) == "function" then
name = "Thread#"..threadCount name = "Thread#"..threadCount
end end
local Gref = {}
local env = { local env = {
THREAD_NAME = name,
thread = thread, thread = thread,
multi = multi, multi = multi,
coroutine = coroutine, coroutine = coroutine,
@ -1197,7 +1199,16 @@ function multi:newISOThread(name,func,...)
env[i]=v env[i]=v
end end
end end
local c={}
function c:inject(tab)
for i,v in pairs(tab) do
Gref[i] = v
env[i] = v
end
end
function c:start()
setmetatable(env,{ setmetatable(env,{
__index = Gref,
__newindex = function(t,k,v) __newindex = function(t,k,v)
if type(v)=="function" then if type(v)=="function" then
rawset(t,k,thread:newFunction(v)) rawset(t,k,thread:newFunction(v))
@ -1206,14 +1217,13 @@ function multi:newISOThread(name,func,...)
end end
end end
}) })
local func = multi.setEnv(func,env) self.thread=coroutine.create(multi.setEnv(func,env))
local c={} end
env.self = c env.self = c
c.TempRets = {nil,nil,nil,nil,nil,nil,nil,nil,nil,nil} c.TempRets = {nil,nil,nil,nil,nil,nil,nil,nil,nil,nil}
c.startArgs = {...} c.startArgs = {...}
c.ref={} c.ref={}
c.Name=name c.Name=name
c.thread=coroutine.create(func)
c.sleep=1 c.sleep=1
c.Type="thread" c.Type="thread"
c.TID = threadid c.TID = threadid

View File

@ -43,7 +43,17 @@ function multi:getPlatform()
end end
THREAD.newFunction=thread.newFunction THREAD.newFunction=thread.newFunction
multi.newSystemThread = multi.newISOThread local id = 0
function multi:newSystemThread(name,func,...)
local t = multi:newISOThread(name,func,...)
t:inject{
GLOBAL = GLOBAL,
THREAD = THREAD,
THREAD_ID = id
}
id = id + 1
t:start()
end
-- System threads as implemented here cannot share memory, but use a message passing system. -- 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 -- 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

View File

@ -82,7 +82,6 @@ local function INIT()
function THREAD.getID() function THREAD.getID()
return THREAD_ID return THREAD_ID
end end
_G.THREAD_ID = 0
function THREAD.sleep(n) function THREAD.sleep(n)
thread.sleep(n) thread.sleep(n)
end end

View File

@ -3,16 +3,20 @@ multi,thread = require("multi"):init()
GLOBAL,THREAD = require("multi.integration.pesudoManager"):init() GLOBAL,THREAD = require("multi.integration.pesudoManager"):init()
test = true test = true
local haha = true local haha = true
jq = multi:newSystemThreadedJobQueue(100) -- Job queue with 4 worker threads local test = multi:newSystemThreadedTable("test"):init()
func = jq:newFunction("test",function(a,b) test['hi'] = "Hello World!!!"
THREAD.sleep(2) test['bye'] = "Bye World!!!"
return a+b multi:newSystemThread("test_1",function()
print(THREAD_NAME,THREAD_ID,THREAD.getName())
print("thread",GLOBAL,THREAD,test,haha)
tab = THREAD.waitFor("test"):init()
print(tab["hi"])
end) end)
multi:newSystemThread("test_2",function()
for i = 1,100 do print(THREAD_NAME,THREAD_ID,THREAD.getName())
func(i,i*3).connect(function(data) print("thread",GLOBAL,THREAD,test,haha)
print(data) tab = THREAD.waitFor("test"):init()
print(tab["bye"])
end) end)
end
multi:mainloop() multi:mainloop()