lua-users home
lua-l archive

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


Hi.

--- In lua-l@egroups.com, Edgar Toernig <froese@g...> wrote:
> Hi,
> 
> Just for your information.  When Lua 4.0 is out, I will make a
> version of Lua that will contain (beside other stuff) coroutines.

After checking out what coroutines are, it seems very similar to the 
approach that was suggested (and that i tried) using setjmp/longjmp. 
>From what i noticed, this approach doesn't really require any 
modifications to lua (one of the big advantages). But then again, 
since no modifications are requiered inside lua, why not just make a 
coroutine library and give some examples on how to use them with lua?
 
> Coroutines are all you need to implement a lightweight cooperative
> multitasking environment.  I'll not take the route to make a
> stackless Lua.  IMHO that would cripple Lua.  The tight coupling
> between C and Lua will be lost.  I'll take coroutines for C and
> export them to Lua.
> 

Well, making Lua totaly stackless is very difficult (if not 
impossible). But it's possible without too much effort to make the 
lua VM stackless. However you still can get in to recursive 
situations (Lua->C->Lua) and in thoes cases, you cannot interrupt a 
thread. This restricts the multitasking possible with this approach 
but in lots of case, this restriction is acceptable. Also, this 
solution is more cross-platform than the coroutine(or setjmp) 
approach. However, it implies modifications to lua which can be 
annoying. But i wouldn't claim it cripples Lua since it can function 
as it used to.

> setjmp/longjmp was mentioned to help with this and some people
> were asking how?  It's possible to implement coroutines with
> the combination of setjmp/longjump and sigaltstack.  But, it's
> not much easier to port than a version with about a dozen lines
> of assembler.

Out of curiosity, how would you do coroutines without setjmp/longjmp? 
Basicly copy the state of the CPU (IP, Registers, Stack Pointer,...) 
to a structure and restore it? If so, why not simply use 
setjmp/longjmp since it basicly does that. In my test implementation, 
the only line of assembly code required was to set the stack pointer 
to the coroutine(or thread) specific one.

The biggest problem with the coroutine (or setjmp) approach, is that 
you have to allocate a stakc per thread and have to find an 
apropriate size what will allow all threads to run without overflow 
yet be carefull not to overuse memory.

Sebastien St-Laurent
Software Engineer
sebby@z-axis.com