lua-users home
lua-l archive

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

Joseph Stewart wrote:
> Maybe this is a red herring to even bring up, but are Dunkel's protothreads
> a possible portable solution to some of lua's woes with coroutines?

The core of this are some cute C macros which hide a simple
event-id dispatcher using switch(). In particular a yield is really
a return. This means you cannot yield anywhere but from a single
function which must do _all_ event handling and dispatching.

Lua's standard coroutine implementation already solves that in a
more generic way using a similar mechanism. A return with a special
value signifies a yield to the core interpreter loop which makes it
return to its caller (i.e. back to lua_resume).

I wouldn't say that Lua has 'woes' with coroutines. It compares
rather favorably with most other languages in this regard. The key
problem is that you cannot easily suspend and resume the frames of
arbitrarily nested C functions. Most users notice this only when
they want to yield across pcall (which is implemented as a C
function in standard Lua).

You either have to design your code to work around this restriction
(standard Lua), or add special code to all C functions to make them
resumable (RVM), or switch between multiple C stacks (Coco), or
don't use C functions which call back into the interpreter loop to
implement pcall and metamethods (LuaJIT 2.x).

Each of these solutions has advantages and disadvantages. That's
why none of them has emerged as a clear winner yet. IMHO diversity
is not necessarily a bad choice for evolving a language ecosystem.