- File:
-
- 1 edited
-
tests/exceptions/pingpong_nonlocal.cfa (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
tests/exceptions/pingpong_nonlocal.cfa
r64e3ac7 ra2eb21a 1 1 #include <fstream.hfa> 2 2 #include <thread.hfa> 3 #include <coroutine.hfa>4 #include <stdlib.hfa>5 3 #include <fstream.hfa> 4 #include <mutex_stmt.hfa> 6 5 7 exception num_ping s { int num; };8 vtable(num_ping s) num_pings_vt;6 exception num_ping_pongs { int num; }; 7 vtable(num_ping_pongs) num_ping_pongs_vt; 9 8 10 exception num_pongs { int num; }; 11 vtable(num_pongs) num_pongs_vt; 9 thread Ping_Pong { 10 char * name; 11 int cnt; 12 num_ping_pongs except; 13 Ping_Pong & partner; 14 }; 12 15 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 } 16 void ?{}( Ping_Pong & this, char * name ) with( this ) { 17 this.name = name; 18 cnt = 0; 19 ?{}( except, &num_ping_pongs_vt, 0 ); 34 20 } 35 21 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 } 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"; 51 45 } 52 46 53 47 int 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"; 65 58 } 66
Note:
See TracChangeset
for help on using the changeset viewer.