source: tests/exceptions/pingpong_nonlocal.cfa @ d344a63

Last change on this file since d344a63 was 77bc259, checked in by Peter A. Buhr <pabuhr@…>, 10 months ago

move exception macro to general location, update more code to use macros

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <fstream.hfa>
2#include <thread.hfa>
3#include <mutex_stmt.hfa>
4#include <Exception.hfa>
5
6ExceptionDecl( num_ping_pongs, int num; );
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, ExceptionArgs( num_ping_pongs, 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                resumeAt( pong, ExceptionInst( num_ping_pongs, 0 ) );
52        }
53        sout | "main end";
54}
Note: See TracBrowser for help on using the repository browser.