#include #include #include "channel.hfa" // forall(otype T) { void ?{}( channel & this, int _size ) with(this) { buffer = anew( _size ); front = 0; back = 0; count = 0; size = _size; (lock){}; (prods){}; (cons){}; } void ^?{}( channel & this ) { delete( this.buffer ); } void put( channel & this, int elem ) with( this ) { lock( this.lock ); while( count == size) { wait( prods, this.lock ); } /* paranoid */ assert( count < size ); buffer[back] = elem; count ++; back = (back + 1) % size; notify_one( cons ); unlock( this.lock ); } int take( channel & this ) with( this ) { lock( this.lock ); while( count == 0 ) { wait( cons, this.lock ); } /* paranoid */ assert( count > 0 ); int temp = buffer[front]; count --; front = (front + 1) % size; notify_one( prods ); unlock( this.lock ); return temp; } // }