[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: First timer on cooperative multitasking
- From: "Thijs Schreijer" <thijs@...>
- Date: Sat, 27 Aug 2011 13:45:43 +0200
> > 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.
>
I'm looking at lua for the cross platform support mac, win, linux, etc. with
ultimately also embedded linux (wlan routers, nas devices, etc.) So I want
to stick to only cross platform stuff and hence choose luasockets.
> > 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.
That's clear. No problem, I intend to write the code myself, so that
shouldn't be an issue. Also I want it to use cooperative, as a learning
experience (I have had my share of race conditions, deadlocks and the like).
>
> 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.
And that's exactly my main question...
>
> -spc (If you like, I can provide what I have, although it's mostly
> undocumented ... )
Thx Sean for your kind (and elaborate) reply