Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/exceptions/pingpong_nonlocal.cfa

    r64e3ac7 ra2eb21a  
    11#include <fstream.hfa>
    22#include <thread.hfa>
    3 #include <coroutine.hfa>
    4 #include <stdlib.hfa>
    53#include <fstream.hfa>
     4#include <mutex_stmt.hfa>
    65
    7 exception num_pings { int num; };
    8 vtable(num_pings) num_pings_vt;
     6exception num_ping_pongs { int num; };
     7vtable(num_ping_pongs) num_ping_pongs_vt;
    98
    10 exception num_pongs { int num; };
    11 vtable(num_pongs) num_pongs_vt;
     9thread Ping_Pong {
     10        char * name;
     11        int cnt;
     12        num_ping_pongs except;
     13        Ping_Pong & partner;
     14};
    1215
    13 thread Ping;
    14 thread Pong { Ping & p; int cnt; };
    15 thread Ping { Pong & p; int cnt; };
    16 
    17 int numtimes = 100000;
    18 
    19 void main( Pong & this ) with(this) {
    20     try {
    21         for ( ;; ) {
    22             while( !poll( this ) ) {}
    23             num_pongs except{ &num_pongs_vt, cnt + 1 };
    24             resumeAt( p, except );
    25         }
    26     } catchResume ( num_pings * e; e->num < numtimes ) {
    27         cnt = e->num;
    28     } catch( num_pings * e ) {
    29         if ( e->num == numtimes ){
    30             num_pongs except{ &num_pongs_vt, e->num + 1 };
    31             resumeAt( p, except );
    32         }
    33     }
     16void ?{}( Ping_Pong & this, char * name ) with( this ) {
     17        this.name = name;
     18        cnt = 0;
     19        ?{}( except, &num_ping_pongs_vt, 0 );
    3420}
    3521
    36 void main( Ping & this ) with(this) {
    37     try {
    38         for ( ;; ) {
    39             while( !poll( this ) ) {}
    40             num_pings except{ &num_pings_vt, cnt + 1 };
    41             resumeAt( p, except );
    42         }
    43     } catchResume ( num_pongs * e; e->num < numtimes ) {
    44         cnt = e->num;
    45     } catch( num_pongs * e ) {
    46         if ( e->num == numtimes ){
    47             num_pings except{ &num_pings_vt, e->num + 1 };
    48             resumeAt( p, except );
    49         }
    50     }
     22int numtimes = 100;
     23
     24void main( Ping_Pong & this ) with( this ) {
     25        void inc_resume_at( int value ) {
     26                except.num = value + 1;
     27                resumeAt( partner, except );
     28        }
     29        mutex( sout ) sout | name | "start";
     30        try {
     31                for () {
     32                        while( ! poll( this ) ) { yield(); }
     33            inc_resume_at( cnt );
     34                }
     35        } catchResume( num_ping_pongs * e; e->num < numtimes ) {
     36                mutex( sout ) sout | name | "catchResume" | cnt | e->num;
     37                cnt = e->num;
     38        } catch( num_ping_pongs * e ) {
     39                mutex( sout ) sout | name | "catch" | cnt | e->num;
     40                if ( e->num == numtimes ) {
     41            inc_resume_at( e->num );
     42                }
     43        }
     44        mutex( sout ) sout | name | "end";
    5145}
    5246
    5347int main() {
    54     processor p;
    55     sout | "start";
    56     {
    57         Ping ping;
    58         Pong pong;
    59         &ping.p = &pong;
    60         &pong.p = &ping;
    61         num_pings except{ &num_pings_vt, 0 };
    62         resumeAt( pong, except );
    63     }
    64     sout | "done";
     48        processor p;
     49        sout | "main start";
     50        {
     51                Ping_Pong ping { "ping" }, pong{ "pong" };
     52                &ping.partner = &pong;                                                  // create cycle
     53                &pong.partner = &ping;
     54                num_ping_pongs except{ &num_ping_pongs_vt, 0 };
     55                resumeAt( pong, except );
     56        }
     57        sout | "main end";
    6558}
    66 
Note: See TracChangeset for help on using the changeset viewer.