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