lua-users home
lua-l archive

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


It was thus said that the Great Thijs Schreijer once stated:
> Hi all,
>
> I've been using lua for some time as an embedded scripting language. The
> implementation was a pre-emptive multitasking environment. I now have the
> intention to do some lua-only developments and have been looking into the
> cooperative multitasking model.
> 
> The basics are very clear, but there is one part that I feel uncomfortable
> with; How often and when would I have to yield? Are there any guidelines on
> that?

  I implemented a cooperative scheduler in C/Lua based around a homebrewed
network API.  Basically, when ever I read from a socket, I would yield, and
possibly with socket writes as well (for instance, I wanted to write 8k, but
only 1k was accepted, yield until I can write to that socket again).  I
didn't bother with trying to ensure arbitrary code would yield properly as I
was the only one writing the code.

  The C code was event driven (using epoll() under Linux).  The Lua code is
nice---for instance, an echo service:

	-- When a TCP connection is accepted, a Lua coroutine is created and
	-- a function called "main()" in the script is called.  Any calls 
	-- through the "socket" object can block, causing another Lua
	-- "thread" to run.

	function main(socket)
	  while true do
	    socket:write(socket:read("*a"))
	  end
	end

  The C code ... isn't quite so straight forward.  

> In general my understanding is that not yielding for too long, will make
> other threads unresponsive, that's obvious. But I intent do use luasockets
> and maybe some RS232 serial stuff, so that's where my concerns are; loosing
> incoming data on those IO channels because another thread doesn't yield in
> time.

  It's up to the programmer writing the scripts to properly yield---if you
want to enfore this (and don't trust the programmer), you need preemption,
period.

  But, like I said, decent places to call yield() behind the scenes include
places that normally block, like network or serial communication.

> The google mainly returned long debates on preemptive vs cooperative, but no
> info on how to make it work best.

  Cooperative multitasking is actually very simple---just yield to another
thread.  When to do that, is the not-so-easy part.

  -spc (If you like, I can provide what I have, although it's mostly
	undocumented ... )