| 1 | #include <select.hfa> | 
|---|
| 2 | #include <thread.hfa> | 
|---|
| 3 | #include <channel.hfa> | 
|---|
| 4 |  | 
|---|
| 5 | channel(long long int) B, C; | 
|---|
| 6 |  | 
|---|
| 7 | volatile bool done = false; | 
|---|
| 8 | long long int globalTotal = 0; | 
|---|
| 9 |  | 
|---|
| 10 | thread Server1 {}; | 
|---|
| 11 | void main( Server1 & this ) { | 
|---|
| 12 | long long int b, c, i = 0, myTotal = 0; | 
|---|
| 13 | for( ;;i++ ) { | 
|---|
| 14 | waituntil( b << B ) { myTotal += b; } | 
|---|
| 15 | or waituntil( c << C ) { if ( c == -1 ) break; myTotal += c; } | 
|---|
| 16 | } | 
|---|
| 17 | __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST ); | 
|---|
| 18 | } | 
|---|
| 19 |  | 
|---|
| 20 | size_t numtimes = 100000; | 
|---|
| 21 | size_t numServers = 1; | 
|---|
| 22 | int main( int argc, char * argv[] ) { | 
|---|
| 23 | if ( argc == 2 ) | 
|---|
| 24 | numtimes = atoi( argv[1] ); | 
|---|
| 25 |  | 
|---|
| 26 | processor p[numServers]; | 
|---|
| 27 | B{5}; | 
|---|
| 28 | C{5}; | 
|---|
| 29 |  | 
|---|
| 30 | long long int total = 0; | 
|---|
| 31 | printf("start\n"); | 
|---|
| 32 | { | 
|---|
| 33 | Server1 s[numServers]; | 
|---|
| 34 | for( long long int j = 0; j < numtimes; j++ ) { | 
|---|
| 35 | waituntil( j >> B ) { total += j; } | 
|---|
| 36 | or waituntil( j >> C ) { total += j; } | 
|---|
| 37 | } | 
|---|
| 38 | printf("waiting for empty channels\n"); | 
|---|
| 39 | size_t B_count = get_count( B ); | 
|---|
| 40 | size_t C_count = get_count( C ); | 
|---|
| 41 | while( B_count > 0 || C_count > 0 ) { | 
|---|
| 42 | B_count = get_count( B ); | 
|---|
| 43 | C_count = get_count( C ); | 
|---|
| 44 | } | 
|---|
| 45 | printf("sending sentinels\n"); | 
|---|
| 46 | for ( i; numServers ) insert( C, -1 ); | 
|---|
| 47 | printf("joining servers\n"); | 
|---|
| 48 | } | 
|---|
| 49 | if ( total != globalTotal ) | 
|---|
| 50 | printf("CHECKSUM MISMATCH!! Main thread got %lld, server sum is %lld\n", total, globalTotal); | 
|---|
| 51 | printf("done\n"); | 
|---|
| 52 | } | 
|---|