lua-users home
lua-l archive

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


Hi,

Derek Wong wrote:
> I'm new to the list and new to lua. I was wondering if someone might
> offer me some advice at implementing cooperative multitasking with
> lua scripts. I'm an amateur programmer so please excuse me if I've
> used the wrong terminology.
> 
> What I'd like is something like this...
> 
> C program runs along and starts a Lua script...
> Lua script does some stuff and then executes a pause() command
>  expecting the C program to resume at that point when it's ready...
> C program goes along doing other stuff and then at some point
>  along the way will check if there are any Lua scripts waiting
>  to be resumed, if there are it will resume their execution at
>  where they left off...

The term cooperative multitasking is OK *g*.  If you'll omit the
management functionality you could call it coroutines.
Unfortunately lua uses the C stack and allows lua to call C functions
which may further call lua again.  So, any plans to add this to
lua starts to add it to the C runtime system.

There is a portable cooperative multitasking library.  It was called
nps (iirc correctly it was later renamed).  Take a look at
http://freshmeat.net/ .  It's pretty complete in that it has wrappers
to most blocking system calls and a real scheduler.  But it incurs
some processing overhead.

A not very portable coroutine library for C is on
http://user.exit.de/froese/ .  This is the low level handling of
coroutines (co_create/call/resume/delete).  It's very simple but
fast.

The problem with both libs is that you have to know the size of the
C stack in advance.  Only one task/coroutine uses the usual growing
stack, the other ones get a fixed size one.

I already though about adding the coroutine lib to lua but never
got it done.

Ciao, ET.