Really close to portable proxies, currently extreamly unstable!

This commit is contained in:
Ryan Ward 2023-06-18 00:08:51 -04:00
parent 6fe10b22ab
commit 1b3e3303e9

View File

@ -128,7 +128,6 @@ function multi:newProxy(list)
end).OnError(print) end).OnError(print)
return self return self
else else
print("INIT IN",THREAD_NAME)
local multi, thread = require("multi"):init() local multi, thread = require("multi"):init()
local me = self local me = self
self.proc_name = proc_name self.proc_name = proc_name
@ -142,7 +141,6 @@ function multi:newProxy(list)
for _,v in pairs(self.funcs) do for _,v in pairs(self.funcs) do
if type(v) == "table" then if type(v) == "table" then
-- We have a connection -- We have a connection
print("Init Conn",v[1],THREAD_NAME)
v[2]:init(proc_name) v[2]:init(proc_name)
self["_"..v[1]] = v[2] self["_"..v[1]] = v[2]
v[2].Parent = self v[2].Parent = self
@ -150,11 +148,11 @@ function multi:newProxy(list)
self[v[1]] = multi:newConnection() self[v[1]] = multi:newConnection()
thread:newThread(function() thread:newThread(function()
print("HOLD:","_"..v[1],self["_"..v[1]].Type)
while true do while true do
self[v[1]]:Fire(thread.hold(self["_"..v[1]])) local data = thread.hold(self["_"..v[1]])
self[v[1]]:Fire(data)
end end
end) end).OnError(multi.error)
else else
self[v] = thread:newFunction(function(self,...) self[v] = thread:newFunction(function(self,...)
if self == me then if self == me then
@ -198,36 +196,72 @@ local targets = {}
local references = {} local references = {}
local nFunc = 0 local nFunc = 0
function multi:newTargetedFunction(ID, name, func, holup) -- This registers with the queue function multi:newTargetedFunction(ID, proxy, name, func, holup) -- This registers with the queue
if type(name)=="function" then if type(name)=="function" then
holup = func holup = func
func = name func = name
name = "JQ_TFunc_"..nFunc name = "JQ_TFunc_"..nFunc
end end
nFunc = nFunc + 1 nFunc = nFunc + 1
multi:executeOnProcess(proxy.proc_name, function(proc, name, func)
proc.jobqueue:registerFunction(name, func) proc.jobqueue:registerFunction(name, func)
end, name, func)
return thread:newFunction(function(...) return thread:newFunction(function(...)
return multi:executeOnProcess(proxy.proc_name, function(proc, name, ID, ...)
local multi, thread = require("multi"):init()
local id = proc:pushJob(ID, name, ...) local id = proc:pushJob(ID, name, ...)
local link
local rets local rets
link = proc.jobqueue.OnJobCompleted(function(jid,...) local tjq = THREAD.get(proc.Name .. "_target_rtq_" .. ID):init()
if id==jid then
rets = {...}
end
end)
return thread.hold(function() return thread.hold(function()
if rets then local data = tjq:peek()
return multi.unpack(rets) or multi.NIL if data then
print(data)
end
if data and data[1] == id then
print("Got it sigh")
tjq:pop()
table.remove(data, 1)
return multi.unpack(data) or multi.NIL
end end
end) end)
-- proc.jobqueue.OnJobCompleted(function(jid, ...)
-- if id==jid then
-- rets = {...}
-- print("Got!")
-- end
-- end)
-- return thread.hold(function()
-- if rets then
-- return multi.unpack(rets) or multi.NIL
-- end
-- end)
end, name, ID, ...)
end, holup), name end, holup), name
end end
-- local qname = name .. "_tq_" .. THREAD_ID
-- local rqname = name .. "_rtq_" .. THREAD_ID
local function getQueue(name) multi.executeOnProcess = thread:newFunction(function(self, name, func, ...)
return THREAD.waitFor(name):init() local queue = THREAD.get(name .. "_local_proc")
local queueR = THREAD.get(name .. "_local_return")
if queue and queueR then
local multi, thread = require("multi"):init()
local id = multi.randomString(8)
queue = queue:init()
queueR = queueR:init()
queue:push({func, id, ...})
return thread.hold(function()
local data = queueR:peek()
if data and data[1] == id then
queueR:pop()
table.remove(data, 1)
return multi.unpack(data) or multi.NIL
end end
end)
else
return nil, "Unable to find a process queue with name: '" .. name .. "'"
end
end, true)
local jid = -1 local jid = -1
function multi:newSystemThreadedProcessor(cores) function multi:newSystemThreadedProcessor(cores)
@ -249,12 +283,16 @@ function multi:newSystemThreadedProcessor(cores)
c.OnObjectCreated = multi:newConnection() c.OnObjectCreated = multi:newConnection()
c.parent = self c.parent = self
c.jobqueue = multi:newSystemThreadedJobQueue(c.cores) c.jobqueue = multi:newSystemThreadedJobQueue(c.cores)
c.local_cmd = multi:newSystemThreadedQueue(name .. "_local_proc"):init()
c.local_cmd_return = multi:newSystemThreadedQueue(name .. "_local_return"):init()
c.jobqueue:registerFunction("STP_enable_targets",function(name) c.jobqueue:registerFunction("STP_enable_targets",function(name)
local multi, thread = require("multi"):init() local multi, thread = require("multi"):init()
local qname = name .. "_tq_" .. THREAD_ID local qname = name .. "_tq_" .. THREAD_ID
local rqname = name .. "_rtq_" .. THREAD_ID local rqname = name .. "_rtq_" .. THREAD_ID
local tjq = multi:newSystemThreadedQueue(qname):init() local tjq = multi:newSystemThreadedQueue(qname):init()
local trq = multi:newSystemThreadedQueue(rqname):init()
multi:newThread("TargetedJobHandler", function() multi:newThread("TargetedJobHandler", function()
local th local th
while true do while true do
@ -269,13 +307,13 @@ function multi:newSystemThreadedProcessor(cores)
local args = table.remove(dat, 1) local args = table.remove(dat, 1)
th.OnError(function(self,err) th.OnError(function(self,err)
-- We want to pass this to the other calling thread incase -- We want to pass this to the other calling thread incase
rqname:push{jid, err} trq:push{jid, err}
end) end)
rqname:push{jid, func(multi.unpack(args))} trq:push{jid, func(multi.unpack(args))}
end) end)
end end
end end
end).OnError(multi.error) end).OnError(print)
end) end)
c.jobqueue:registerFunction("STP_GetThreadCount",function() c.jobqueue:registerFunction("STP_GetThreadCount",function()
@ -287,7 +325,10 @@ function multi:newSystemThreadedProcessor(cores)
end) end)
function c:pushJob(ID, name, ...) function c:pushJob(ID, name, ...)
targets[ID]:push{name, jid, {...}} print("pushing")
local tq = THREAD.waitFor(self.Name .. "_target_tq_" .. ID):init()
--targets[ID]:push{name, jid, {...}}
tq:push{name, jid, {...}}
jid = jid - 1 jid = jid - 1
return jid + 1 return jid + 1
end end
@ -432,37 +473,59 @@ function multi:newSystemThreadedProcessor(cores)
return loads return loads
end, true) end, true)
local check = function()
return c.local_cmd:pop()
end
thread:newThread(function()
while true do
local data = thread.hold(check)
if data then
thread:newThread(function()
local func = table.remove(data, 1)
local id = table.remove(data, 1)
local ret = {id, func(c, multi.unpack(data))}
c.local_cmd_return:push(ret)
end).OnError(multi.error)
end
end
end).OnError(multi.error)
return c return c
end end
-- Modify thread.hold to handle proxies -- Modify thread.hold to handle proxies
local thread_ref = thread.hold local thread_ref = thread.hold
function thread.hold(n, opt) function thread.hold(n, opt)
--if type(n) == "table" then print(n.Type, n.isConnection()) end
if type(n) == "table" and n.Type == multi.PROXY and n.isConnection() then if type(n) == "table" and n.Type == multi.PROXY and n.isConnection() then
local ready = false local ready = false
local args local args
local id = n.getThreadID() local id = n.getThreadID()
local name = n:getUniqueName() local name = n:getUniqueName()
local func = multi:newTargetedFunction(id, references[n.Parent], "conn_"..multi.randomString(8), function(_name) print(id, name)
local func = multi:newTargetedFunction(id, n, "conn_"..multi.randomString(8), function(_name)
local multi, thread = require("multi"):init() local multi, thread = require("multi"):init()
local obj = _G[_name] local obj = _G[_name]
print("Start")
local rets = {thread.hold(obj)} local rets = {thread.hold(obj)}
print("Ring ;)")
for i,v in pairs(rets) do for i,v in pairs(rets) do
if v.Type then if v.Type then
rets[i] = {_self_ref_ = "parent"} rets[i] = {_self_ref_ = "parent"}
end end
end end
return multi.unpack(rets) return multi.unpack(rets)
end) end, true)
local conn local conn
local handle = func(name) local args = {func(name)}
conn = handle.OnReturn(function(...) -- conn = handle.OnReturn(function(...)
ready = true -- ready = true
args = {...} -- args = {...}
handle.OnReturn:Unconnect(conn) -- for i,v in pairs(args) do
end) -- print("DATA",i,v)
-- end
-- handle.OnReturn:Unconnect(conn)
-- end)
local ret = {thread_ref(function() local ret = {thread_ref(function()
if ready then if ready then
@ -471,7 +534,9 @@ function thread.hold(n, opt)
end, opt)} end, opt)}
for i,v in pairs(ret) do for i,v in pairs(ret) do
print("OBJECT",v.Type)
if type(v) == "table" and v._self_ref_ == "parent" then if type(v) == "table" and v._self_ref_ == "parent" then
print("assign")
ret[i] = n.Parent ret[i] = n.Parent
end end
end end