lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

The recent (hot) discussion about threads x coroutines x sockets was
certainly interesting but sometimes a bit cryptic for me.

During it duck made a question that I'd also like to know more about. What
would be the problems involved with a coroutine based server dispatcher?
Xavante has been on hold for quite a while simply because I haven't figured
out that one.

During a previous discussion over coroutines and pcall
(http://lua-users.org/lists/lua-l/2004-10/msg00130.html) Roberto posted a
draft of a coroutine based pcall
(http://lua-users.org/lists/lua-l/2004-10/msg00142.html) that seems to solve
some of the issues.

We were recently discussing this draft and it evolved to the attached
prototype of coroutine based pcalls and xpcalls for Lua 5.0. They have been
succesfull used on the current single-connection-at-a-time Xavante version
by simply making pcall = copcall and xpcall = coxpcall.


----------
local function pack (...) return arg end

function coxpcall(f, err)
  local co = coroutine.create(f)
  while true do
    local results = pack(coroutine.resume(co, unpack(arg)))
    local status = results[1]
    table.remove (results, 1) -- remove status of coroutine.resume
    if not status then
      return false, copcall(err(unpack(results)))
    end
    if coroutine.status(co) == "suspended" then
      arg = pack(coroutine.yield(unpack(results)))
    else
      return true, unpack(results)
    end
  end
end

function copcall(f, ...)
  return coxpcall(function() return f(unpack(arg)) end, error)
end
----------


I'd like to find out how much this approach would be useful/stable. Since
Xavante is targeted for embedded devices, performance is not a big issue but
stability is.

Thanks in advance for any hint, and btw both Mike's and Skaller's opinion
are equally welcome... :o)

Andre Carregal