added a cleanup routine to IPC

there's also lazyvim reformatting everything, like usual
This commit is contained in:
Ponali
2025-10-15 18:34:33 +02:00
parent a020229a69
commit d771a1fe39
+159 -124
View File
@@ -1,9 +1,11 @@
local module = {} local module = {}
module.dependencies = { "tsched" }
function module.check() function module.check()
return true -- IPC should always be loaded return true -- IPC should always be loaded
end end
local checkProcess
function module.init() function module.init()
_G.ipc = {} _G.ipc = {}
_G.ipc.shared = {} _G.ipc.shared = {}
@@ -11,126 +13,134 @@ function module.init()
function _PUBLIC.ipc.shareWithAll() function _PUBLIC.ipc.shareWithAll()
local shareTable = {} local shareTable = {}
setmetatable(shareTable, {["__newindex"] = function(_, key, value) setmetatable(shareTable, {
local currentPID = _PUBLIC.tsched.getCurrentTask().id ["__newindex"] = function(_, key, value)
if not _G.ipc.shared[currentPID] then local currentPID = _PUBLIC.tsched.getCurrentTask().id
_G.ipc.shared[currentPID] = {} -- TODO: Add some kind of cleanup routine since these IPC shares can just keep piling up if not _G.ipc.shared[currentPID] then
end _G.ipc.shared[currentPID] = {}
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == "all" then
globalTable = tab
end end
end local globalTable
if not globalTable then for _, tab in pairs(_G.ipc.shared[currentPID]) do
globalTable = {["sharedWith"] = "all"} if tab.sharedWith == "all" then
table.insert(_G.ipc.shared[currentPID], globalTable) globalTable = tab
end end
if not globalTable.vars then
globalTable.vars = {}
end
globalTable.vars[key] = value
end, ["__index"] = function(_, key)
local currentPID = _PUBLIC.tsched.getCurrentTask().id
if not _G.ipc.shared[currentPID] then
return nil
end
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == "all" then
globalTable = tab
end end
end if not globalTable then
if not globalTable then globalTable = { ["sharedWith"] = "all" }
return nil table.insert(_G.ipc.shared[currentPID], globalTable)
end end
if not globalTable.vars then if not globalTable.vars then
return nil globalTable.vars = {}
end end
return globalTable.vars[key] globalTable.vars[key] = value
end,["__pairs"]=function() end,
if not _G.ipc.shared[currentPID] then ["__index"] = function(_, key)
return pairs({}) local currentPID = _PUBLIC.tsched.getCurrentTask().id
end if not _G.ipc.shared[currentPID] then
local globalTable return nil
for _, tab in pairs(_G.ipc.shared[currentPID]) do end
if tab.sharedWith == pid then local globalTable
globalTable = tab for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == "all" then
globalTable = tab
end
end
if not globalTable then
return nil
end
if not globalTable.vars then
return nil
end
return globalTable.vars[key]
end,
["__pairs"] = function()
if not _G.ipc.shared[currentPID] then
return pairs({})
end
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == pid then
globalTable = tab
end
end
if not globalTable then
return pairs({})
end
if not globalTable.vars then
return pairs({})
end end
end
if not globalTable then
return pairs({})
end
if not globalTable.vars then
return pairs({})
end
return pairs(table.copy(globalTable.vars)) return pairs(table.copy(globalTable.vars))
end}) end,
})
return shareTable return shareTable
end end
function _PUBLIC.ipc.shareWith(pid) function _PUBLIC.ipc.shareWith(pid)
checkArg(1, pid, "number") checkArg(1, pid, "number")
local shareTable = {} local shareTable = {}
setmetatable(shareTable, {["__newindex"] = function(_, key, value) setmetatable(shareTable, {
local currentPID = _PUBLIC.tsched.getCurrentTask().id ["__newindex"] = function(_, key, value)
if not _G.ipc.shared[currentPID] then local currentPID = _PUBLIC.tsched.getCurrentTask().id
_G.ipc.shared[currentPID] = {} -- TODO: Add some kind of cleanup routine since these IPC shares can just keep piling up if not _G.ipc.shared[currentPID] then
end _G.ipc.shared[currentPID] = {}
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == "all" then
globalTable = tab
end end
end local globalTable
if not globalTable then for _, tab in pairs(_G.ipc.shared[currentPID]) do
globalTable = {["sharedWith"] = pid} if tab.sharedWith == "all" then
table.insert(_G.ipc.shared[currentPID], globalTable) globalTable = tab
end end
if not globalTable.vars then end
globalTable.vars = {} if not globalTable then
end globalTable = { ["sharedWith"] = pid }
globalTable.vars[key] = value table.insert(_G.ipc.shared[currentPID], globalTable)
end, ["__index"] = function(_, key) end
print(_G.ipc.shared) if not globalTable.vars then
globalTable.vars = {}
end
globalTable.vars[key] = value
end,
["__index"] = function(_, key)
print(_G.ipc.shared)
local currentPID = _PUBLIC.tsched.getCurrentTask().id local currentPID = _PUBLIC.tsched.getCurrentTask().id
if not _G.ipc.shared[currentPID] then if not _G.ipc.shared[currentPID] then
return nil return nil
end
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == pid then
globalTable = tab
end end
end local globalTable
if not globalTable then for _, tab in pairs(_G.ipc.shared[currentPID]) do
return nil if tab.sharedWith == pid then
end globalTable = tab
if not globalTable.vars then end
return nil end
end if not globalTable then
return globalTable.vars[key] return nil
end,["__pairs"]=function() end
if not _G.ipc.shared[currentPID] then if not globalTable.vars then
return pairs({}) return nil
end end
local globalTable return globalTable.vars[key]
for _, tab in pairs(_G.ipc.shared[currentPID]) do end,
if tab.sharedWith == pid then ["__pairs"] = function()
globalTable = tab if not _G.ipc.shared[currentPID] then
return pairs({})
end
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == pid then
globalTable = tab
end
end
if not globalTable then
return pairs({})
end
if not globalTable.vars then
return pairs({})
end end
end
if not globalTable then
return pairs({})
end
if not globalTable.vars then
return pairs({})
end
return pairs(table.copy(globalTable.vars)) return pairs(table.copy(globalTable.vars))
end}) end,
})
-- check if the reverse is also available -- check if the reverse is also available
--[[ if not _G.ipc.shared[pid] then --[[ if not _G.ipc.shared[pid] then
@@ -150,35 +160,60 @@ function module.init()
end end
_PUBLIC.ipc.shared = {} _PUBLIC.ipc.shared = {}
setmetatable(_PUBLIC.ipc.shared, {["__index"] = function(_, pid) setmetatable(_PUBLIC.ipc.shared, {
local currentPID = _PUBLIC.tsched.getCurrentTask().id ["__index"] = function(_, pid)
local returnTable = {} local currentPID = _PUBLIC.tsched.getCurrentTask().id
for _, shareTable in pairs(ipc.shared[pid] or {}) do local returnTable = {}
if shareTable.sharedWith == currentPID then for _, shareTable in pairs(ipc.shared[pid] or {}) do
for key, value in pairs(shareTable.vars) do if shareTable.sharedWith == currentPID then
returnTable[key] = table.copy(value) for key, value in pairs(shareTable.vars) do
end
elseif shareTable.sharedWith == "all" then
for key, value in pairs(shareTable.vars) do
if not returnTable[key] then
returnTable[key] = table.copy(value) returnTable[key] = table.copy(value)
end end
elseif shareTable.sharedWith == "all" then
for key, value in pairs(shareTable.vars) do
if not returnTable[key] then
returnTable[key] = table.copy(value)
end
end
end end
end end
return returnTable
end,
["__pairs"] = function()
local ftbl = {}
for i in pairs(_G.ipc.shared) do
ftbl[i] = _PUBLIC.ipc.shared[i]
end
return pairs(ftbl)
end,
})
_, checkProcess = _PUBLIC.tsched.addTask(function()
while true do
-- get all PIDs that exists
local tasks = _PUBLIC.tsched.getTasks()
local pids = {}
for _, v in pairs(tasks) do
table.insert(pids, v.id)
end
-- get all shares from unexistant processes and delete them
for i in pairs(_G.ipc.shared) do
if not table.find(pids, i) then
_G.ipc.shared[i] = nil
end
end
-- let the other processes run
coroutine.yield()
end end
return returnTable end, "ipc")
end,["__pairs"]=function()
local ftbl = {}
for i in pairs(_G.ipc.shared) do
ftbl[i]=_PUBLIC.ipc.shared[i]
end
return pairs(ftbl)
end})
end end
function module.exit() function module.exit()
_G.ipc = nil _G.ipc = nil
_PUBLIC.ipc = nil _PUBLIC.ipc = nil
_PUBLIC.tsched.removeTask(checkProcess.id)
end end
return module return module