This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
PixelToast's profile picture

no more coroutines

Started by PixelToast, 10 May 2013 - 11:08 AM
PixelToast #1
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!

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 API
here 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
Lyqyd #2
Posted 10 May 2013 - 01:16 PM
Well, that's certainly a step in the wrong direction. Interesting idea, though.
PixelToast #3
Posted 10 May 2013 - 01:19 PM
how so?
i wanted to remove the rednet coroutine, and i dont like how parellel works