[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to swap coroutine ?
- From: Tony Finch <dot@...>
- Date: Mon, 12 Sep 2011 18:59:57 +0100
Gaspard Bucher <gaspard@teti.ch> wrote:
>
> I would like to create a coroutine and then continue execution in the
> coroutine so that the original calling code can yield.
You need to create the coroutine, tell your scheduler about it, then
yield, and allow the scheduler to start the new coroutine.
There is a stack of "normal" coroutines with the currently "running"
coroutine at the top. (Terminology is according to coro.status.) Calling
coro.resume pushes a coroutine onto the stack, switching the caller from
"running" to "normal" and the callee from "suspended" to "running".
Calling coro.yield pops the top coroutine, switching it from "running" to
"suspended".
There aren't any functions that allow you to manipulate coroutines other
than in this stack-like manner, so if you want to re-arrange the stack you
need to code it explicitly. This generally means you need a scheduler
running in the deepest coroutine in the stack, and some utility functions
that the other coroutines call to co-operate with the scheduler.
Tony.
--
f.anthony.n.finch <dot@dotat.at> http://dotat.at/
Viking, North Utsire: Cyclonic, becoming westerly or northwesterly 5 to 7,
occasionally gale 8. Moderate or rough, becoming rough or very rough. Rain.
Good, occasionally poor.