Posted 10 May 2013 - 01:08 PM
if you have any while true do yield loops and want to multitask them efficiently without the use of resource hogging coroutines this is for you!
here is an example usage to remove the pesky rednet coroutine
bios.lua:
routines={}
local oldyield=coroutine.yield
function coroutine.yield(filter)
while true do
local p={oldyield()}
for k,v in pairs(routines) do
local a,b=pcall(v,unpack(p))
if not a then
routines[k]=nil
error(b,2)
end
end
if p[1]==filter or p[1]=="terminate" or not filter then
return unpack(p)
end
end
end
function add(func,name)
routines[name or func]=func
return name or func
end
function remove(name)
for k,v in pairs(routines) do
if v==name then
routines[k]=nil
break
end
end
end
much simpler than the parallel APIhere is an example usage to remove the pesky rednet coroutine
bios.lua:
-- Run the shell
local ok, err = pcall(function()
eroutine.add(rednet.run,"rednet")
os.run({},"rom/programs/shell")
end)
rednet:
function run(sEvent, sSide, sChannel, sReplyChannel, sMessage, nDistance)
if sEvent == "modem_message" and isOpen( sSide ) and (sChannel == os.getComputerID() or sChannel == CHANNEL_BROADCAST) then
os.queueEvent( "rednet_message", sReplyChannel, sMessage, nDistance )
end
end