From 62e05788d2a3d3bba600b973b659b706d7affbdb Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Wed, 21 Sep 2022 00:06:48 -0400 Subject: [PATCH] Working on STC --- makeENV.lua | 10 ++-- multi/init.lua | 2 +- test.lua | 142 +++++++++++++++++++++++++++++++------------------ 3 files changed, 96 insertions(+), 58 deletions(-) diff --git a/makeENV.lua b/makeENV.lua index acb6fc0..d312349 100644 --- a/makeENV.lua +++ b/makeENV.lua @@ -1,9 +1,9 @@ commands = [[ -mkdir luajit && python -m hererocks -j 2.1.0-beta3 -r latest --patch --compat all ./luajit && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi -mkdir lua5.1 && python -m hererocks -l 5.1 -r latest --patch --compat all ./lua5.1 && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi -mkdir lua5.2 && python -m hererocks -l 5.2 -r latest --patch --compat all ./lua5.2 && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi -mkdir lua5.3 && python -m hererocks -l 5.3 -r latest --patch --compat all ./lua5.3 && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi -mkdir lua5.4 && python -m hererocks -l 5.4 -r latest --patch --compat all ./lua5.4 && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi +mkdir luajit && python -m hererocks -j 2.1.0-beta3 -r latest --patch --compat all ./luajit && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi && luarocks install lanes +mkdir lua5.1 && python -m hererocks -l 5.1 -r latest --patch --compat all ./lua5.1 && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi && luarocks install lanes +mkdir lua5.2 && python -m hererocks -l 5.2 -r latest --patch --compat all ./lua5.2 && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi && luarocks install lanes +mkdir lua5.3 && python -m hererocks -l 5.3 -r latest --patch --compat all ./lua5.3 && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi && luarocks install lanes +mkdir lua5.4 && python -m hererocks -l 5.4 -r latest --patch --compat all ./lua5.4 && set "PATH=G:\VSCWorkspace\multi\luajit\bin;%PATH%" && lua -v && luarocks install multi && luarocks install lanes ]] function string.split (inputstr, sep) local sep = sep or "\n" diff --git a/multi/init.lua b/multi/init.lua index e6caf78..63e3bbc 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -328,7 +328,7 @@ function multi:newConnection(protect,func,kill) conn_helper = func end - c.Connect=c.connect + c.connect=c.Connect c.GetConnection=c.getConnection c.HasConnections = c.hasConnections c.GetConnection = c.getConnection diff --git a/test.lua b/test.lua index 87c2e00..d7bcd66 100644 --- a/test.lua +++ b/test.lua @@ -1,15 +1,15 @@ -package.path = "./?/init.lua;?.lua;lua5.4/share/lua/?/init.lua;lua5.4/share/lua/?.lua;"..package.path -package.cpath = "lua5.4/lib/lua/?/core.dll;"..package.cpath +package.path = "./?/init.lua;?.lua;lua5.2/share/lua/5.2/?/init.lua;lua5.2/share/lua/5.2/?.lua;" +package.cpath = "lua5.2/lib/lua/5.2/?/core.dll;" multi, thread = require("multi"):init{print=true} GLOBAL, THREAD = require("multi.integration.lanesManager"):init() -function multi:newSystemThreadedConnection(name,...) +function multi:newSystemThreadedConnection(name) + local name = name or multi.randomString(16) local c = {} c.CONN = 0x00 c.TRIG = 0x01 c.PING = 0x02 c.PONG = 0x03 - c.proxy_conn = multi:newConnection(...) local function remove(a, b) local ai = {} local r = {} @@ -19,37 +19,14 @@ function multi:newSystemThreadedConnection(name,...) end return r end - local name = name or multi.randomString(16) c.Name = name - local connections = {} -- All triggers sent from main connection. When a connection is triggered on another thread, they speak to the main then send stuff out. - local funcs = {} - setmetatable(c,master_conn) -- A different approach will be taken for the non main connection objects - c.subscribe = multi:newSystemThreadedQueue("SUB_STC_"..name):init() -- Incoming subscriptions - multi:newThread("STC_SUB_MAN"..name,function() - local item - while true do - thread.yield() - -- We need to check on broken connections - c:Ping() -- Should return instantlly and process this in another thread - thread.hold(function() - item = c.subscribe:peek() - if item ~= nil and item[1] == c.CONN then - c.subscribe:pop() - connections[#connections+1] = item - multi.ForEach(funcs, function(link) -- Sync new connections - item:push{c.CONN, link} - end) - end - -- Nil return keeps this hold going until timeout - end,{cycles=multi.Priority_Normal}) - -- Usually a bunch of threads subscribe close to the same time. - -- Give those threads some time to ready up. - end - end) - c.Ping = thread:newFunction(function(self) - c.Ping:Pause() -- Don't allow this function to be called more than once + 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. + local ping + ping = thread:newFunction(function(self) + ping:Pause() -- Don't allow this function to be called until the first instance is done local pings = {} - multi.ForEach(funcs, function(link) -- Sync new connections + local count = #self.links + multi.ForEach(self.links, function(link) -- Sync new connections link:push{self.PING} end) thread.hold(function() @@ -57,36 +34,97 @@ function multi:newSystemThreadedConnection(name,...) if item ~= nil and item[1] == self.PONG then table.insert(pings,item[2]) end + if #pings==count then + return true -- If we get all pings give control back + end end,{sleep=3}) -- Give all threads time to respond to the ping -- We waited long enough for a response, anything that did not respond gets removed - remove(funcs, pings) - c.Ping:Resume() + self.links = remove(self.links, pings) + ping:Resume() end,false) + + thread:newThread("STC_SUB_MAN"..name,function() + local item + while true do + thread.yield() + -- We need to check on broken connections + ping() -- Should return instantlly and process this in another thread + thread.hold(function() + item = c.subscribe:peek() + if item ~= nil and item[1] == c.CONN then + c.subscribe:pop() + multi.ForEach(c.links, function(link) -- Sync new connections + item[2]:push{c.CONN, link} + end) + c.links[#c.links+1] = item + end + -- Nil return keeps this hold going until timeout + end,{cycles=multi.Priority_Normal}) + -- Usually a bunch of threads subscribe close to the same time. + end + end) + function c:Fire(...) - master_conn:Fire(...) - multi.ForEach(funcs, function(link) -- Sync new connections - link:push{self.TRIG,{...}} - end) - end - function c:Sync(link) - -- - end - function c:Connect(func) - local conn_func = func - self.proxy_conn(func) - proxy_conn(function() - funcs[#funcs+1] = func -- Used for syncing new connections to this connection later on - multi.ForEach(self.connections, function(link) - link:push{CONN, func} - end) + self.proxy_conn:Fire(...) + local args = {...} + multi.ForEach(self.links, function(link) -- Sync new connections + print(link[2]) -- Bugs everywhere + for i,v in pairs(link[2]) do print(i,v) end + link[2]:push{self.TRIG, args} end) end + function c:init() - self.proxy_conn + self.links = {} + self.proxy_conn = multi:newConnection() + setmetatable(self, {__index = self.proxy_conn}) + self.subscribe = multi:newSystemThreadedQueue("SUB_STC_"..self.Name):init() -- Incoming subscriptions + thread:newThread("STC_CONN_MAN"..name,function() + local item + local link_self_ref = multi:newSystemThreadedQueue() + self.subscribe:push{self.CONN,link_self_ref} + while true do + item = thread.hold(function() + return self.subscribe:peek() + end) + if item[1] == self.PING then + self.subscribe:push{self.PONG, link_self_ref} + elseif item[1] == self.CONN then + if item[2] ~= link_self_ref then + table.insert(self.links, item[2]) + end + elseif item[1] == self.TRIG then + self.proxy_conn:Fire(unpack(item[2])) + else + -- This shouldn't be the case + end + end + end) return self end + GLOBAL[name] = c + return c end +local conn = multi:newSystemThreadedConnection("conn"):init() +multi:newSystemThread("Test",function() + local multi, thread = require("multi"):init() + local conn = THREAD.waitFor("conn"):init() + conn(function() + print("Thread was triggered!") + end) + multi:mainloop() +end) +-- conn(function() +-- print("Mainloop conn got triggered!") +-- end) + +alarm = multi:newAlarm(1) +alarm:OnRing(function() + print("Ring") + conn:Fire() +end) + multi:mainloop() \ No newline at end of file