On 2016-09-24 17:49, Nick Sabalausky wrote:

"Protothreads"! I've been trying to remember the name of that lib and
couldn't find it. I used that the last time I was working on a C/C++
thing, and it was very helpful.

AIUI though, that IS stackless coroutines, same thing AFAIK. The
Protothreads lib refers to itself as "extremely lightweight stackless
threads". And the way it's used is definitely what I'd consider a
coroutine.

I find it quite difficult to find correct definitions with many of these terms like: coroutine, fiber, stackless thread, resumeable function. Because many languages use the same name of different things and they use different names for the same things.

Protothreads solves the problem of saving/restoring state by doing two
things:

1. You put all your locals (at least, any that need preserved across
save/restore) in a struct, together with the appropriate jump label to
be jumped to upon resume. Then when yielding, this struct gets passed
out, and when resuming you pass it back in.

Ah, right. Previously when I looked at this I only saw examples which stored the jump label. That might be possible to automatically rewrite in a AST macro.

Would it be possible to store the state in a TLS variable inside the function to avoid having to pass in the state?

2. Compared to C, D has much stricter rules about where a switch's
"case" labels can do. Ie, they can't really cross scope boundaries. So
*all* the coroutine's "yield" statements would have to be within the
same scope *and* at the same nesting-level (or at the very least,
there'd be some big annoying restrictions). Otherwise, when it gets
converted to a "case:", the resulting code would be invalid.

Do you have any examples?

--
/Jacob Carlborg

Reply via email to