1 | #include <select.hfa>
|
---|
2 | #include <thread.hfa>
|
---|
3 | #include <channel.hfa>
|
---|
4 | #include <time.hfa>
|
---|
5 |
|
---|
6 | channel(long long int) A, B;
|
---|
7 |
|
---|
8 | volatile long long int inserts = 0;
|
---|
9 | volatile long long int removes = 0;
|
---|
10 |
|
---|
11 | thread Producer {};
|
---|
12 | void main( Producer & this ) {
|
---|
13 | try {
|
---|
14 | for( long long int i = 0;;i++ ) {
|
---|
15 | waituntil( A << i ) { inserts++; }
|
---|
16 | and waituntil( B << i ) { inserts++; }
|
---|
17 | }
|
---|
18 | } catch ( channel_closed * e ) {}
|
---|
19 | }
|
---|
20 |
|
---|
21 | bool useAnd = false;
|
---|
22 | thread Consumer {}; // ensures that the changing when states of Server1 don't result in a deadlock
|
---|
23 | void main( Consumer & this ) {
|
---|
24 | long long int in, in2, A_removes = 0, B_removes = 0;
|
---|
25 | try {
|
---|
26 | for( ;; ) {
|
---|
27 | if ( useAnd ) {
|
---|
28 | waituntil( (in << A) ) { __atomic_thread_fence( __ATOMIC_SEQ_CST ); assert( A_removes == in ); A_removes++; removes++; }
|
---|
29 | and waituntil( (in2 << B) ) { __atomic_thread_fence( __ATOMIC_SEQ_CST ); assert( B_removes == in2 ); B_removes++; removes++; }
|
---|
30 | continue;
|
---|
31 | }
|
---|
32 | waituntil( (in << A) ) { __atomic_thread_fence( __ATOMIC_SEQ_CST ); assert( A_removes == in ); A_removes++; removes++; }
|
---|
33 | or waituntil( (in << B) ) { __atomic_thread_fence( __ATOMIC_SEQ_CST ); ( B_removes == in ); B_removes++; removes++; }
|
---|
34 | }
|
---|
35 | } catchResume ( channel_closed * e ) {} // continue to remove until would block
|
---|
36 | catch ( channel_closed * e ) {}
|
---|
37 | try {
|
---|
38 | for( ;; )
|
---|
39 | waituntil( (in << A) ) { __atomic_thread_fence( __ATOMIC_SEQ_CST ); assert( A_removes == in ); A_removes++; removes++; }
|
---|
40 | } catchResume ( channel_closed * e ) {} // continue to remove until would block
|
---|
41 | catch ( channel_closed * e ) {}
|
---|
42 | try {
|
---|
43 | for( ;; )
|
---|
44 | waituntil( (in << B) ) { __atomic_thread_fence( __ATOMIC_SEQ_CST ); assert( B_removes == in ); B_removes++; removes++; }
|
---|
45 | } catchResume ( channel_closed * e ) {} // continue to remove until would block
|
---|
46 | catch ( channel_closed * e ) {}
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | size_t time = 5;
|
---|
51 | int main( int argc, char * argv[] ) {
|
---|
52 | if ( argc == 2 )
|
---|
53 | time = atoi( argv[1] );
|
---|
54 |
|
---|
55 | processor p[2];
|
---|
56 | A{5};
|
---|
57 | B{5};
|
---|
58 |
|
---|
59 | printf("start OR\n");
|
---|
60 | {
|
---|
61 | Producer p;
|
---|
62 | Consumer c;
|
---|
63 | sleep(time`s);
|
---|
64 | printf("done sleep\n");
|
---|
65 | printf("closing A\n");
|
---|
66 | close(A);
|
---|
67 | printf("closing B\n");
|
---|
68 | close(B);
|
---|
69 | }
|
---|
70 | if ( inserts != removes )
|
---|
71 | printf("CHECKSUM MISMATCH!! Producer got: %lld, Consumer got: %lld\n", inserts, removes);
|
---|
72 | printf("done\n");
|
---|
73 | ^A{};
|
---|
74 | ^B{};
|
---|
75 |
|
---|
76 | useAnd = true;
|
---|
77 |
|
---|
78 | inserts = 0;
|
---|
79 | removes = 0;
|
---|
80 | A{5};
|
---|
81 | B{5};
|
---|
82 | printf("start AND\n");
|
---|
83 | {
|
---|
84 | Producer p;
|
---|
85 | Consumer c;
|
---|
86 | sleep(time`s);
|
---|
87 | printf("done sleep\n");
|
---|
88 | printf("closing A\n");
|
---|
89 | close(A);
|
---|
90 | printf("closing B\n");
|
---|
91 | close(B);
|
---|
92 | }
|
---|
93 | if ( inserts != removes )
|
---|
94 | printf("CHECKSUM MISMATCH!! Producer got: %lld, Consumer got: %lld\n", inserts, removes);
|
---|
95 | printf("done\n");
|
---|
96 | }
|
---|