Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/exceptions/pingpong_nonlocal.cfa

    ra2eb21a r64e3ac7  
    11#include <fstream.hfa>
    22#include <thread.hfa>
     3#include <coroutine.hfa>
     4#include <stdlib.hfa>
    35#include <fstream.hfa>
    4 #include <mutex_stmt.hfa>
    56
    6 exception num_ping_pongs { int num; };
    7 vtable(num_ping_pongs) num_ping_pongs_vt;
     7exception num_pings { int num; };
     8vtable(num_pings) num_pings_vt;
    89
    9 thread Ping_Pong {
    10         char * name;
    11         int cnt;
    12         num_ping_pongs except;
    13         Ping_Pong & partner;
    14 };
     10exception num_pongs { int num; };
     11vtable(num_pongs) num_pongs_vt;
    1512
    16 void ?{}( Ping_Pong & this, char * name ) with( this ) {
    17         this.name = name;
    18         cnt = 0;
    19         ?{}( except, &num_ping_pongs_vt, 0 );
     13thread Ping;
     14thread Pong { Ping & p; int cnt; };
     15thread Ping { Pong & p; int cnt; };
     16
     17int numtimes = 100000;
     18
     19void 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    }
    2034}
    2135
    22 int numtimes = 100;
    23 
    24 void 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";
     36void 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    }
    4551}
    4652
    4753int main() {
    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";
     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";
    5865}
     66
Note: See TracChangeset for help on using the changeset viewer.