[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Coroutines [was: snapshot of Lua 4.1 (work)]
- From: Jean-Claude Wippler <jcw@...>
- Date: Wed, 9 May 2001 20:43:17 -0700
Edgar Toernig <froese@gmx.de> wrote:
[coroutines]
>I'll definitely take a look. In Sol I already have a coroutine library
>based on longjmp. But it requires knowledge about the structure of the
>jmp_buf. I recently found the POSIX functions getcontext/makecontext/
>swapcontext. I'll change Sol's coro-lib to depend on these functions
>and add a prototype ucontext implementation for systems that do not have
>these functions [1]. I'll then port this lib to Lua 4.1.
Ouch. C-level co-routines? My first reaction was: why not simply
implement them at the Lua level? Oh, of course: Lua intermingles the C
call stack with its own... like Tcl and standard Python - and unlike
Stackless Python (which implements "calling" without nesting the C
stack). SP's trick is to have the VM push and pop call frames, instead
of going through a C function which nests the VM.
For Lua, this would require luaD_call to be split into two, and all
places where it gets called as well. Quite a bit of surgery, unfortunately.
That's a pity. If Lua were to avoid the C stack for ordinary function-
call nesting, then coroutines (at the Lua-level) would be easy to
implement - no special C code, and portable. Simply a closure which
remembers the new_thread pointer, and saving/restoring the current VM
instruction pointer on that stack for suspend/resume.
-jcw