source: tests/exceptions/pingpong_nonlocal.cfa @ caa3e2c

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

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

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[bef2245]1#include <fstream.hfa>
2#include <thread.hfa>
[a2eb21a]3#include <mutex_stmt.hfa>
[77bc259]4#include <Exception.hfa>
[a2eb21a]5
[77bc259]6ExceptionDecl( num_ping_pongs, int num; );
[a2eb21a]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;
[77bc259]18        ?{}( except, ExceptionArgs( num_ping_pongs, 0 ) );
[bef2245]19}
20
[a2eb21a]21void main( Ping_Pong & this ) with( this ) {
[ffac259]22        enum { numtimes = 100 };
23
[a2eb21a]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(); }
[77bc259]31                        inc_resume_at( cnt );
[a2eb21a]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 ) {
[77bc259]39                        inc_resume_at( e->num );
[a2eb21a]40                }
41        }
[bef2245]42}
43
44int main() {
[a2eb21a]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;
[77bc259]51                resumeAt( pong, ExceptionInst( num_ping_pongs, 0 ) );
[a2eb21a]52        }
53        sout | "main end";
[bef2245]54}
Note: See TracBrowser for help on using the repository browser.