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 = {}
module.dependencies = { "tsched" }
function module.check()
return true -- IPC should always be loaded
end
local checkProcess
function module.init()
_G.ipc = {}
_G.ipc.shared = {}
@@ -11,126 +13,134 @@ function module.init()
function _PUBLIC.ipc.shareWithAll()
local shareTable = {}
setmetatable(shareTable, {["__newindex"] = function(_, key, value)
local currentPID = _PUBLIC.tsched.getCurrentTask().id
if not _G.ipc.shared[currentPID] then
_G.ipc.shared[currentPID] = {} -- TODO: Add some kind of cleanup routine since these IPC shares can just keep piling up
end
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == "all" then
globalTable = tab
setmetatable(shareTable, {
["__newindex"] = function(_, key, value)
local currentPID = _PUBLIC.tsched.getCurrentTask().id
if not _G.ipc.shared[currentPID] then
_G.ipc.shared[currentPID] = {}
end
end
if not globalTable then
globalTable = {["sharedWith"] = "all"}
table.insert(_G.ipc.shared[currentPID], globalTable)
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
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
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
if not globalTable then
globalTable = { ["sharedWith"] = "all" }
table.insert(_G.ipc.shared[currentPID], globalTable)
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
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
if not globalTable then
return pairs({})
end
if not globalTable.vars then
return pairs({})
end
return pairs(table.copy(globalTable.vars))
end})
return pairs(table.copy(globalTable.vars))
end,
})
return shareTable
end
function _PUBLIC.ipc.shareWith(pid)
checkArg(1, pid, "number")
local shareTable = {}
setmetatable(shareTable, {["__newindex"] = function(_, key, value)
local currentPID = _PUBLIC.tsched.getCurrentTask().id
if not _G.ipc.shared[currentPID] then
_G.ipc.shared[currentPID] = {} -- TODO: Add some kind of cleanup routine since these IPC shares can just keep piling up
end
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == "all" then
globalTable = tab
setmetatable(shareTable, {
["__newindex"] = function(_, key, value)
local currentPID = _PUBLIC.tsched.getCurrentTask().id
if not _G.ipc.shared[currentPID] then
_G.ipc.shared[currentPID] = {}
end
end
if not globalTable then
globalTable = {["sharedWith"] = pid}
table.insert(_G.ipc.shared[currentPID], globalTable)
end
if not globalTable.vars then
globalTable.vars = {}
end
globalTable.vars[key] = value
end, ["__index"] = function(_, key)
print(_G.ipc.shared)
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == "all" then
globalTable = tab
end
end
if not globalTable then
globalTable = { ["sharedWith"] = pid }
table.insert(_G.ipc.shared[currentPID], globalTable)
end
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
if not _G.ipc.shared[currentPID] then
return nil
end
local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do
if tab.sharedWith == pid then
globalTable = tab
local currentPID = _PUBLIC.tsched.getCurrentTask().id
if not _G.ipc.shared[currentPID] then
return nil
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
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 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
if not globalTable then
return pairs({})
end
if not globalTable.vars then
return pairs({})
end
return pairs(table.copy(globalTable.vars))
end})
return pairs(table.copy(globalTable.vars))
end,
})
-- check if the reverse is also available
--[[ if not _G.ipc.shared[pid] then
@@ -150,35 +160,60 @@ function module.init()
end
_PUBLIC.ipc.shared = {}
setmetatable(_PUBLIC.ipc.shared, {["__index"] = function(_, pid)
local currentPID = _PUBLIC.tsched.getCurrentTask().id
local returnTable = {}
for _, shareTable in pairs(ipc.shared[pid] or {}) do
if shareTable.sharedWith == currentPID then
for key, value in pairs(shareTable.vars) do
returnTable[key] = table.copy(value)
end
elseif shareTable.sharedWith == "all" then
for key, value in pairs(shareTable.vars) do
if not returnTable[key] then
setmetatable(_PUBLIC.ipc.shared, {
["__index"] = function(_, pid)
local currentPID = _PUBLIC.tsched.getCurrentTask().id
local returnTable = {}
for _, shareTable in pairs(ipc.shared[pid] or {}) do
if shareTable.sharedWith == currentPID then
for key, value in pairs(shareTable.vars) do
returnTable[key] = table.copy(value)
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
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
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})
end, "ipc")
end
function module.exit()
_G.ipc = nil
_PUBLIC.ipc = nil
_PUBLIC.tsched.removeTask(checkProcess.id)
end
return module