1
0
mirror of https://github.com/Adorable-Catgirl/Zorya-NEO.git synced 2024-11-01 08:10:57 +11:00
Zorya-NEO/ksrc/libs/thd.lua

94 lines
1.9 KiB
Lua
Raw Normal View History

2020-01-09 15:01:35 +11:00
local thd = {}
local threads = {}
local idx = 1
local computer = computer
local unpack = unpack or table.unpack
local coroutine = coroutine
local c_create = coroutine.create
local c_yield = coroutine.yield
local c_resume = coroutine.resume
local c_status = coroutine.status
function thd.add(name, func)
local t = assert(c_create(func))
--component.proxy(component.list("sandbox")()).debug_log(name, t, func)
threads[#threads+1] = {name, t, {}, 0, ".+"}
threads[t] = threads[#threads]
end
local sigs = {}
local ps = computer.pullSignal
function thd.autosleep()
local msleep = math.huge
for i=1, #threads do
if (threads[i][4] and threads[i][4] < msleep) then
msleep = threads[i][4]
end
end
local rsleep = msleep-computer.uptime()
if (rsleep < 0 or #sigs > 0) then
rsleep = 0
end
local sig = {ps(rsleep)}
if (#sigs > 0) then
if (#sig > 0) then
sigs[#sigs+1] = sig
end
sig = sigs[1]
table.remove(sigs, 1)
end
return sig
end
local last_sig = {}
function thd.run()
last_sig = thd.autosleep()
for i=1, #threads do
if (threads[i][4] <= computer.uptime() or #last_sig > 0) then
if (c_status(threads[i][2]) ~= "running") then
2020-03-13 08:01:05 +11:00
local dt = computer.uptime()
2020-01-09 15:01:35 +11:00
local er, dl = c_resume(threads[i][2], unpack(last_sig))
if (not er) then error(threads[i][1]..": "..dl) end
2020-01-15 12:07:27 +11:00
if (dl == "k") then
threads[i][6] = true
end
2020-01-09 15:01:35 +11:00
dl = computer.uptime() + (dl or math.huge)
threads[i][4] = dl
2020-03-13 08:01:05 +11:00
threads[i].delta = computer.uptime() - dt
2020-05-17 14:23:43 +10:00
--sigs[#sigs+1] = {ps(0)}
2020-01-09 15:01:35 +11:00
end
end
end
local t = {}
2020-01-15 12:07:27 +11:00
for i=1, #threads do
local v = threads[i]
2020-01-09 15:01:35 +11:00
if (c_status(v[2]) ~= "dead" and not v[6]) then
2020-01-15 12:07:27 +11:00
t[#t+1] = v
t[v[2]] = v
2020-01-09 15:01:35 +11:00
end
end
threads = t
return #threads > 0
end
function thd.kill(i)
threads[i][6] = true
end
function thd.sched_end()
return #threads == idx
end
function thd.get_threads()
return threads
end
function computer.pullSignal(t)
return c_yield(t)
end
return thd