threads = {}
threads.active = {}

threads.debug = function(event, line)
  local s = debug.getinfo(2).short_src
  -- print(s .. ":" .. line)
end

threads.create = function(func, ...)
  local co = coroutine.create(func)
  debug.sethook(co, threads.debug, "l")
  table.insert(threads.active,co)
  -- DAs muss ... nicht {...} sein !!
  coroutine.resume(co, ...)
end

threads.sleep = function(delay)
  local yieldUntilTime = delay + Pacemaker.instance().frameTime
  while (yieldUntilTime > Pacemaker.instance().frameTime) do
    coroutine.yield()
  end
end

threads.dispatch = function()
  for i, co in ipairs(threads.active) do
    if (coroutine.status(co)  == "dead") then
      table.remove(threads.active,i)
    else
      local r = coroutine.resume(co)
    end
  end
end