Ignore:
Timestamp:
Mar 27, 2019, 9:07:47 AM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
arm-eh, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation
Children:
86fb8f2
Parents:
0087e0e
Message:

start rewrite of coroutine section

Location:
doc/papers/concurrency/examples
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/examples/Pingpong.cfa

    r0087e0e r1e5d0f0c  
    44coroutine PingPong {
    55        const char * name;
    6         /* const */ unsigned int N;
    7         PingPong * part;
     6        unsigned int N;
     7        PingPong & part;
    88};
    99
    1010void ?{}( PingPong & this, const char * name, unsigned int N, PingPong & part ) {
    11         (this.__cor){name};
    12         this.name = name;
    13         this.N = N;
    14         this.part = &part;
     11        this.[name, N] = [name, N];  &this.part = &part;
    1512}
    1613void ?{}( PingPong & this, const char * name, unsigned int N ) {
    17         this{ name, N, *(PingPong *)0 };
     14        this{ name, N, *0p };                                                           // call first constructor
    1815}
    1916void cycle( PingPong & pingpong ) {
     
    2118}
    2219void partner( PingPong & this, PingPong & part ) {
    23         this.part = &part;
     20        &this.part = &part;
    2421        resume( this );
    2522}
    26 void main( PingPong & pingpong ) {                                              // ping's starter ::main, pong's starter ping
    27         for ( pingpong.N ) {                                                            // N ping-pongs
    28                 sout | pingpong.name;
    29                 cycle( *pingpong.part );
     23void main( PingPong & pingpong ) with(pingpong) {               // ping's starter ::main, pong's starter ping
     24        for ( N ) {                                                                                     // N ping-pongs
     25                sout | name;
     26                cycle( part );
    3027        } // for
    3128}
    3229int main() {
    33         enum { N = 20 };
     30        enum { N = 5 };
    3431        PingPong ping = { "ping", N }, pong = { "pong", N, ping };
    3532        partner( ping, pong );
     
    3835// Local Variables: //
    3936// tab-width: 4 //
    40 // compile-command: "cfa pingpong.cfa" //
     37// compile-command: "cfa Pingpong.cfa" //
    4138// End: //
  • doc/papers/concurrency/examples/Pingpong.py

    r0087e0e r1e5d0f0c  
    1 def Scheduler
     1def PingPong( name, N ):
     2        partner = (yield)           # get partner
     3        yield                       # resume scheduler
     4        for i in range( N ):
     5                print( name )
     6                yield partner           # execute next
     7        print( "end", name )
     8
     9def Scheduler():
     10        n = (yield)                 # starting coroutine
     11        while True:
     12                n = next( n )           # schedule coroutine
     13
     14pi = PingPong( "ping", 5 )
     15po = PingPong( "pong", 5 )
     16next( pi )                      # prime
     17pi.send( po )                   # send partner
     18next( po )                      # prime
     19po.send( pi )                   # send partner
     20
     21s = Scheduler();
     22next( s )                       # prime
    223try:
    3         yield from ping();
    4         yield from pong();
     24        s.send( pi )                            # start cycle
    525except StopIteration:
    6         print( "Scheduler stop" )
    7 
    8 
    9 def pong():
    10         print( "pong" )
    11 for i in range( 10 ):
    12 
    13                 yield from ping()
    14         print( "stop pong" )
    15 
    16 def ping():
    17         global i
    18         print( "ping" )
    19         i += 1
    20         if i < 5:
    21                 yield from pong()
    22         print( "stop ping" )
    23 
    24 p = ping()
    25 try:
    26         next( p )
    27 except StopIteration:
    28         print( "stop" )
     26        print( "scheduler stop" )
     27print( "stop" )
    2928
    3029# Local Variables: #
    3130# tab-width: 4 #
    32 # compile-command: "python3.5 pingpong.py" #
     31# compile-command: "python3.5 Pingpong.py" #
    3332# End: #
  • doc/papers/concurrency/examples/ProdCons.cfa

    r0087e0e r1e5d0f0c  
    2323        sout | "prod stops";
    2424}
    25 int payment( Prod & prod, int money ) {
    26         prod.money = money;
     25int payment( Prod & prod, int m ) with(prod) {
     26        money = m;
    2727        resume( prod );                                                                         // main 1st time, then
    28         return prod.receipt;                                                            // prod in delivery
     28        return receipt;                                                                         // prod in delivery
    2929}
    3030void start( Prod & prod, int N, Cons &c ) {
Note: See TracChangeset for help on using the changeset viewer.