Ignore:
Timestamp:
Mar 27, 2019, 11:09:23 AM (6 years ago)
Author:
tdelisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
a45fc7b
Parents:
2b10f95 (diff), 1e5d0f0c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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

Legend:

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

    r2b10f95 r86fb8f2  
    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

    r2b10f95 r86fb8f2  
    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

    r2b10f95 r86fb8f2  
    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 ) {
  • doc/papers/concurrency/examples/ProdCons.cpp

    r2b10f95 r86fb8f2  
    1212struct Cons;
    1313
    14 struct Prod {
     14struct resumable {
     15        virtual resumable * resume() = 0;
     16};
     17
     18struct Prod : public resumable {
    1519        struct local {
    1620                Cons * c;
     
    2024        struct promise_type {
    2125                local _l;
     26                resumable * next;
    2227
    2328                Prod get_return_object() {
     
    6974        static Prod main();
    7075
    71         auto payment(int money) {
    72                 _coroutine.promise()._l.money = money;
    73                 struct ret {
    74                         int _receipt;
    75                         bool await_ready() { return false; }
    76                         void await_suspend(std::experimental::coroutine_handle<>) {}
    77                         int await_resume() { return _receipt; }
    78                 };
    79                 return ret{ _coroutine.promise()._l.receipt };
    80         }
     76        struct payment_return;
     77
     78        payment_return payment(int money);
    8179
    8280        auto start(int N, Cons & c) {
     
    8482                _coroutine.promise()._l.N = N;
    8583                _coroutine.promise()._l.receipt = 0;
     84        }
     85
     86        virtual resumable * resume() override final {
    8687                _coroutine.resume();
    87         }
    88 };
    89 
    90 struct Cons {
     88                return _coroutine.promise().next;
     89        }
     90};
     91
     92struct Cons : public resumable {
    9193        struct local {
    9294                Prod * p;
     
    9799        struct promise_type {
    98100                local _l;
     101                resumable * next;
    99102
    100103                Cons get_return_object() {
     
    154157                struct ret {
    155158                        int _status;
     159                        Cons * c;
    156160                        bool await_ready() { return false; }
    157                         void await_suspend(std::experimental::coroutine_handle<>) {}
     161                        void await_suspend(std::experimental::coroutine_handle<Prod::promise_type> _coroutine) {
     162                                _coroutine.promise().next = c;
     163                        }
    158164                        int await_resume() { return _status; }
    159165                };
    160                 return ret{ _coroutine.promise()._l.status };
     166                return ret{ _coroutine.promise()._l.status, this };
    161167        }
    162168
     
    164170                _coroutine.promise()._l.done = true;
    165171                struct ret {
     172                        Cons * c;
     173                        Prod::promise_type * _promise;
    166174                        bool await_ready() { return false; }
    167                         void await_suspend(std::experimental::coroutine_handle<>) {}
    168                         void await_resume() {}
     175                        void await_suspend(std::experimental::coroutine_handle<Prod::promise_type> _coroutine) {
     176                                _promise = &_coroutine.promise();
     177                                _promise->next = c;
     178                        }
     179                        void await_resume() {
     180                                _promise->next = nullptr;
     181                        }
    169182                };
    170                 return ret{};
    171         }
    172 };
     183                return ret{this, nullptr};
     184        }
     185
     186        virtual resumable * resume() override final {
     187                _coroutine.resume();
     188                return _coroutine.promise().next;
     189        }
     190};
     191
     192struct Prod::payment_return {
     193        int _receipt;
     194        Prod * p;
     195        bool await_ready() { return false; }
     196        void await_suspend(std::experimental::coroutine_handle<Cons::promise_type> _coroutine) {
     197                _coroutine.promise().next = p;
     198        }
     199        int await_resume() { return _receipt; }
     200};
     201
     202Prod::payment_return Prod::payment(int money)  {
     203        _coroutine.promise()._l.money = money;
     204        return payment_return{ _coroutine.promise()._l.receipt, this };
     205}
    173206
    174207Prod Prod::main() {
     
    176209        for(int i = 0; i < p.N; i++) {
    177210                int p1 = random(100), p2 = random(100);
    178                 std::cout << p1 << " " << p2;
     211                std::cout << p1 << " " << p2 << std::endl;
    179212                int status = co_await p.c->deliver(p1, p2);
    180                 std::cout << " $" << p.money << std::endl << status;
     213                std::cout << " $" << p.money << std::endl << status << std::endl;
    181214                p.receipt += 1;
    182215        }
    183216        co_await p.c->stop();
    184         std::cout << "prod stops";
     217        std::cout << "prod stops" << std::endl;
    185218}
    186219
     
    189222        int money = 1, receipt;
    190223        for(;!c.done ;) {
    191                 std::cout << c.p1 << " " << c.p2 << std::endl << " $" << money;
     224                std::cout << c.p1 << " " << c.p2 << std::endl;
     225                std::cout << " $ " << money << std::endl;
    192226                c.status += 1;
    193227                receipt = co_await c.p->payment( money );
    194                 std::cout << " #" << receipt;
     228                std::cout << " # " << receipt << std::endl;
    195229                money += 1;
    196230        }
    197         std::cout << "const stops";
     231        std::cout << "cons stops" << std::endl;
     232}
     233
     234void dispatch(resumable * r) {
     235        while((r = r->resume()));
    198236}
    199237
     
    203241        srandom( getpid() );
    204242        prod.start(5, cons);
    205 }
     243        dispatch(&prod);
     244}
Note: See TracChangeset for help on using the changeset viewer.