ADTast-experimental
Last change
on this file since 1c80f20 was
0aec496,
checked in by Thierry Delisle <tdelisle@…>, 4 years ago
|
First attempt at webserver, no option support yet
|
-
Property mode set to
100644
|
File size:
935 bytes
|
Rev | Line | |
---|
[0aec496] | 1 | #include <assert.h> |
---|
| 2 | #include <stdlib.hfa> |
---|
| 3 | |
---|
| 4 | #include "channel.hfa" |
---|
| 5 | |
---|
| 6 | // forall(otype T) { |
---|
| 7 | void ?{}( channel & this, int _size ) with(this) { |
---|
| 8 | buffer = anew( _size ); |
---|
| 9 | front = 0; |
---|
| 10 | back = 0; |
---|
| 11 | count = 0; |
---|
| 12 | size = _size; |
---|
| 13 | |
---|
| 14 | (lock){}; |
---|
| 15 | (prods){}; |
---|
| 16 | (cons){}; |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | void ^?{}( channel & this ) { |
---|
| 20 | delete( this.buffer ); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | void put( channel & this, int elem ) with( this ) { |
---|
| 24 | lock( this.lock ); |
---|
| 25 | while( count == size) { |
---|
| 26 | wait( prods, this.lock ); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | /* paranoid */ assert( count < size ); |
---|
| 30 | buffer[back] = elem; |
---|
| 31 | count ++; |
---|
| 32 | back = (back + 1) % size; |
---|
| 33 | |
---|
| 34 | notify_one( cons ); |
---|
| 35 | unlock( this.lock ); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | int take( channel & this ) with( this ) { |
---|
| 39 | lock( this.lock ); |
---|
| 40 | while( count == 0 ) { |
---|
| 41 | wait( cons, this.lock ); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /* paranoid */ assert( count > 0 ); |
---|
| 45 | int temp = buffer[front]; |
---|
| 46 | count --; |
---|
| 47 | front = (front + 1) % size; |
---|
| 48 | |
---|
| 49 | notify_one( prods ); |
---|
| 50 | unlock( this.lock ); |
---|
| 51 | return temp; |
---|
| 52 | } |
---|
| 53 | // } |
---|
Note: See
TracBrowser
for help on using the repository browser.