|
|
@@ -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 |