source: tests/concurrent/waituntil/channels.cfa@ 085f67a

ADT ast-experimental
Last change on this file since 085f67a was a33a5e2, checked in by caparsons <caparson@…>, 2 years ago

added tests for the waituntil stmt

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#include <select.hfa>
2#include <thread.hfa>
3#include <channel.hfa>
4
5channel(long long int) A, B, C;
6
7volatile bool done = false;
8long long int globalTotal = 0;
9
10thread Server1 {};
11void main( Server1 & this ) {
12 long long int a, b, c, i = 0, myTotal = 0;
13 for( ;;i++ ) {
14 when( i % 2 == 0 ) waituntil( (a << A) ) { myTotal += a; }
15 or when( i % 4 < 2 ) waituntil( (b << B) ) { myTotal += b; }
16 or waituntil( (c << C) ) { if ( c == -1 ) break; myTotal += c; }
17 or when( i % 8 < 4 ) else {}
18 }
19 __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST );
20}
21
22thread Drainer {}; // ensures that the changing when states of Server1 don't result in a deadlock
23void main( Drainer & this ) {
24 long long int a, b, c, i = 0, myTotal = 0;
25 for( ;;i++ ) {
26 waituntil( (a << A) ) { myTotal += a; }
27 or waituntil( (b << B) ) { myTotal += b; }
28 or waituntil( (c << C) ) { if ( c == -1 ) break; myTotal += c; }
29 }
30 __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST );
31}
32
33thread Churner {}; // performs non-waituntil try insert/remove operations to add churn/interference
34void main( Churner & this ) {
35 long long int out, myTotal = 0;
36 bool success;
37 while( !done ) {
38 try_insert( A, 0 );
39 try_insert( B, 0 );
40 try_insert( C, 0 );
41 [out, success] = try_remove(A);
42 if ( success ) myTotal += out;
43 [out, success] = try_remove(B);
44 if ( success ) myTotal += out;
45 [out, success] = try_remove(C);
46 if ( success ) myTotal += out;
47 }
48 __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST );
49}
50
51size_t numtimes = 100000;
52size_t numServers = 3;
53int main( int argc, char * argv[] ) {
54 if ( argc == 2 )
55 numtimes = atoi( argv[1] );
56
57 processor p[numServers + 2];
58 A{5};
59 B{5};
60 C{5};
61
62 long long int total = 0;
63 printf("start\n");
64 {
65 Server1 s[numServers];
66 Drainer d;
67 {
68 Churner c;
69 for( long long int j = 0; j < numtimes; j++ ) {
70 when( j % 2 == 0 ) waituntil( (j >> A) ) { total += j; }
71 or when( j % 4 < 2 ) waituntil( (j >> B) ) { total += j; }
72 and when( j % 8 < 4 ) waituntil( (j >> C) ) { total += j; }
73 }
74 done = true;
75 printf("terminating churner\n");
76 }
77 printf("waiting for empty channels\n");
78 while( get_count(A) > 0 || get_count(B) > 0 || get_count(C) > 0 ) { Pause(); }
79 printf("sending sentinels\n");
80 for ( i; numServers + 1 ) insert( C, -1 );
81 printf("joining servers\n");
82 }
83 if ( total != globalTotal )
84 printf("CHECKSUM MISMATCH!! Main thread got %lld, server sum is %lld\n", total, globalTotal);
85 printf("done\n");
86}
Note: See TracBrowser for help on using the repository browser.