|
Last change
on this file since 37ceccb 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
|
| Rev | Line | |
|---|
| [bef2245] | 1 | #include <fstream.hfa>
|
|---|
| 2 | #include <thread.hfa>
|
|---|
| [a2eb21a] | 3 | #include <mutex_stmt.hfa>
|
|---|
| 4 |
|
|---|
| 5 | exception num_ping_pongs { int num; };
|
|---|
| 6 | vtable(num_ping_pongs) num_ping_pongs_vt;
|
|---|
| 7 |
|
|---|
| 8 | thread Ping_Pong {
|
|---|
| 9 | char * name;
|
|---|
| 10 | int cnt;
|
|---|
| 11 | num_ping_pongs except;
|
|---|
| 12 | Ping_Pong & partner;
|
|---|
| 13 | };
|
|---|
| 14 |
|
|---|
| 15 | void ?{}( Ping_Pong & this, char * name ) with( this ) {
|
|---|
| 16 | this.name = name;
|
|---|
| 17 | cnt = 0;
|
|---|
| 18 | ?{}( except, &num_ping_pongs_vt, 0 );
|
|---|
| [bef2245] | 19 | }
|
|---|
| 20 |
|
|---|
| [a2eb21a] | 21 | void 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(); }
|
|---|
| 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 | }
|
|---|
| [bef2245] | 42 | }
|
|---|
| 43 |
|
|---|
| 44 | int 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;
|
|---|
| 51 | num_ping_pongs except{ &num_ping_pongs_vt, 0 };
|
|---|
| 52 | resumeAt( pong, except );
|
|---|
| 53 | }
|
|---|
| 54 | sout | "main end";
|
|---|
| [bef2245] | 55 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.