made events specific to processes

there also needs pending events for the kernel just in case if we ever
need them, but now i'm not sure if it's required
This commit is contained in:
Ponali
2025-10-18 16:56:46 +02:00
parent 61eea92fe0
commit 883f20f269
2 changed files with 31 additions and 18 deletions
+11 -5
View File
@@ -10,7 +10,7 @@ local process
function module.init() function module.init()
_G.evmgr = {} _G.evmgr = {}
_G.evmgr.eventQueue = {} _G.evmgr.eventQueue = { kernel = {} }
local maxEventQueueLength = 10 -- increase if events start getting dropped local maxEventQueueLength = 10 -- increase if events start getting dropped
local computer = require("computer") local computer = require("computer")
@@ -31,11 +31,14 @@ function module.init()
_, process = _PUBLIC.tsched.addTask(function() _, process = _PUBLIC.tsched.addTask(function()
while true do while true do
-- check for events
local args local args
repeat repeat
args = { computer.uptime(), computer.pullSignal(0) } args = { computer.uptime(), computer.pullSignal(0) }
if args and args[2] then if args and args[2] then
table.insert(evmgr.eventQueue, args) for pid in pairs(evmgr.eventQueue) do
table.insert(evmgr.eventQueue[pid], args)
end
if _PUBLIC.keyboard then if _PUBLIC.keyboard then
if args[2] == "key_down" then if args[2] == "key_down" then
local keycode = args[5] local keycode = args[5]
@@ -64,13 +67,16 @@ function module.init()
end end
end end
end end
while #evmgr.eventQueue > maxEventQueueLength do for pid in pairs(evmgr.eventQueue) do
while #evmgr.eventQueue[pid] > maxEventQueueLength do
--ocelot.log("Queue length breach, removing first signal") --ocelot.log("Queue length breach, removing first signal")
table.remove(evmgr.eventQueue, 1) table.remove(evmgr.eventQueue[pid], 1)
end
end end
end end
until not args or not args[1] until not args or not args[1]
--ocelot.log("done") -- TODO: check for processes that have ended
-- run other tasks
coroutine.yield() coroutine.yield()
end end
end, "evmgr") end, "evmgr")
+13 -6
View File
@@ -5,7 +5,12 @@ local bufferTime = 0.1 -- A little bit of buffer time so events won't be skipped
--local ocelot = component.proxy(component.list("ocelot")()) --local ocelot = component.proxy(component.list("ocelot")())
function event.pull(...) function event.pull(...)
local args = {...} local pid = _PUBLIC.tsched and _PUBLIC.tsched.getCurrentTask() and _PUBLIC.tsched.getCurrentTask().id or "kernel"
if not evmgr.eventQueue[pid] then
evmgr.eventQueue[pid] = {}
end
local eventQueue = evmgr.eventQueue[pid]
local args = { ... }
local evtypes, timeout = {}, nil local evtypes, timeout = {}, nil
for _, arg in pairs(args) do for _, arg in pairs(args) do
@@ -20,23 +25,23 @@ function event.pull(...)
while true do while true do
-- Check event queue for matching event -- Check event queue for matching event
for i = 1, #evmgr.eventQueue do for i = 1, #eventQueue do
local foundevent = false local foundevent = false
if evtypes[1] then -- event type(s) specified if evtypes[1] then -- event type(s) specified
for _, evtype in pairs(evtypes) do for _, evtype in pairs(evtypes) do
if evmgr.eventQueue[i][2] == evtype and evmgr.eventQueue[i][1] >= startTime - bufferTime then if eventQueue[i][2] == evtype and eventQueue[i][1] >= startTime - bufferTime then
foundevent = true foundevent = true
end end
end end
else else
if evmgr.eventQueue[i][1] >= startTime - bufferTime then if eventQueue[i][1] >= startTime - bufferTime then
foundevent = true foundevent = true
end end
end end
if foundevent then if foundevent then
-- Found matching event (or any event if no type specified) -- Found matching event (or any event if no type specified)
local result = table.copy(evmgr.eventQueue[i]) local result = table.copy(eventQueue[i])
table.remove(evmgr.eventQueue, i) table.remove(eventQueue, i)
table.remove(result, 1) -- remove the time of event argument table.remove(result, 1) -- remove the time of event argument
return table.unpack(result) return table.unpack(result)
end end
@@ -50,6 +55,8 @@ function event.pull(...)
-- Yield to allow other processes to run and more events to be added -- Yield to allow other processes to run and more events to be added
if timeout and timeout > 0 then if timeout and timeout > 0 then
coroutine.yield() coroutine.yield()
elseif not timeout then
coroutine.yield()
end end
end end
end end