source: tests/exceptions/pingpong_nonlocal.cfa @ b93bf85

Last change on this file since b93bf85 was 4c8ce47, checked in by caparsons <caparson@…>, 16 months ago

updated pingpong exception test to remove output race for repeatable test output

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