lua-users home
lua-l archive

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

On Wed, 2005-01-12 at 23:06, Javier Guerra wrote:

> it's similar to what i tried before using threads; but i kept stumbling with 
> trying to read and write without blocking.  at the end, i decided to use 
> threads

There are several ways to solve this problem,
it isn't peculiar to Lua.

What you can do is replace the blocking operations
with a function that queues a message in an I/O request
queue, then you yield control to the scheduler.

You can do this in Lua by using a coroutine based scheduler.
One or more separate pre-emptive threads are used to service
the request queue -- either by doing blocking operations 
or some other method. When a request is satisfied the
scheduler is notified, and it puts the coroutine making
the request back in the schedule list, eventually scheduling
it and passing the I/O request result to it (and removing
the request from the I/O queue).

[This is basically what Felix does, except it has a dedicated
instruction for control inverting blocking reads]

Another way to do this is make the bytecode interpreter
do the scheduling, in that case you'd be switching between
Lua state objects not coroutines, and you would be
doing the scheduling in C rather than in Lua -- say
you execute one bytecode from each running thread in turn,
and occasionally check if some 'blocked' thread can
be put back in the 'running' list by inspecting
the I/O service queue.

Both Python and Ocaml use this technique. 

John Skaller,
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language