56 lines
1.3 KiB
Lua
56 lines
1.3 KiB
Lua
local thd = {}
|
|
local next_pause = 0
|
|
local max_proctime = 5 -- 5s of process time max.
|
|
local last_proctime = 0
|
|
local lastsig = {}
|
|
local threads = {}
|
|
local function run_threads()
|
|
local rthd = {}
|
|
local mfd = {}
|
|
for i=1, #thd do
|
|
if (threads[i].dead) then
|
|
mfd[#mfd+1] = i
|
|
elseif (threads[i].deadline >= computer.uptime()) then
|
|
rthd[#rthd+1] = threads[i]
|
|
end
|
|
end
|
|
table.sort(rthd, function(a, b)
|
|
if (a.priority == b.priority) then
|
|
return a.deadline < b.deadline
|
|
end
|
|
return a.priority < b.priority
|
|
end)
|
|
local starttime = computer.uptime()
|
|
local mindl = math.huge
|
|
for i=1, #rthd do
|
|
local ok, dl = coroutine.resume(rthd[i].coro, table.unpack(rthd[i].args or lastsig))
|
|
if (not ok or coroutine.status(rthd[i].coro) == "dead") then
|
|
signal.push(thd.get(rthd[i].parent), {"subproc", "dead", rthd[i].pid, (rthd[i].exitcode or (dl and 1) or 0)})
|
|
for j=1, #thd do
|
|
if (threads[j] == rthd[i]) then
|
|
mfd[#mfd+1] = j
|
|
break
|
|
end
|
|
end
|
|
else
|
|
mindl = ((mindl > dl) and dl) or mindl
|
|
buffers.update(rthd[i])
|
|
if (rthd[i].sigpushed) then
|
|
mindl = 0
|
|
rthd[i].sigpushed = false
|
|
end
|
|
end
|
|
if (computer.uptime() >= starttime+max_proctime) then
|
|
goto cleanup
|
|
end
|
|
end
|
|
::cleanup::
|
|
for i=#mfd, 1, -1 do
|
|
table.remove(threads, mfd[i])
|
|
end
|
|
lastsig = table.pack(computer.pullSignal(mindl))
|
|
end
|
|
|
|
function thd.add(tinfo)
|
|
|
|
end |