www.digitalmars.com         C & C++   DMDScript  

D - most portable coroutine implementation (in C)

reply nobody no.where writes:
"The most portable coroutine solution I have seen in C is at 

http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html 

and relies on the infamous "Duff's device" in order to do its magic of jumping
back into the middle of a C routine (it uses a switch statement as a sort of
computed goto). A good hack, and written in fully portable ANSI C."


(from:
http://nebuladevice.sourceforge.net/cgi-bin/twiki/view/Nebula/FiberMicrothread)
Jan 18 2004
next sibling parent reply Georg Wrede <Georg_member pathlink.com> writes:
If you weren't the same guy who posted about Stackless Python,
I would've jumped on the table.

Now, I have to ask you, what's your agenda?
Jan 18 2004
parent nobody no.where writes:
In article <buf189$1krd$1 digitaldaemon.com>, Georg Wrede says...
If you weren't the same guy who posted about Stackless Python,
I would've jumped on the table.

Now, I have to ask you, what's your agenda?

Collect all the info, and let Walter decide :-)
Jan 18 2004
prev sibling next sibling parent Roel Mathys <roel.mathys yucom.be> writes:
nice thingies,
played a bit in D with it (see code below).

btw in Python you do have the keyword "yield" - used for generators - 
which does stuff like this, there you can do something like this:

for x in array:
	doit(x)

after the last occurrence an exception is thrown (I believe),
this exception indicates that the iteration is finished.

bye,
roel

=============================================


char[uint] enumerate( char[] s )
{
     static uint state = 0;
     static uint index = 0;
     char[uint] ti;
     switch (state)
     {
     case 0:
         for ( ; index<s.length; ++index )
         {
             state = 1;
             ti[index] = s[index];
             return ti;
     case 1:
         }
         return ti;
     }
}

void main()
{
     char[] s = "hello";
     char[uint] ci;
     while ( cast(bool)(ci = enumerate(s)))
         printf( "%d -> %c\n", ci.keys[0],ci.values[0]);
}
Jan 20 2004
prev sibling parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
Looks like finally a legitimate use of the __LINE__ macro besides
cross-compilation and leak tracking.

This is why language support is so important for coroutines/fibers/whatever
you call them.

Sean

<nobody no.where> wrote in message news:buessj$1dg1$1 digitaldaemon.com...
 "The most portable coroutine solution I have seen in C is at

 http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

 and relies on the infamous "Duff's device" in order to do its magic of

 back into the middle of a C routine (it uses a switch statement as a sort

 computed goto). A good hack, and written in fully portable ANSI C."


 (from:

Jan 21 2004
parent reply Georg Wrede <Georg_member pathlink.com> writes:
In article <bule3p$2sjc$1 digitaldaemon.com>, Sean L. Palmer says...
This is why language support is so important for 
coroutines/fibers/whatever you call them.

<nobody no.where> wrote in message news:buessj$1dg1$1 digitaldaemon.com...
 "The most portable coroutine solution I have seen in C is at
 http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
 and relies on the infamous "Duff's device" in order to do 
 its magic of jumping back into the middle of a C routine (it 
 uses a switch statement as a sort of computed goto). A good hack, 
 and written in fully portable ANSI C."


Maybe we should try to figure out what the absolute minimum hacking by Walter would be, that enables us to implement help for this in D? I don't mean anything final and fancy, or perfect, just the minimum so that one can implement this particular solution in D with less brittle results and somewhat less coding. I may have some ideas, but they're still just embryonic.
Jan 21 2004
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
His iterator stuff, opApply, is getting really close.

Sean

"Georg Wrede" <Georg_member pathlink.com> wrote in message
news:bulluv$6tj$1 digitaldaemon.com...
 In article <bule3p$2sjc$1 digitaldaemon.com>, Sean L. Palmer says...
This is why language support is so important for
coroutines/fibers/whatever you call them.

Maybe we should try to figure out what the absolute minimum hacking by Walter would be, that enables us to implement help for this in D? I don't mean anything final and fancy, or perfect, just the minimum so that one can implement this particular solution in D with less brittle results and somewhat less coding. I may have some ideas, but they're still just embryonic.

Jan 22 2004