From 36a73b892a4719291c0b0ea48bb6313c1871d3b7 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Mon, 31 Jul 2023 08:11:24 +1000 Subject: [PATCH] implement component.get for convenience --- build.lua | 4 ++++ cfg/rc.cfg | 2 +- module/component-get.lua | 8 ++++++++ module/init.lua | 1 + module/io.lua | 5 +++-- module/sched.lua | 21 ++++++++++++--------- module/syslog.lua | 1 + service/fsmanager.lua | 8 +++++--- 8 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 module/component-get.lua diff --git a/build.lua b/build.lua index 39298ff..40cdcb9 100644 --- a/build.lua +++ b/build.lua @@ -1,3 +1,7 @@ local preproc = require "preproc" --local tA = {...} +function preproc.directives.includelib(file,name) + return string.format("package.loaded.%s = (function()\n%s\nend)()", name, preproc.preproc(file)) +end + preproc(...) diff --git a/cfg/rc.cfg b/cfg/rc.cfg index 32b46b0..cd750b9 100644 --- a/cfg/rc.cfg +++ b/cfg/rc.cfg @@ -1 +1 @@ -{enabled={"getty","minitel"}} +{enabled={"getty","minitel","fsmanager","rtfsman","partman"}} diff --git a/module/component-get.lua b/module/component-get.lua new file mode 100644 index 0000000..eaef0f4 --- /dev/null +++ b/module/component-get.lua @@ -0,0 +1,8 @@ +function component.get(addr, ctype) + for c in component.list(ctype, true) do + if c:sub(1, addr:len()) == addr then + return c + end + end + return nil, "no such component" +end diff --git a/module/init.lua b/module/init.lua index cdda963..ba38c46 100644 --- a/module/init.lua +++ b/module/init.lua @@ -6,6 +6,7 @@ --#include "module/io.lua" --#include "module/devfs.lua" --#include "module/devfs/syslog.lua" +--#include "module/component-get.lua" --#include "module/loadfile.lua" _OSVERSION=_OSVERSION or "PsychOS 2" diff --git a/module/io.lua b/module/io.lua index ed1a57c..0592c3e 100644 --- a/module/io.lua +++ b/module/io.lua @@ -32,8 +32,9 @@ function io.write(...) -- Writes its arguments to the default output stream. io.output():write(...) end -function print(...) -- Writes each argument to the default output stream, separated by newlines. +function print(...) -- Writes each argument to the default output stream, separated by space. for k,v in ipairs({...}) do - io.write(tostring(v).."\n") + io.write(tostring(v).." ") end + io.write("\n") end diff --git a/module/sched.lua b/module/sched.lua index bcdb9ec..0e16936 100644 --- a/module/sched.lua +++ b/module/sched.lua @@ -1,5 +1,5 @@ do -local tTasks,nPid,nTimeout,cPid = {},1,0.25,0 -- table of tasks, next process ID, event timeout, current PID +local tTasks,nPid,nTimeout,cPid = {},1,0.25,0 -- table of tasks, next process ID, default event timeout, current PID function os.spawn(f,n) -- function string -- number -- creates a process from function *f* with name *n* tTasks[nPid] = { c=coroutine.create(function() @@ -14,6 +14,7 @@ function os.spawn(f,n) -- function string -- number -- creates a process from fu P=cPid, -- parent PID t=0, -- CPU time T=0, -- total uptime + E=nTimeout, -- event timeout e={} -- environment variables } if tTasks[cPid] then @@ -44,18 +45,22 @@ function os.taskInfo(pid) -- number -- table -- returns info on process *pid* as end function os.sched() -- the actual scheduler function os.sched = nil + local sTimeout = nTimeout while #tTasks > 0 do - local tEv = {computer.pullSignal(nTimeout)} + local tEv = {computer.pullSignal(sTimeout)} + sTimeout = nTimeout for k,v in pairs(tTasks) do if coroutine.status(v.c) ~= "dead" then cPid = k local sT, sC = os.clock(), computer.uptime() coroutine.resume(v.c,table.unpack(tEv)) v.t, v.T = v.t + os.clock() - sT, v.T + computer.uptime() - sC + sTimeout=math.min(sTimeout, v.E) else tTasks[k] = nil end end + sTimeout = nTimeout end end function os.setenv(k,v) -- set's the current process' environment variable *k* to *v*, which is passed to children @@ -69,13 +74,11 @@ function os.getenv(k) -- gets a process' *k* environment variable end end function os.getTimeout() - return nTimeout + return tTasks[cPid].E end -function os.setTimeout(n) - if type(n) == "number" and n >= 0 then - nTimeout = n - return true - end - return false +function os.setTimeout(n,pid) + assert(type(n) == "number" and n >= 0) + tTasks[pid or cPid].E = n + return true end end diff --git a/module/syslog.lua b/module/syslog.lua index f28e7ec..e80549d 100644 --- a/module/syslog.lua +++ b/module/syslog.lua @@ -1,3 +1,4 @@ +--#include "module/ocelot-debug.lua" do syslog = {} syslog.emergency = 0 diff --git a/service/fsmanager.lua b/service/fsmanager.lua index a2f906e..b5385bb 100644 --- a/service/fsmanager.lua +++ b/service/fsmanager.lua @@ -8,16 +8,17 @@ local function mount(addr) local w,r = fs.mount(dest,component.proxy(addr)) if not w then syslog("Failed to mount: "..r) + return false end fsmanager.filesystems[addr] = dest end -for addr, _ in component.list("filesystem") do - mount(addr) -end function fsmanager.start() run = true return os.spawn(function() + for addr, _ in component.list("filesystem") do + mount(addr) + end while run do local tE = {coroutine.yield()} if tE[1] == "component_added" and tE[3] == "filesystem" then @@ -25,6 +26,7 @@ function fsmanager.start() elseif tE[1] == "component_removed" and fsmanager.filesystems[tE[2]] and tE[3] == "filesystem" then syslog("Unmounting "..tE[2].." from "..fsmanager.filesystems[tE[2]]) fs.umount(fsmanager.filesystems[tE[2]]) + fsmanager.filesystems[tE[2]] = nil end end end,"fsmanager")