source: tests/exceptions/pingpong_nonlocal.cfa@ 65b851a

Last change on this file since 65b851a was c3e510b, checked in by caparsons <caparson@…>, 2 years ago

added support for resuming at resumer, added test for support and cleaned up uneeded include in pingpong exception test

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#include <fstream.hfa>
2#include <thread.hfa>
3#include <mutex_stmt.hfa>
4
5exception num_ping_pongs { int num; };
6vtable(num_ping_pongs) num_ping_pongs_vt;
7
8thread Ping_Pong {
9 char * name;
10 int cnt;
11 num_ping_pongs except;
12 Ping_Pong & partner;
13};
14
15void ?{}( Ping_Pong & this, char * name ) with( this ) {
16 this.name = name;
17 cnt = 0;
18 ?{}( except, &num_ping_pongs_vt, 0 );
19}
20
21void main( Ping_Pong & this ) with( this ) {
22 enum { numtimes = 100 };
23
24 void inc_resume_at( int value ) {
25 except.num = value + 1;
26 resumeAt( partner, except );
27 }
28 try {
29 for () {
30 while( ! poll( this ) ) { yield(); }
31 inc_resume_at( cnt );
32 }
33 } catchResume( num_ping_pongs * e; e->num < numtimes ) {
34 mutex( sout ) sout | name | "catchResume" | cnt | e->num;
35 cnt = e->num;
36 } catch( num_ping_pongs * e ) {
37 mutex( sout ) sout | name | "catch" | cnt | e->num;
38 if ( e->num == numtimes ) {
39 inc_resume_at( e->num );
40 }
41 }
42}
43
44int main() {
45 processor p;
46 sout | "main start";
47 {
48 Ping_Pong ping { "ping" }, pong{ "pong" };
49 &ping.partner = &pong; // create cycle
50 &pong.partner = &ping;
51 num_ping_pongs except{ &num_ping_pongs_vt, 0 };
52 resumeAt( pong, except );
53 }
54 sout | "main end";
55}
Note: See TracBrowser for help on using the repository browser.