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