From 5e2ab9af3d6af261cd91430df7150bf93b6bb171 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Wed, 31 May 2023 21:51:47 -0400 Subject: [PATCH] Added an easy way to share a table, found some limitations with lanes threading. --- docs/changes.md | 69 +++++++++++++++++++++++---- init.lua | 13 ++--- integration/sharedExtensions/init.lua | 10 +++- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 3b45ee4..24bd809 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -74,6 +74,51 @@ Allows the user to have multi auto set priorities (Requires chronos). Also adds Added --- +- shared_table = STP:newSharedTable(tbl_name) -- Allows you to create a shared table that all system threads in a process have access to. Returns a reference to that table for use on the main thread. Sets `_G[tbl_name]` on the system threads so you can access it there. + ```lua + package.path = "?/init.lua;?.lua;"..package.path + + multi, thread = require("multi"):init({print=true}) + THREAD, GLOBAL = require("multi.integration.lanesManager"):init() + + stp = multi:newSystemThreadedProcessor(8) + + local shared = stp:newSharedTable("shared") + + shared["test"] = "We work!" + + for i=1,5 do + -- There is a bit of overhead when creating threads on a process. Takes some time, mainly because we are creating a proxy. + stp:newThread(function() + local multi, thread = require("multi"):init() + local shared = _G["shared"] + print(THREAD_NAME, shared.test, shared.test2) + multi:newAlarm(.5):OnRing(function() -- Play around with the time. System threads do not create instantly. They take quite a bit of time to get spawned. + print(THREAD_NAME, shared.test, shared.test2) + end) + end) + end + + shared["test2"] = "We work!!!" + + multi:mainloop() + ``` + + Output: + ``` + INFO: Integrated Lanes Threading! + STJQ_cPXT8GOx We work! nil + STJQ_hmzdYDVr We work! nil + STJQ_3lwMhnfX We work! nil + STJQ_hmzdYDVr We work! nil + STJQ_cPXT8GOx We work! nil + STJQ_cPXT8GOx We work! We work!!! + STJQ_hmzdYDVr We work! We work!!! + STJQ_3lwMhnfX We work! We work!!! + STJQ_hmzdYDVr We work! We work!!! + STJQ_cPXT8GOx We work! We work!!! + ``` + - STP:getLoad(type) -- returns a table where the index is the threadID and the value is the number of objects[type] running on that thread. `type`: "threads" for coroutines running or nil for all other objects running. - multi:newTargetedFunction(ID, proc, name, func, holup) -- This is used internally to handle thread.hold(proxy.conn) - proxy.getThreadID() -- Returns the threadID of the thread that the proxy is running in @@ -108,16 +153,22 @@ Added This event is subscribed to on the proxy threads side of things! Currently supporting: - - STP:newLoop(...) - - STP:newTLoop(...) - - STP:newUpdater(...) - - STP:newEvent(...) - - STP:newAlarm(...) - - STP:newStep(...) - - STP:newTStep(...) - - STP:newThread(...) - - STP:newFunction(...) + - proxyLoop = STP:newLoop(...) + - proxyTLoop = STP:newTLoop(...) + - proxyUpdater = STP:newUpdater(...) + - proxyEvent = STP:newEvent(...) + - proxyAlarm = STP:newAlarm(...) + - proxyStep = STP:newStep(...) + - proxyTStep = STP:newTStep(...) + - proxyThread = STP:newThread(...) + - threadedFunction = STP:newFunction(...) + Unique: + - STP:newSharedTable(name) + +
+ + **STP** functions (The ones above) cannot be called within coroutine based thread when using lanes. This causes thread.hold to break. Objects(proxies) returned by these functions are ok to use in coroutine based threads! ```lua package.path = "?/init.lua;?.lua;"..package.path diff --git a/init.lua b/init.lua index 6f9755f..2398697 100644 --- a/init.lua +++ b/init.lua @@ -312,7 +312,7 @@ function multi:newConnection(protect,func,kill) c.FC=0 function c:hasConnections() - return #call_funcs~=0 + return #fast~=0 end function c:Lock(conn) @@ -367,7 +367,7 @@ function multi:newConnection(protect,func,kill) end function c:getConnections() - return call_funcs + return fast end function c:getConnection(name, ignore) @@ -383,6 +383,7 @@ function multi:newConnection(protect,func,kill) end function c:fastMode() return self end + if kill then local kills = {} function c:Fire(...) @@ -457,14 +458,14 @@ function multi:newConnection(protect,func,kill) end function c:Bind(t) - local temp = call_funcs - call_funcs=t + local temp = fast + fast=t return temp end function c:Remove() - local temp = call_funcs - call_funcs={} + local temp = fast + fast={} return temp end diff --git a/integration/sharedExtensions/init.lua b/integration/sharedExtensions/init.lua index 893ebdc..bbd3691 100644 --- a/integration/sharedExtensions/init.lua +++ b/integration/sharedExtensions/init.lua @@ -353,6 +353,15 @@ function multi:newSystemThreadedProcessor(cores) return c.jobqueue:newFunction(func, holdme) end + function c:newSharedTable(name) + if not name then multi.error("You must provide a name when creating a table!") end + local tbl_name = "TABLE_"..multi.randomString(8) + c.jobqueue:doToAll(function(tbl_name, interaction) + _G[interaction] = THREAD.waitFor(tbl_name):init() + end, tbl_name, name) + return multi:newSystemThreadedTable(tbl_name):init() + end + function c.run() return self end @@ -373,7 +382,6 @@ function multi:newSystemThreadedProcessor(cores) return false end - -- Special functions c.getLoad = thread:newFunction(function(self, tp) local loads = {} local func