1 | #include <select.hfa> |
---|
2 | #include <thread.hfa> |
---|
3 | #include <channel.hfa> |
---|
4 | |
---|
5 | channel(long long int) A, B, C; |
---|
6 | |
---|
7 | long long int globalTotal = 0; |
---|
8 | |
---|
9 | thread Server1 {}; |
---|
10 | void main( Server1 & this ) { |
---|
11 | long long int a, b, c, i = 0, myTotal = 0; |
---|
12 | for( ;;i++ ) { |
---|
13 | when( i % 2 == 0 ) waituntil( a << A ) { myTotal += a; } |
---|
14 | or when( i % 4 < 2 ) waituntil( b << B ) { myTotal += b; } |
---|
15 | or waituntil( c << C ) { if ( c == -1 ) break; myTotal += c; } |
---|
16 | or when( i % 8 < 4 ) else {} |
---|
17 | } |
---|
18 | __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST ); |
---|
19 | } |
---|
20 | |
---|
21 | thread Drainer {}; // ensures that the changing when states of Server1 don't result in a deadlock |
---|
22 | void main( Drainer & this ) { |
---|
23 | long long int a, b, c, myTotal = 0; |
---|
24 | for( ;; ) { |
---|
25 | waituntil( a << A ) { myTotal += a; } |
---|
26 | or waituntil( b << B ) { myTotal += b; } |
---|
27 | or waituntil( c << C ) { if ( c == -1 ) break; myTotal += c; } |
---|
28 | or else {} |
---|
29 | } |
---|
30 | __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST ); |
---|
31 | } |
---|
32 | |
---|
33 | size_t numtimes = 100000; |
---|
34 | size_t numServers = 3; |
---|
35 | int main( int argc, char * argv[] ) { |
---|
36 | if ( argc == 2 ) |
---|
37 | numtimes = atoi( argv[1] ); |
---|
38 | |
---|
39 | processor p[numServers + 1]; |
---|
40 | A{5}; |
---|
41 | B{5}; |
---|
42 | C{5}; |
---|
43 | |
---|
44 | long long int total = 0; |
---|
45 | printf("start\n"); |
---|
46 | { |
---|
47 | Server1 s[numServers]; |
---|
48 | Drainer d; |
---|
49 | { |
---|
50 | for( long long int j = 0; j < numtimes; j++ ) { |
---|
51 | when( j % 2 == 0 ) waituntil( j >> A ) { total += j; } |
---|
52 | or when( j % 4 < 2 ) waituntil( j >> B ) { total += j; } |
---|
53 | and when( j % 8 < 4 ) waituntil( j >> C ) { total += j; } |
---|
54 | } |
---|
55 | } |
---|
56 | printf("waiting for empty channels\n"); |
---|
57 | while( get_count( A ) > 0 || get_count( B ) > 0 || get_count( C ) > 0 ) { } |
---|
58 | printf("sending sentinels\n"); |
---|
59 | for ( i; numServers + 1 ) insert( C, -1 ); |
---|
60 | printf("joining servers\n"); |
---|
61 | } |
---|
62 | if ( total != globalTotal ) |
---|
63 | printf("CHECKSUM MISMATCH!! Main thread got %lld, server sum is %lld\n", total, globalTotal); |
---|
64 | printf("done\n"); |
---|
65 | } |
---|