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
+12 -6
View File
@@ -10,7 +10,7 @@ local process
function module.init()
_G.evmgr = {}
_G.evmgr.eventQueue = {}
_G.evmgr.eventQueue = { kernel = {} }
local maxEventQueueLength = 10 -- increase if events start getting dropped
local computer = require("computer")
@@ -31,11 +31,14 @@ function module.init()
_, process = _PUBLIC.tsched.addTask(function()
while true do
-- check for events
local args
repeat
args = { computer.uptime(), computer.pullSignal(0) }
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 args[2] == "key_down" then
local keycode = args[5]
@@ -64,13 +67,16 @@ function module.init()
end
end
end
while #evmgr.eventQueue > maxEventQueueLength do
--ocelot.log("Queue length breach, removing first signal")
table.remove(evmgr.eventQueue, 1)
for pid in pairs(evmgr.eventQueue) do
while #evmgr.eventQueue[pid] > maxEventQueueLength do
--ocelot.log("Queue length breach, removing first signal")
table.remove(evmgr.eventQueue[pid], 1)
end
end
end
until not args or not args[1]
--ocelot.log("done")
-- TODO: check for processes that have ended
-- run other tasks
coroutine.yield()
end
end, "evmgr")
+14 -7
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")())
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
for _, arg in pairs(args) do
@@ -20,23 +25,23 @@ function event.pull(...)
while true do
-- Check event queue for matching event
for i = 1, #evmgr.eventQueue do
for i = 1, #eventQueue do
local foundevent = false
if evtypes[1] then -- event type(s) specified
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
end
end
else
if evmgr.eventQueue[i][1] >= startTime - bufferTime then
if eventQueue[i][1] >= startTime - bufferTime then
foundevent = true
end
end
if foundevent then
-- Found matching event (or any event if no type specified)
local result = table.copy(evmgr.eventQueue[i])
table.remove(evmgr.eventQueue, i)
local result = table.copy(eventQueue[i])
table.remove(eventQueue, i)
table.remove(result, 1) -- remove the time of event argument
return table.unpack(result)
end
@@ -44,12 +49,14 @@ function event.pull(...)
-- Check if we've timed out
if timeout and computer.uptime() >= startTime + timeout then
return nil -- Timed out, return nil
return nil -- Timed out, return nil
end
-- Yield to allow other processes to run and more events to be added
if timeout and timeout > 0 then
coroutine.yield()
elseif not timeout then
coroutine.yield()
end
end
end