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
+48 -13
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,10 +13,11 @@ function module.init()
function _PUBLIC.ipc.shareWithAll() function _PUBLIC.ipc.shareWithAll()
local shareTable = {} local shareTable = {}
setmetatable(shareTable, {["__newindex"] = function(_, key, value) setmetatable(shareTable, {
["__newindex"] = function(_, key, value)
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
_G.ipc.shared[currentPID] = {} -- TODO: Add some kind of cleanup routine since these IPC shares can just keep piling up _G.ipc.shared[currentPID] = {}
end end
local globalTable local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do for _, tab in pairs(_G.ipc.shared[currentPID]) do
@@ -30,7 +33,8 @@ function module.init()
globalTable.vars = {} globalTable.vars = {}
end end
globalTable.vars[key] = value globalTable.vars[key] = value
end, ["__index"] = function(_, key) end,
["__index"] = function(_, key)
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
@@ -48,7 +52,8 @@ function module.init()
return nil return nil
end end
return globalTable.vars[key] return globalTable.vars[key]
end,["__pairs"]=function() end,
["__pairs"] = function()
if not _G.ipc.shared[currentPID] then if not _G.ipc.shared[currentPID] then
return pairs({}) return pairs({})
end end
@@ -66,17 +71,19 @@ function module.init()
end 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, {
["__newindex"] = function(_, key, value)
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
_G.ipc.shared[currentPID] = {} -- TODO: Add some kind of cleanup routine since these IPC shares can just keep piling up _G.ipc.shared[currentPID] = {}
end end
local globalTable local globalTable
for _, tab in pairs(_G.ipc.shared[currentPID]) do for _, tab in pairs(_G.ipc.shared[currentPID]) do
@@ -92,7 +99,8 @@ function module.init()
globalTable.vars = {} globalTable.vars = {}
end end
globalTable.vars[key] = value globalTable.vars[key] = value
end, ["__index"] = function(_, key) end,
["__index"] = function(_, key)
print(_G.ipc.shared) print(_G.ipc.shared)
local currentPID = _PUBLIC.tsched.getCurrentTask().id local currentPID = _PUBLIC.tsched.getCurrentTask().id
@@ -112,7 +120,8 @@ function module.init()
return nil return nil
end end
return globalTable.vars[key] return globalTable.vars[key]
end,["__pairs"]=function() end,
["__pairs"] = function()
if not _G.ipc.shared[currentPID] then if not _G.ipc.shared[currentPID] then
return pairs({}) return pairs({})
end end
@@ -130,7 +139,8 @@ function module.init()
end 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,7 +160,8 @@ function module.init()
end end
_PUBLIC.ipc.shared = {} _PUBLIC.ipc.shared = {}
setmetatable(_PUBLIC.ipc.shared, {["__index"] = function(_, pid) setmetatable(_PUBLIC.ipc.shared, {
["__index"] = function(_, pid)
local currentPID = _PUBLIC.tsched.getCurrentTask().id local currentPID = _PUBLIC.tsched.getCurrentTask().id
local returnTable = {} local returnTable = {}
for _, shareTable in pairs(ipc.shared[pid] or {}) do for _, shareTable in pairs(ipc.shared[pid] or {}) do
@@ -167,18 +178,42 @@ function module.init()
end end
end end
return returnTable return returnTable
end,["__pairs"]=function() end,
["__pairs"] = function()
local ftbl = {} local ftbl = {}
for i in pairs(_G.ipc.shared) do for i in pairs(_G.ipc.shared) do
ftbl[i] = _PUBLIC.ipc.shared[i] ftbl[i] = _PUBLIC.ipc.shared[i]
end end
return pairs(ftbl) return pairs(ftbl)
end}) 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, "ipc")
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