[85e49a6] | 1 | #include <select.hfa>
|
---|
| 2 | #include <thread.hfa>
|
---|
| 3 | #include <channel.hfa>
|
---|
| 4 |
|
---|
| 5 | channel(long long int) A, 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 a, b, c, i = 0, myTotal = 0;
|
---|
| 13 | bool isDone = false;
|
---|
| 14 | for( ; !isDone; i++ ) {
|
---|
| 15 | waituntil( a << A ) { myTotal += a; }
|
---|
| 16 | and waituntil( b << B ) { myTotal += b; }
|
---|
| 17 | and waituntil( c << C ) { if ( c == -1 ) { isDone = true; } else myTotal += c; } // C_TODO fix breaks inside waituntils
|
---|
| 18 | or else {}
|
---|
| 19 | }
|
---|
| 20 | __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST );
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | size_t numtimes = 100000;
|
---|
| 24 | size_t numServers = 1;
|
---|
| 25 | int main( int argc, char * argv[] ) {
|
---|
| 26 | if ( argc == 2 )
|
---|
| 27 | numtimes = atoi( argv[1] );
|
---|
| 28 |
|
---|
| 29 | processor p[numServers];
|
---|
| 30 | A{5};
|
---|
| 31 | B{5};
|
---|
| 32 | C{5};
|
---|
| 33 |
|
---|
| 34 | long long int total = 0;
|
---|
| 35 | printf("start\n");
|
---|
| 36 | {
|
---|
| 37 | Server1 s[numServers];
|
---|
| 38 | for( long long int j = 0; j < numtimes; j++ ) {
|
---|
| 39 | waituntil( j >> A ) { total += j; }
|
---|
| 40 | and waituntil( j >> B ) { total += j; }
|
---|
| 41 | and waituntil( j >> C ) { total += j; }
|
---|
| 42 | }
|
---|
| 43 | printf("waiting for empty channels\n");
|
---|
| 44 | size_t A_count = get_count( A );
|
---|
| 45 | size_t B_count = get_count( B );
|
---|
| 46 | size_t C_count = get_count( C );
|
---|
| 47 | while( A_count > 0 || B_count > 0 || C_count > 0 ) {
|
---|
| 48 | A_count = get_count( A );
|
---|
| 49 | B_count = get_count( B );
|
---|
| 50 | C_count = get_count( C );
|
---|
| 51 | }
|
---|
| 52 | printf("sending sentinels\n");
|
---|
| 53 | for ( i; numServers ) {
|
---|
| 54 | insert( A, 0 );
|
---|
| 55 | insert( B, 0 );
|
---|
| 56 | insert( C, -1 );
|
---|
| 57 | }
|
---|
| 58 | printf("joining servers\n");
|
---|
| 59 | }
|
---|
| 60 | if ( total != globalTotal )
|
---|
| 61 | printf("CHECKSUM MISMATCH!! Main thread got %lld, server sum is %lld\n", total, globalTotal);
|
---|
| 62 | printf("done\n");
|
---|
| 63 | }
|
---|